溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

前端雜談: CSS 權(quán)重 (Specificity)

發(fā)布時間:2020-08-10 19:39:04 來源:ITPUB博客 閱讀:179 作者:Tybyq 欄目:大數(shù)據(jù)

前端雜談: CSS 權(quán)重 (Specificity)

css 權(quán)重 想必大家都聽說過, 一些簡單的規(guī)則大部分人也都知道:

  • 較長的 css selector 權(quán)重會大于較短的 css selector

  • id selector 權(quán)重高于 class selector.

但是 具體規(guī)范 是什么? 瀏覽器是按照什么標準來判定不同選擇器的權(quán)重的呢?

讓我們來看一下 官方文檔 是怎么說的~

第一個關(guān)鍵詞:  Specificity

Specificity  is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of  CSS selectors

官方文檔中用  Specificity: 特異性  來表示一個 css selector 和其元素的相關(guān)性. 相關(guān)性越強, 即表示表示其權(quán)重最高.

那么問題來了,  Specificity  是如何被比較的呢?

Specificity is a weight that is applied to a given CSS declaration, determined by the number of each  selector type  in the matching selector.

Specificity 是由 selector 中 不同  selector type  的數(shù)目決定的.

第二個關(guān)鍵詞:  Selector Type

根據(jù)  W3 標準 中的規(guī)定, css selector 分為 4 種 type:  a, b, c, d . 他們的數(shù)目計算規(guī)則為:

  • a:  如果 css 屬性聲明是寫在  style=“”  中的, 則數(shù)目為 1, 否則為 0

  • b:  id 選擇器的數(shù)目

  • c:  class 選擇器, 屬性選擇器(如  type=“text” ), 偽類選擇器(如:  ::hover ) 的數(shù)目

  • d:  標簽名(如:  p div ), 偽類 (如:  :before )的數(shù)目

在比較不同 css selector 的權(quán)重時, 按照  a => b => c => d  的順序進行比較.

由第一個 selector type a 的計算規(guī)則可知: 寫在 html 代碼 style 屬性中的 css 相較寫在 css 選擇器中的 css 屬性具有最高的優(yōu)先級.

而 id selector (b) 相較 class selector (c) 有更高的優(yōu)先級. 這也和我們平時的經(jīng)驗相吻合.

還有一些 css 選擇器你沒提, 它們該怎么計算權(quán)重?

除了上面 Specificity 計算規(guī)則中的 css 選擇器類型, 還有一些選擇器如:  * + > , :not()  等. 這些選擇器該如何計算其權(quán)重呢?

答案是這些選擇器并不會被計算到 css 的權(quán)重當中 :)

有一個需要特別注意一下的選擇器:  :not() , 雖然它本身是不計權(quán)重的, 但是寫在它里面的 css selector 是需要計算權(quán)重的.

如果  a,b,c,d  算完都一樣, 怎么辦?

默認行為是: 當 specificity 一樣時, 最后聲明的 css selector 會生效.

如果我重復同樣的 css selectory type, 權(quán)重會增加嗎?

讓我們來做個實驗, 我們聲明一個 html 節(jié)點:

<div>
  <div id="testId" class="testClass"><span>test div</span></div></div>

在 css 中我們添加兩個選擇器:

.testClass.testClass {  background-color: red;
}.testClass {  background-color: black;
}

如果重復的 css selector 會被忽略的話, 按照前面的規(guī)則, 最后聲明的 css selector 會生效, 所以 這個 div 節(jié)點背景色應該是黑色. 讓我們看看結(jié)果:

前端雜談: CSS 權(quán)重 (Specificity)

結(jié)果我們得到的是一個紅色的 div, 也就是說  .testClass.testClass  高于  .testClass . 所以結(jié)論是: 重復的 css selector, 其權(quán)重會被重復計算.

關(guān)于  !important :

按照  MDN 的說法,  !important  是不在 css 選擇器的權(quán)重計算范圍內(nèi)的, 而它之所以能讓 css 選擇器生效是因為瀏覽器在遇見  !important  時會進行特殊的判斷. 當多個  !important  需要進行比較時, 才會計算其權(quán)重再進行比較.

通常來說, 不提倡使用  !important . 如果你認為一定要使用, 不妨先自檢一下:

  • 總是 先考慮使用權(quán)重更高的 css 選擇器, 而不是使用  !important

  • 只有 當你的目的是覆蓋來自第三方的 css(如: Bootstrap, normalize.css)時, 才在頁面范圍使用  !important

  • 永遠不要  在你寫一個第三方插件時使用  !important

  • 永遠不要 在全站范圍使用  !important

一些誤導的信息

我在搜索關(guān)于 css 權(quán)重的資料時, 看到了下面這張圖, 看似十分有道理, 但其實是 錯誤的! 前端雜談: CSS 權(quán)重 (Specificity)

讓我們來做一個簡單的測試:

按照圖片中的計算公式: 如果一個 css 選擇器包含 11 個 class selector type , 另一個選擇器是 1 個 id selector type . 那么 class 選擇器的權(quán)重會高于 id 選擇器的權(quán)重. 讓我們來試一試:

.testClass.testClass.testClass.testClass.testClass.testClass
  .testClass.testClass.testClass.testClass.testClass {  background-color: red;
}#testId {  background-color: black;
}

讓我們看看結(jié)果:

前端雜談: CSS 權(quán)重 (Specificity)

結(jié)果顯示還是  id 選擇器 權(quán)重更高.

雖然我們在實際編碼過程中很少會出現(xiàn) 10 多個 class 的情況, 但萬一出現(xiàn)了, 知道權(quán)重真正的 計算 比較 規(guī)則, 我們才能正確的處理~


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI