Html 使用 Font Awesome 图标作为 CSS 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20782368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:12:00  来源:igfitidea点击:

Use Font Awesome icon as CSS content

htmlcssfont-awesome

提问by sdvnksv

I want to use a Font Awesome icon as CSS content, i.e.,

我想使用 Font Awesome 图标作为 CSS 内容,即,

a:before {
    content: "<i class='fa...'>...</i>";
}

I know I cannot use HTML code in content, so is it only images left?

我知道我不能在 中使用 HTML 代码content,所以只剩下图像了吗?

回答by Mr. Alien

Update for FontAwesome 5Thanks to Aurelien

感谢Aurelien更新 FontAwesome 5

You need to change the font-familyto Font Awesome 5 BrandsOR Font Awesome 5 Free, based on the type of icon you are trying to render. Also, do not forget to declare font-weight: 900;

您需要根据您尝试呈现的图标类型将 更改font-familyFont Awesome 5 BrandsOR Font Awesome 5 Free。另外,不要忘记声明font-weight: 900;

a:before {
   font-family: "Font Awesome 5 Free";
   content: "\f095";
   display: inline-block;
   padding-right: 3px;
   vertical-align: middle;
   font-weight: 900;
}

Demo

演示

You can read the rest of the answer below to understand how it works and to know some workarounds for spacing between icon and the text.

您可以阅读下面的其余答案以了解它的工作原理并了解图标和文本之间的间距的一些解决方法。



FontAwesome 4 and below

FontAwesome 4 及以下

That's the wrong way to use it. Open the font awesome style sheet, go to the classof the font you want to use say fa-phone, copy the content property under that class with the entity, and use it like:

那是错误的使用方法。打开字体真棒样式表,转到class您要使用的字体的 say fa-phone,使用实体复制该类下的内容属性,然后使用它:

a:before {
    font-family: FontAwesome;
    content: "\f095";
}

Demo

演示

Just make sure that if you are looking to target a specific atag, then consider using a classinstead to make it more specific like:

只需确保如果您要定位特定a标签,请考虑使用 aclass来使其更具体,例如:

a.class_name:before {
    font-family: FontAwesome;
    content: "\f095";
}

Using the way above will stick the icon with the remaining text of yours, so if you want to have a bit of space between the two of them, make it display: inline-block;and use some padding-right:

使用上面的方法会将图标与您的剩余文本粘贴在一起,因此,如果您想在两者之间留出一点空间display: inline-block;,请使用一些空间padding-right

a:before {
    font-family: FontAwesome;
    content: "\f095";
    display: inline-block;
    padding-right: 3px;
    vertical-align: middle;
}


Extending this answer further, since many might be having a requirement to change an icon on hover, so for that, we can write a separate selector and rules for :hoveraction:

进一步扩展这个答案,因为许多人可能需要在悬停时更改图标,所以为此,我们可以编写一个单独的选择器和操作规则:hover

a:hover:before {
    content: "\f099"; /* Code of the icon you want to change on hover */
}

Demo

演示

Now in the above example, icon nudges because of the different size and you surely don't want that, so you can set a fixed widthon the base declaration like

现在在上面的例子中,图标因为大小不同而轻推,你肯定不想要这样,所以你可以width在基本声明上设置一个固定的,比如

a:before {
    /* Other properties here, look in the above code snippets */
    width: 12px; /* add some desired width here to prevent nudge */
}

Demo

演示

回答by dustinmoris

Another solution without you having to manually mess around with the Unicode characters can be found in Making Font Awesome awesome - Using icons without i-tags(disclaimer: I wrote this article).

另一种无需手动处理 Unicode 字符的解决方案可以在让字体真棒 - 使用没有 i-tags 的图标中找到(免责声明:我写了这篇文章)。

In a nutshell, you can create a new class like this:

简而言之,您可以像这样创建一个新类:

.icon::before {
    display: inline-block;
    margin-right: .5em;
    font: normal normal normal 14px/1 FontAwesome;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transform: translate(0, 0);
}

And then use it with any icon, for example:

然后将其与任何图标一起使用,例如:

<a class="icon fa-car" href="#">This is a link</a>

回答by ersefuril

If you have access to SCSS files from font-awesome, you can use this simple solution:

如果你可以从 font-awesome 访问 SCSS 文件,你可以使用这个简单的解决方案:

.a:after {
    // Import mixin from font-awesome/scss/mixins.scss
    @include fa-icon();

    // Use icon variable from font-awesome/scss/variables.scss
    content: $fa-var-exclamation-triangle;
}

回答by Pervez

a:before {
     content: "\f055";
     font-family: FontAwesome;
     left:0;
     position:absolute;
     top:0;
}

Example Link: https://codepen.io/bungeedesign/pen/XqeLQg

示例链接:https: //codepen.io/bungeedesign/pen/XqeLQg

Get Icon code from: https://fontawesome.com/cheatsheet?from=io

从以下位置获取图标代码:https: //fontawesome.com/cheatsheet?from =io

回答by Pervez

You should have font-weight set to 900 for Font Awesome 5 Freefont-family to work.

您应该将 font-weight 设置为 900,以便Font Awesome 5 Freefont-family 工作。

This is the working one:

这是工作的:

.css-selector::before {
   font-family: 'Font Awesome 5 Free';
   content: "\f101";
   font-weight: 900;
}

回答by Mahib

You can use unicode for this in CSS. If you are using font awesome 5, this is the syntax;

您可以在 CSS 中为此使用 unicode。如果您使用的是 font awesome 5,这是语法;

.login::before {
    font-family: "Font Awesome 5 Free"; 
    font-weight: 900; 
    content: "\f007";  
}

You can see their documentation here.

您可以在此处查看他们的文档。

回答by Prusakov

As it says at FontAwesome website FontAwesome=>

正如它在 FontAwesome 网站FontAwesome=>

HTML:

HTML:

<span class="icon login"></span> Login</li>

CSS:

CSS:

 .icon::before {
    display: inline-block;
    font-style: normal;
    font-variant: normal;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
  }

    .login::before {
        font-family: "Font Awesome 5 Free";
        font-weight: 900;
        content: "\f007";
   }

In .login::before-> edit content:'';to suit your unicode.

.login::before-> 编辑content:'';以适合您的 unicode。

回答by Friendly Code

Update for Font Awesome 5 using SCSS

使用 SCSS 更新 Font Awesome 5

.icon {
  @extend %fa-icon;
  @extend .fas;

  &:before {
    content: fa-content($fa-var-user);
  }
}

回答by setec

Here's my webpack 4 + font awesome 5 solution:

这是我的 webpack 4 + font awesome 5 解决方案:

webpack plugin:

网络包插件:

new CopyWebpackPlugin([
    { from: 'node_modules/font-awesome/fonts', to: 'font-awesome' }
  ]),

global css style:

全局css样式:

@font-face {
    font-family: 'FontAwesome';
    src: url('/font-awesome/fontawesome-webfont.eot');
    src: url('/font-awesome/fontawesome-webfont.eot?#iefix') format('embedded-opentype'),
    url('/font-awesome/fontawesome-webfont.woff2') format('woff2'),
    url('/font-awesome/fontawesome-webfont.woff') format('woff'),
    url('/font-awesome/fontawesome-webfont.ttf') format('truetype'),
    url('/font-awesome/fontawesome-webfont.svgfontawesomeregular') format('svg');
    font-weight: normal;
    font-style: normal;
}

i {
    font-family: "FontAwesome";
}