本质是变量
@font face规则支持的CSS属性有:font-family,src,font-weight,font-style,unicode-range,font-variant,font-stretch,font-feature-settings。例如:
@font-face {// 字体变量名,当css中设置了font-family为该值时就会应用src对应字体font-family: 'example';// 指定字体文件,也可以是本地字体 src: local("Microsoft Yahei")src: url(example.ttf);font-style: normal;font-weight: normal;unicode-range: U+0025-00FF;font-variant: small-caps;font-stretch: expanded;font-feature-settings:"liga1" on;}
@font face常用属性
1. src
作用1:字体文件名简写
@font-face {font-family: YH;src: local("Microsoft Yahei");}.font {font-family: YH;}
src还支持多个local字体地址同时出现
body {font-family: PingFangSC-Regular,HelveticaNeue-Light,'Helvetica Neue Light','Microsoft YaHei',sans-serif;}@font-face {font-family: BASE;src: local("HelveticaNeue-Light"), local("Helvetica Neue Light"), local("PingFang SC"), local("Microsoft YaHei"), local(sans-serif);}body {font-family: BASE;}
作用2: 优先使用用户本地字体
如果用户本地已经安装有所需要的字体,就不必再去请求网络资源字体。
@font-face {font-family: FZCYS;src: local("FZYaSongS-B-GB"),url("FZCYS.woff2"),url("FZCYS.woff"),url("FZCYS.ttf");}
2. font-style
@font-face {font-family: 'I';font-style: normal;src: local('FZYaoti');}@font-face {font-family: 'I';font-style: italic;src: local('FZShuTi');}
制定一个字体,名叫’I’,当文字样式正常的时候,字体表现使用“方正姚体”,当文字设置了font-style:italic的时候,字体表现为“方正舒体”。
例子
<p><i class="i">类名是i,标签是i</i></p><p><span class="i">类名是i, 标签是span</span></p>
.i {font-family: I;}
上面一行文字表现为“方正舒体”,下面一行为“方正姚体”
3. font-weight
这个在项目中碰到过
@font-face {font-family: 'Montserrat';src: url('../Montserrat-Black.ttf');font-weight: 900;}@font-face {font-family: 'Montserrat';src: url('../../assets/fonts/badge/Montserrat-SemiBold.ttf');font-weight: 600;}
// 应用 Montserrat-Black.ttf 字体.xxx {font-family: 'Montserrat';font-weight: 900;}// 应用 Montserrat-SemiBold.ttf 字体.xxx {font-family: 'Montserrat';font-weight: 900;}
4. unicode-range
unicode-range的作用是可以让特定的字符或者字符片段使用特定的字体。
