使用字体真棒图标作为css内容 [英] Use font awesome icon as css content

查看:129
本文介绍了使用字体真棒图标作为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 */ 
}

演示

这篇关于使用字体真棒图标作为css内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆