[英]Use Font Awesome icon as CSS content
我想使用 Font Awesome 图标作为 CSS 内容,即,
a:before {
content: "<i class='fa...'>...</i>";
}
我知道我不能在content
使用 HTML 代码,所以只剩下图像了吗?
感谢Aurelien更新 FontAwesome 5
您需要根据您尝试渲染的图标类型将font-family
更改为Font Awesome 5 Brands
或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;
}
您可以阅读下面的其余答案以了解它的工作原理并了解图标和文本之间的间距的一些解决方法。
FontAwesome 4 及以下
那是错误的使用方法。 打开 font awesome 样式表,转到您要使用的字体class
,例如fa-phone
,使用实体复制该类下的内容属性,然后使用它:
a:before {
font-family: FontAwesome;
content: "\f095";
}
只需确保如果您要针对特定a
标签,请考虑使用class
来使其更具体,例如:
a.class_name:before {
font-family: FontAwesome;
content: "\f095";
}
使用上面的方法会将图标与你的剩余文本粘在一起,所以如果你想在它们两个之间有一点空间,让它display: inline-block;
并使用一些padding-right
:
a:before {
font-family: FontAwesome;
content: "\f095";
display: inline-block;
padding-right: 3px;
vertical-align: middle;
}
进一步扩展这个答案,因为许多人可能需要在悬停时更改图标,因此,为此,我们可以为:hover
操作编写一个单独的选择器和规则:
a:hover:before {
content: "\f099"; /* Code of the icon you want to change on hover */
}
现在在上面的例子中,图标因为大小不同而微移,你肯定不希望那样,所以你可以在基本声明上设置一个固定的width
,比如
a:before {
/* Other properties here, look in the above code snippets */
width: 12px; /* add some desired width here to prevent nudge */
}
另一种无需手动处理 Unicode 字符的解决方案可以在让字体真棒 - 使用没有 i-tags 的图标中找到(免责声明:我写了这篇文章)。
简而言之,您可以像这样创建一个新类:
.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);
}
然后将其与任何图标一起使用,例如:
<a class="icon fa-car" href="#">This is a link</a>
如果你可以从 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;
}
示例链接: https : //codepen.io/bungeedesign/pen/XqeLQg
从以下位置获取图标代码: https : //fontawesome.com/cheatsheet? from =io
您应该将 font-weight 设置为 900,以便Font Awesome 5 Free font-family 工作。
这是工作的:
.css-selector::before {
font-family: 'Font Awesome 5 Free';
content: "\f101";
font-weight: 900;
}
您可以在 CSS 中为此使用 unicode。 如果您使用的是 font awesome 5,这是语法;
.login::before {
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: "\f007";
}
您可以在此处查看他们的文档。
正如它在 FontAwesome 网站FontAwesome =>
HTML:
<span class="icon login"></span> Login</li>
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";
}
在.login::before
-> 编辑content:'';
以适合您的 unicode。
使用 SCSS 更新 Font Awesome 5
.icon {
@extend %fa-icon;
@extend .fas;
&:before {
content: fa-content($fa-var-user);
}
}
这是我的 webpack 4 + font awesome 5 解决方案:
网络包插件:
new CopyWebpackPlugin([
{ from: 'node_modules/font-awesome/fonts', to: 'font-awesome' }
]),
全局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.