使用字体真棒图标作为css内容

问题描述:

我想使用字体真棒图标作为css内容,例如

I want to use a font awesome icon as a css content, i.e.

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



我知道我不能在 / code>,所以它只剩下图像吗?

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

这是错误的方式使用它,真棒样式表,转到要使用的字体的 fa-phone ,然后复制内容属性

That's the wrong way to use it, open the font awesome stylesheet, go to the class of the font you want to use say fa-phone and then copy the content property under that class with the entity and than use it like

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

演示

只要确保如果您希望定位到 / code>标记,而不是考虑使用,使其更具体,如

Just make sure that if you are looking to target a specific a tag, than consider using a class instead to make it more specific like

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

使用上面的方法将图标与您的剩余文本如果你想在两者之间有一点空格,使它 display:inline-block; 并使用一些 padding-right

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 the, make it display: inline-block; and use some padding-right

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






因为许多人可能需要更改悬停时的图标,所以为此,我们可以为:hover 操作编写单独的选择器和规则


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

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

演示

现在在上面的示例中,图标,因为不同的大小,你肯定不想要的,所以你可以设置一个固定的宽度在基本声明如

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

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

演示