简体   繁体   English

使用 Font Awesome 图标作为 CSS 内容

[英]Use Font Awesome icon as CSS content

I want to use a Font Awesome icon as CSS content, ie,我想使用 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?我知道我不能在content使用 HTML 代码,所以只剩下图像了吗?

Update for FontAwesome 5 Thanks to Aurelien感谢Aurelien更新 FontAwesome 5

You need to change the font-family to Font Awesome 5 Brands OR Font Awesome 5 Free , based on the type of icon you are trying to render.您需要根据您尝试渲染的图标类型将font-family更改为Font Awesome 5 BrandsFont Awesome 5 Free Also, do not forget to declare font-weight: 900;另外,不要忘记声明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 class of the font you want to use say fa-phone , copy the content property under that class with the entity, and use it like:打开 font awesome 样式表,转到您要使用的字体class ,例如fa-phone ,使用实体复制该类下的内容属性,然后使用它:

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

Demo演示

Just make sure that if you are looking to target a specific a tag, then consider using a class instead to make it more specific like:只需确保如果您要针对特定a标签,请考虑使用class来使其更具体,例如:

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;使用上面的方法会将图标与你的剩余文本粘在一起,所以如果你想在它们两个之间有一点空间,让它display: inline-block; and use some padding-right :并使用一些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 :hover action:进一步扩展这个答案,因为许多人可能需要在悬停时更改图标,因此,为此,我们可以为: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 width on 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演示

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>

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;
}
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

You should have font-weight set to 900 for Font Awesome 5 Free font-family to work.您应该将 font-weight 设置为 900,以便Font Awesome 5 Free font-family 工作。

This is the working one:这是工作的:

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

You can use unicode for this in CSS.您可以在 CSS 中为此使用 unicode。 If you are using font awesome 5, this is the syntax;如果您使用的是 font awesome 5,这是语法;

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

You can see their documentation here .您可以在此处查看他们的文档。

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:'';.login::before -> 编辑content:''; to suit your unicode.以适合您的 unicode。

Update for Font Awesome 5 using SCSS使用 SCSS 更新 Font Awesome 5

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

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

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM