DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> WEB網站前端 >> 前端技巧
面試常見 CSS 題一次搞懂:選擇器、繼承、優先級與 !important
編輯:前端技巧     

僅以此篇緬懷那些筆試寫過一百次、面試被問過一百次的 CSS 題目。這些問題看起來瑣碎,其實背後藏著理解 CSS 運作方式所需的關鍵概念。

經典四連問

CSS 選擇器有哪些?哪些屬性可以繼承?優先級如何計算?內聯樣式和 !important 誰優先?

一、常見選擇器

名稱寫法說明
通配選擇器*選取頁面上所有元素。*{font-size:12px;margin:0;padding:0;}
型別選擇器div、p、span以標籤名稱作為選擇條件。div{width:10px;height:10px;}
群組選擇器,對多組物件同時套用相同樣式。a:link, a:visited{color:#fff;}
後代選擇器空白選取某元素內的所有後代。#header p{width:100px;}
ID 選擇器#具有唯一性,同一頁面不可重複。#header{width:300px;}
類別選擇器.可在頁面中重複使用。.title{width:300px;}
IE hack_、*、\0、\9用於相容不同瀏覽器。.title{_width:50px;*height:30px;}

除了上表,現代 CSS 還有屬性選擇器([type="text"])、子元素選擇器(>)、相鄰兄弟選擇器(+)、通用兄弟選擇器(~),以及 :hover、:nth-child() 等偽類與 ::before、::after 等偽元素。面試時能主動補充這些,會比只背七種更有說服力。

二、哪些屬性可以繼承

可以繼承的屬性大致可分為字體、文字、列表與可見性四大類:

azimuth, border-collapse, border-spacing, caption-side,
color, cursor, direction, elevation, empty-cells,
font-family, font-size, font-style, font-variant, font-weight, font,
letter-spacing, line-height,
list-style-image, list-style-position, list-style-type, list-style,
orphans, pitch-range, pitch, quotes, richness,
speak-header, speak-numeral, speak-punctuation, speak, speech-rate,
stress,
text-align, text-indent, text-transform,
visibility, voice-family, volume,
widows, whitespace, word-spacing

反之,盒模型相關的屬性一律不繼承:margin、padding、width、height、border、background 都不會自動傳給子元素。這是面試的加分回答。

還有一個實用補充:所有屬性都能透過 inherit、initial、unset 這三個通用關鍵字強制繼承或重置。

三、優先級的四大原則

原則一:繼承的敗給指定的

只要元素自己有明確的宣告,就一定勝過從祖先繼承而來的值。

<style>
* { font-size: 20px }
.class3 { font-size: 12px; }
</style>
<p><span class="class3">我是多大字號?</span></p>
<!-- 結果:12px -->

即使層層嵌套,指定值永遠贏過繼承值:

<style>
#id1 #id2 { font-size: 20px }
.class3   { font-size: 12px }
</style>
<div id="id1" class="class1">
  <p id="id2" class="class2">
    <span id="id3" class="class3">我是多大字號?</span>
  </p>
</div>
<!-- 結果:12px。因為 .class3 直接作用在 span 上,
     而 #id1 #id2 的值是「繼承」下來的 -->

原則二:#ID > .class > 標籤

<style>
#id3   { font-size: 25px; }
.class3{ font-size: 18px; }
span   { font-size: 12px }
</style>
<p><span id="id3" class="class3">我是多大字號?</span></p>
<!-- 結果:25px -->

原則三:越具體越優先

選擇器寫得越長、越明確,權重越高。

<style>
.class1 .class2 .class3 { font-size: 25px; }
.class2 .class3        { font-size: 18px; }
.class3                { font-size: 12px; }
</style>
<div class="class1">
  <p class="class2"><span class="class3">我是多大字號?</span></p>
</div>
<!-- 結果:25px -->

原則四:標籤#ID > 標籤.class

<style>
span#id3    { font-size: 18px }
#id3        { font-size: 12px }
span.class3 { font-size: 18px }
.class3     { font-size: 12px }
</style>
<p>
  <span id="id3">我是多大字號?</span>
  <span class="class3">我是多大字號?</span>
</p>
<!-- 結果:18px 與 18px -->

衝突時的優先順序

原則一 > 原則二 > 原則三 > 原則四。

四、正式的特權數字計算

面試若追問「具體怎麼算」,標準答案是把選擇器拆成四組計數:

(行內樣式, ID 數量, 類別/偽類/屬性數量, 標籤數量)

*                         → 0,0,0,0
div                       → 0,0,0,1
.class                    → 0,0,1,0
#id                       → 0,1,0,0
div.class                 → 0,0,1,1
ul li a.link              → 0,0,1,3
.nav ul li a:hover        → 0,0,2,3
<div style="...">         → 1,0,0,0

從左至右逐位比較,較大者勝,不進位。這也解釋了為什麼「11 個 class 選不贏 1 個 ID」。

五、IE6 到底認不認 !important?

答:認識,但有一個 bug。

正常情況:

<style>
#ida    { font-size: 18px }
.classb { font-size: 12px; }
</style>
<div id="ida" class="classb">!important 測試:18px</div>

加上 !important 後:

<style>
#ida    { font-size: 18px }
.classb { font-size: 12px !important; }
</style>
<div id="ida" class="classb">!important 測試:12px</div>

這裡 IE6 行為正常。但接著看 bug:

<style>
.classb { font-size: 18px !important; font-size: 12px }
</style>
<div class="classb">!important 測試:12px</div>

在 IE6 中顯示 12px,其他瀏覽器顯示 18px。原因與解法:IE6 在同樣式區塊內只會取最後一條宣告,無視 !important。因此只要把 !important 那條放在後面即可:

.classb { font-size: 12px; font-size: 18px !important; }

這樣 IE6 與其他瀏覽器都會顯示 18px。

六、順帶釐清兩個常見誤解

誤解一:內聯樣式的優先級最高。

不對。!important 的宣告永遠勝過內聯樣式(除非內聯樣式自己也帶 !important)。正確順序是:

!important 使用者樣式
> !important 作者樣式
> 行內樣式(作者)
> ID / class / 標籤(依特權數字)
> 瀏覽器預設樣式

誤解二:後寫的一定贏。

只有在特權數字完全相同時,後寫的才會勝出。這就是所謂的「Source Order」規則,它是最後的決勝關鍵,而不是首要依據。

面試官真正想聽什麼

這些題目背下來不難,但更有價值的回答是加上實務觀點,例如:

  • 為什麼要避免 !important?因為它破壞了樣式的可預測性,讓後續維護時難以覆寫,形成「!important 軍備競賽」。
  • 為什麼要控制選擇器的具體度?CSS 有 Specificity War 的問題,建議保持選擇器扁平、淺層,這也是 BEM、CSS Modules、CSS-in-JS 等方案試圖解決的核心痛點。
  • 需要局部重置時,優先考慮 :where()(特權為 0)而非硬寫 !important。

能把「記憶題」連上「為什麼這樣設計」,才是面試官真正想聽的答案。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved