萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 選擇器完整參考手冊:從基礎到 Level 4、優先級計算、實戰模式與性能避坑

選擇器分級標準(W3C Selectors Level 1-4)

Level 1 (CSS1) — 基礎

  • * 通用選擇器
  • E 類型選擇器
  • .class 類選擇器
  • #id ID 選擇器
  • E F 後代選擇器
  • E:link / :visited / :active / :hover 偽類
  • E:first-line / :first-letter 偽元素

Level 2 (CSS2) — 結構與屬性

  • E > F 子選擇器
  • E + F 相鄰兄弟選擇器
  • E[attr] E[attr="val"] E[attr~="val"] E[attr|="val"] 屬性選擇器
  • E:first-child 結構偽類
  • E:lang(c) 語言偽類
  • E::before / ::after 偽元素(雙冒號語法 Level 3 規範)

Level 3 (Selectors Level 3) — 豐富偽類

  • 結構::nth-child(n) :nth-last-child(n) :nth-of-type(n) :first-of-type :last-of-type :only-child :only-of-type :empty :root
  • 表單::enabled / :disabled / :checked / :indeterminate :valid / :invalid / :in-range / :out-of-range :required / :optional :read-only / :read-write :placeholder-shown :default
  • 目標::target
  • 否定::not(X) (單參數)
  • 兄弟:E ~ F 通用兄弟選擇器
  • 屬性擴展:E[attr^="val"] (開頭) E[attr$="val"] (結尾) E[attr*="val"] (包含)

Level 4 (Selectors Level 4 / 現代草案) — 強大邏輯

  • :not() 多參數/複選器列表: :not(.a, .b, [hidden])
  • :has() 父選擇器/關係選擇器: div:has(> img)、label:has(+ input:checked)、**改變 CSS 單向流向的里程碑**
  • :is() / :where() 選擇器列表分組: :is(h1,h2,h3) > a、:where(.a, .b) .c (:where 優先級為 0)
  • :focus-visible / :focus-within:鍵盤聚焦可見、祖先聚焦檢測
  • :nth-child(n of selector):篩選後再取第 n 個 li:nth-child(2 of .active)
  • :dir(ltr/rtl) / :lang() 增強
  • :scope:當前作用域根元素(JS element.querySelector(':scope > .child'))
  • :state() / :user-valid 等表單新偽類(草案中)

優先級計算系統(Specificity)

/* 四元組 (A, B, C, D) — 十進制比較,非進位制 */
內聯 style=""           → (1,0,0,0)  = 1000
ID 選擇器 #id           → (0,1,0,0)  =  100
類/屬性/偽類 .class     → (0,0,1,0)  =   10
標簽/偽元素 div ::before→ (0,0,0,1)  =    1
通用 * / 組合器 > + ~   → (0,0,0,0)  =    0

/* 實例 */
#nav .item a:hover      → (0,1,2,1) = 121
div.content p           → (0,0,0,2) =   2
:where(.a, #b) .c       → (0,0,1,0) =  10  (:where 優先級歸零)

實戰選擇器模式庫

1. 狀態驅動樣式 — 屬性選擇器 + :has()

/* 表單驗證狀態 */
.field:has(input:invalid) .field__error { display: block; }
.field:has(input:valid) .field__success { display: block; }

/* 容器根據子元素狀態變化 */
.card:has(.badge--new) { border-color: #007bff; }
.card:has(img) { padding: 0; } /* 有圖片時移除內邊距 */

/* 導航當前項父級高亮 */
.nav__item:has(.nav__link[aria-current="page"]) { background: #f0f7ff; }

2. 微調優先級 — :where() 降權、:is() 提權

/* 重置類:極低優先級,易被覆蓋 */
:where(ul, ol) { list-style: none; padding: 0; margin: 0; }

/* 工具類:高優先級,強制生效 */
:is(.u-hidden, [hidden]) { display: none !important; }

/* 組件內部:限制作用域 */
.card :is(.title, .heading) { font-size: 1.25rem; }

3. 數量查詢 — 響應式佈局無 JS

/* 列表項 ≥ 5 時改為網格 */
.grid:has(> :nth-child(5)) { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }

/* 精確 3 項時特殊佈局 */
.items:has(> :nth-child(3):last-child) { display: flex; justify-content: space-between; }

4. 表單 UX 增強

/* 必填標記自動生成 */
label:has(+ input[required])::after { content: " *"; color: #ef4444; }

/* 聚焦時顯示幫助文本 */
.field:focus-within .field__hint { opacity: 1; visibility: visible; }

/* 輸入時實時驗證反饋 */
input:user-invalid { border-color: #ef4444; }
input:user-valid { border-color: #22c55e; }

性能避坑指南

反模式開銷優化
.container div span a 深層後代向上遍歷 DOM 樹,O(N×深度)直接類選擇器 .link
* { } 通用選擇器作關鍵選擇符匹配所有元素避免作為關鍵選擇符(最右側)
div > * + * + * 複雜組合器瀏覽器匹配引擎回溯次數激增拆分為語義類
屬性選擇器 [class*="col-"]字符串匹配慢於哈希查找改用明確類名
:has() 濫用於高頻重繪容器每次佈局觸發父鏈檢查限制範圍、配合 contain:layout

瀏覽器支持現狀 (2024)

  • :has():Chrome 105+, Edge 105+, Safari 15.4+, Firefox 121+ (完整支持)
  • :is() / :where():全主流瀏覽器支持
  • :focus-visible:全主流支持
  • :nth-child(n of S):Chrome 111+, Safari 15.4+, Firefox 113+
  • 屬性選擇器 ^= $= *=:IE7+ 全支持

選擇器是 CSS 的「查詢語言」。掌握 Level 4 的邏輯能力,配合優先級科學管理,能讓樣式表從「堆砌代碼」進化為「可推理的設計系統」。

copyright © 萬盛學電腦網 all rights reserved