Font Awesome 5在伪元素中选择正确的字体系列 [英] Font Awesome 5 Choosing the correct font-family in pseudo-elements
问题描述
我目前对在CSS伪元素中使用图标感到困惑. fontawesome有4种字体家族:Font Awesome 5 Free
,Font Awesome 5 Solid
,Font Awesome 5 Brands
,Font Awesome 5 Regular
这是HTML:
<h1>Hello</h1>
案例1
我使用以下图标: https://fontawesome.com/icons/twitter?style=品牌
如您所见,它是一个brands
图标,所以font-family:Font Awesome 5 Brands
h1:before {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
content: "\f099"; /* TWITTER ICON */
font-family: "Font Awesome 5 Brands";
font-weight: 400;
}
成功!
案例2
我使用以下图标: https://fontawesome.com/icons/phone?style=固体
如您所见,它是一个solid
图标,因此是font-family:Font Awesome 5 Solid
h1:before {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
content: "\f095"; /* PHONE ICON */
font-family: "Font Awesome 5 Solid";
font-weight: 900;
}
不工作!
我做错了什么?
我们如何知道何时使用正确的字体系列?
只需在同一font-family
中全部使用它们,浏览器即可完成工作.如果它在第一个中找不到,它将使用第二个. ( Font-Family属性中的多种字体?)
顺便说一句,正确的font-family
是Free
而不是Solid
,因为 Solid 和 Regular 之间的区别是font-weight
,并且都具有相同的font-family
. 在字体系列中没有 Solid
和Regular
,只有Free
和Brands
.
您可能还会注意到,图标的几乎所有Solid
版本都是免费的,但并非所有regular
版本都是免费的.其中一些包含在PRO软件包中.如果没有显示图标不一定是font-family
问题.
所有Light
和duotone
版本均为PRO版本.
.icon {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
font-family: "Font Awesome 5 Brands","Font Awesome 5 Free";
}
.icon1:before {
content: "\f099";
/* TWITTER ICON */
font-weight: 400;
}
.icon2:before {
content: "\f095";
/* PHONE ICON */
font-weight: 900;
}
.icon3:before {
content: "\f095";
/* PHONE ICON */
font-weight: 400;/*This one will not work because the regular version of the phone icon is in the Pro Package*/
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.0/css/all.css" >
<div class="icon1 icon"></div>
<div class="icon2 icon"></div>
<br>
<div class="icon3 icon"></div>
参考: https://fontawesome.com/how-to-use/on-the-web/advanced/css-pseudo-elements#define
有关font-weight
问题的相关问题:伪元素上的字体真棒5显示正方形而不是图标 >
I'm currently confused in using the icon in CSS pseudo-elements. There are 4 kind of font-family for fontawesome : Font Awesome 5 Free
, Font Awesome 5 Solid
, Font Awesome 5 Brands
, Font Awesome 5 Regular
Here is the HTML :
<h1>Hello</h1>
Case 1
I use this icon : https://fontawesome.com/icons/twitter?style=brands
As you can see, its a brands
icon, so font-family : Font Awesome 5 Brands
h1:before {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
content: "\f099"; /* TWITTER ICON */
font-family: "Font Awesome 5 Brands";
font-weight: 400;
}
IT WORKS!
Case 2
I use this icon : https://fontawesome.com/icons/phone?style=solid
As you can see, its a solid
icon, so font-family : Font Awesome 5 Solid
h1:before {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
content: "\f095"; /* PHONE ICON */
font-family: "Font Awesome 5 Solid";
font-weight: 900;
}
DOESN'T WORK!
What did i do wrong?
How do we know when to use the correct font-family?
Simply use all of them in the same font-family
and the browser will do the job. If it doesn't find it in the first one, it will use the second one. (Multiple fonts in Font-Family property?)
By the way, the correct font-family
is Free
not Solid
because the difference between Solid and Regular is the font-weight
and both have the same font-family
. There is no Solid
and Regular
in font-family, only Free
and Brands
.
You may also notice that almost all the Solid
version of the icons are free BUT not all the regular
version are free. Some of them are included in the PRO package. If an icon is not showing it's not necessarely a font-family
issue.
All the Light
and duotone
version are PRO ones.
.icon {
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
font-family: "Font Awesome 5 Brands","Font Awesome 5 Free";
}
.icon1:before {
content: "\f099";
/* TWITTER ICON */
font-weight: 400;
}
.icon2:before {
content: "\f095";
/* PHONE ICON */
font-weight: 900;
}
.icon3:before {
content: "\f095";
/* PHONE ICON */
font-weight: 400;/*This one will not work because the regular version of the phone icon is in the Pro Package*/
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.0/css/all.css" >
<div class="icon1 icon"></div>
<div class="icon2 icon"></div>
<br>
<div class="icon3 icon"></div>
Reference: https://fontawesome.com/how-to-use/on-the-web/advanced/css-pseudo-elements#define
Related question dealing with the font-weight
issue: Font Awesome 5 on pseudo elements shows square instead of icon
这篇关于Font Awesome 5在伪元素中选择正确的字体系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!