看看同一種字體是如何對應不同的字體文件的

說起在網頁中引入字體文件,首先不得不提一個大多數(shù)前端至少用過或者聽過的庫Font Awesome。使用過這個庫的都知道,只要引入一個 css 文件,然后通過給元素賦予指定的類,就可以展示出一個對應的圖標,而且這個圖標能使用 css 樣式控制其表現(xiàn),比傳統(tǒng)的使用圖片作為圖標好太多了。

Font Awesome 是一種網頁中使用的矢量字體圖標解決方案,得益于 CSS3 中的 CSS Fonts Module Level 3 特性支持。

發(fā)現(xiàn)疑問

今天無意發(fā)現(xiàn) Font Awesome 更新了,升級到 5.x 了之后多了一種 svg with js 的使用方式,嘗試了下和 Element-ui 結合使用,發(fā)現(xiàn)這條路子行不通(因為使用這種方式的時候 font awesome 會把圖標元素的載體元素轉換成 svg 標簽,從到導致了沒法動態(tài)改變圖標的問題,如果沒有動態(tài)改變圖標的需求可以使用這種方式)。

基于以上原因,所以還是使用引入 css 的老方法。然鵝,font5 給圖標增加了分類,分為 solidregular,即同一個圖標可能會有兩種樣式,如下:

<iframe height='166' scrolling='no' title='dJYdxZ' src='//codepen.io/Yuliang-Lee/embed/dJYdxZ/?height=166&theme-id=dark&default-tab=result&embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen <a >dJYdxZ</a> by xlaoyu (<a >@Yuliang-Lee</a>) on <a >CodePen</a>.
</iframe>

此時打開控制面板查看兩個元素的 css,可以??的發(fā)現(xiàn)使用的字體和內容編碼一模一樣

dom-css-content

那么問題來了,為什么圖標會不一樣呢?

事出必有因

我們首先打開引入這兩種圖標的 css 的源碼看看

/* regular.css */
@font-face {
  font-family: 'Font Awesome 5 Free';
  font-style: normal;
  font-weight: 400;
  src: url('#{$fa-font-path}/fa-regular-400.eot');
  src: url('#{$fa-font-path}/fa-regular-400.eot?#iefix') format('embedded-opentype'),
  url('#{$fa-font-path}/fa-regular-400.woff2') format('woff2'),
  url('#{$fa-font-path}/fa-regular-400.woff') format('woff'),
  url('#{$fa-font-path}/fa-regular-400.ttf') format('truetype'),
  url('#{$fa-font-path}/fa-regular-400.svg#fontawesome') format('svg');
}

.far {
  font-family: 'Font Awesome 5 Free';
  font-weight: 400;
}

/* solid.css */
@font-face {
  font-family: 'Font Awesome 5 Free';
  font-style: normal;
  font-weight: 900;
  src: url('#{$fa-font-path}/fa-solid-900.eot');
  src: url('#{$fa-font-path}/fa-solid-900.eot?#iefix') format('embedded-opentype'),
  url('#{$fa-font-path}/fa-solid-900.woff2') format('woff2'),
  url('#{$fa-font-path}/fa-solid-900.woff') format('woff'),
  url('#{$fa-font-path}/fa-solid-900.ttf') format('truetype'),
  url('#{$fa-font-path}/fa-solid-900.svg#fontawesome') format('svg');
}

.fa,
.fas {
  font-family: 'Font Awesome 5 Free';
  font-weight: 900;
}

可以看出 font-face 中的 font-family 確實一模一樣,即可以表示當有元素使用到這個字體的時候,瀏覽器有能力識別出需要用的是哪個'font'。

google 之后在 StackOverflow 找到了一個相關的問答(Stack Overflow 大法好)How to use font-family with same name?

咳咳。。敲黑板,劃重點??

When you describe a font with a name, imagine (in the most abstract of the explanations) that you create an object; but, when you create multiple font-rules with the same name, imagine you create an array. Now, to access and array, you have to use its index. The index here is the font-weight. So, to access different weights (technically, fonts), you use the weight. Continuing the analogy of the array above, you have to manually define the index, it's not automatically done.

原來乳此!如果出現(xiàn)了同樣的字體,就是用 font-weight 這個屬性來判斷要用哪一個!這時候再回頭去看上面的 font awesome的css 定義,可以看出確實兩個字體使用了不同的 font-weight 。一切都'水落石出'了。

不能聽風就是雨

雖然上面回答說得很有道理,并且也符合我們觀察到的現(xiàn)象,但是我們不能聽風就是雨是不?!下定論要有證據(jù)!既然別人能說出來,必然我們能找到對應這塊的規(guī)范標準。

果不其然,在另外一個問答中看到,font-weightfont-style 屬性都可以影響字體選擇行為,并且出現(xiàn)了引領我們走向'真理'的評論:CSS Fonts Module。作為描述字體的屬性有三個

  • font-weight
  • font-style
  • font-stretch

These descriptors define the characteristics of a font face and are used in the process of matching styles to specific faces. For a font family defined with several @font-face rules, user agents can either download all faces in the family or use these descriptors to selectively download font faces that match actual styles used in document. The values for these descriptors are the same as those for the corresponding font properties except that relative keywords are not allowed, ‘bolder’ and ‘lighter’. If these descriptors are omitted, initial values are assumed.

目前為止可以總結出幾點:

  1. 可以使用 @font-face 引入外部字字體
  2. 使用 @font-face 引入相同名字的字體,可以通過設置 font-weightfont-style、font-stretch 屬性控制瀏覽器根據(jù)使用樣式命中不同的字體文件
  3. font-weight 設置為 normal 的定義必須放在最前面,經過在 chrome 上測試,并沒有這個要求。但是在 Stack Overflow 上很多人都提出有這個限制,可能是舊瀏覽器的行為,需要注意一下兼容性。

參考連接

  1. How to use font-family with same name?
  2. How to add multiple font files for the same font?
  3. Style Linking
  4. CSS Fonts Module Level 3
  5. element2-fontawesome5

版權聲明:原創(chuàng)文章,如需轉載,請注明出處“本文首發(fā)于xlaoyu.info

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 我的個人博客 http://yedingding.com 會更新 Teahour.fm 的最新音頻文本,歡迎關注。...
    葉玎玎閱讀 1,135評論 0 2
  • 這篇文字里我會介紹50 個便于使用的 CSS2/CSS3 代碼片段給所有的WEB專業(yè)人員. 選擇IDE開發(fā)環(huán)境來存...
    JamHsiao_aaa4閱讀 1,572評論 0 3
  • 1.CSS3 邊框 border-radius CSS屬性用來設置邊框圓角。當使用一個半徑時確定一個圓形;當使用兩...
    garble閱讀 773評論 0 0
  • 今天這一章看的自己云里霧里的,試試能不能寫著寫著捋捋清楚吧。 解釋人性的三種理論分別是:基因決定論,心理決定論,環(huán)...
    X_Lady閱讀 596評論 2 1
  • 從世錦賽一路來的低迷,到最終的失利,這一屆的男籃確實是熱愛籃球以來看過的最差的一屆!最后一場對戰(zhàn)尼日利亞,阿聯(lián)真的...
    羲和與望舒閱讀 244評論 0 0

友情鏈接更多精彩內容