* /* 通用選擇器,匹配所有元素 */
E /* 標籤選擇器 */
.warning /* 類選擇器 */
#main /* ID 選擇器 */
| 名稱 | 語法 | 含義 | 特異性 |
|---|---|---|---|
| 後代選擇器 | E F | E 內所有層級的 F | (0,0,1,0)×N |
| 子選擇器 | E > F | E 直接子元素 F | (0,0,1,0)×N |
| 相鄰兄弟 | E + F | 緊隨 E 之後的同級 F | (0,0,1,0)×N |
| 通用兄弟 | E ~ F | E 之後所有同級 F | (0,0,1,0)×N |
| 列選擇器 | E || F | 表格列關係 (Level 4) | (0,0,1,0) |
E[attr] /* 存在屬性 */
E[attr="val"] /* 精確匹配 */
E[attr~="val"] /* 空格分隔列表中包含 val */
E[attr|="val"] /* 連字符分隔列表以 val 開頭 (lang) */
E[attr^="val"] /* 值以 val 開頭 */
E[attr$="val"] /* 值以 val 結尾 */
E[attr*="val"] /* 值包含 val 任意位置 */
E[attr="val" i] /* 不區分大小寫 (i flag) */
E[attr="val" s] /* 區分大小寫 (s flag, 默認) */
:root /* 文檔根元素 */
:empty /* 無子節點(含文本) */
:first-child, :last-child
:nth-child(An+B) /* An+B 公式: 3n, 2n+1(奇), 2n(偶), -n+3(前3) */
:nth-last-child(An+B) /* 從後往前 */
:first-of-type /* 同類型首個 */
:last-of-type
:nth-of-type(An+B)
:nth-last-of-type(An+B)
:only-child /* 唯一子元素 */
:only-of-type /* 唯一同類型子元素 */
:enabled, :disabled /* 表單啟用/禁用 */
:checked /* radio/checkbox 選中 */
:indeterminate /* indeterminate 狀態 */
:focus, :focus-visible, :focus-within /* 聚焦狀態,:focus-visible 僅鍵盤 */
:required, :optional /* HTML5 required 屬性 */
:valid, :invalid /* 表單驗證狀態 */
:in-range, :out-of-range /* number/range 範圍 */
:placeholder-shown /* 顯示 placeholder 時 */
:read-only, :read-write /* readonly 屬性 */
:default /* 默認提交按鈕等 */
:link, :visited, :hover, :active /* LVHA 順序 */
:target /* URL hash 對應元素 */
:target-within /* 後代包含 :target (Level 4) */
:lang(zh) /* lang 屬性匹配 */
:dir(ltr), :dir(rtl) /* 書寫方向 (Level 4) */
::before, ::after /* 生成內容,需 content */
::first-line /* 首行 */
::first-letter /* 首字符 */
::marker /* 列表標記 */
::placeholder /* placeholder 文本 */
::selection /* 用戶選中區域 */
::file-selector-button /* input[type=file] 按鈕 */
::backdrop /* dialog/fullscreen 背景 */
::part(name) /* Shadow DOM 部件 */
::slotted(selector) /* Slot 分配節點 */
| 排名 | 類型 | 示例 | 特異性 | 備註 |
|---|---|---|---|---|
| 1 | ID | #id | (0,1,0,0) | 最快,唯一 |
| 2 | 類 | .class | (0,0,1,0) | 主力選擇器 |
| 3 | 標籤 | div, h1 | (0,0,0,1) | 基礎樣式 |
| 4 | 相鄰兄弟 | h1 + p | (0,0,1,0) | 較少用 |
| 5 | 子選擇器 | ul > li | (0,0,1,0) | 推薦替代後代 |
| 6 | 後代選擇器 | li a | (0,0,1,0) | 避免深層嵌套 |
| 7 | 通配符 | * | (0,0,0,0) | 僅限重置/調試 |
| 8 | 屬性選擇器 | [type="text"] | (0,0,1,0) | 效能較低 |
| 9 | 偽類/偽元素 | :hover, ::before | (0,0,1,0)/(0,0,0,1) | 動態匹配開銷大 |
核心原則:瀏覽器從右向左匹配。關鍵選擇器(最右側)決定初始匹配集合大小,ID/類最小,標籤/通用/屬性/偽類最大。
.btn--primary ✅ | button.btn--primary ❌ | .card .btn ⚠️ul.nav → .nav;button#submit → #submit.header .nav .item .link → .nav__link (BEM)[data-modal="open"] → .is-open:where(.header, .footer) .link { color: inherit; } 覆蓋極易:is(h1,h2,h3) .highlight { color: red; }/* 1. :has() 父選擇器 - 革命性 */
.card:has(img) { padding: 0; } /* 包含圖片的卡片無內邊距 */
.form:has(:invalid) .submit-btn { opacity: 0.5; } /* 表單無效時禁用提交 */
nav:not(:has(.active)) .indicator { display: none; } /* 無激活項隱藏指示器 */
/* 2. :is() / :where() 分組 */
:is(h1,h2,h3,h4,h5,h6) { margin-block: 1em 0.5em; }
:where(.sidebar, .footer) a { color: inherit; } /* 特異性歸零 */
/* 3. :not() 複雜參數 */
:not(.hidden, .disabled, [aria-hidden]) { opacity: 1; }
/* 4. :nth-child 選擇器列表 */
li:nth-child(odd of .item) { background: #f5f5f5; } /* 只算 .item 的奇偶 */
/* 5. 邏輯組合 */
:is(:hover, :focus-visible) { outline: none; box-shadow: 0 0 0 2px var(--color-primary); }
// .stylelintrc.js
module.exports = {
rules: {
'selector-class-pattern': '^[a-z][a-z0-9]*(-[a-z0-9]+)*(__[a-z0-9]+(-[a-z0-9]+)*)?(--[a-z0-9]+(-[a-z0-9]+)*)?$',
'selector-id-pattern': '^[a-z][a-z0-9]*(-[a-z0-9]+)*$',
'selector-max-compound-selectors': 3, // 限制複合選擇器長度
'selector-max-id': 0, // 禁止 CSS 中使用 ID 選擇器
'selector-max-universal': 0, // 禁止通用選擇器 (除重置文件)
'selector-pseudo-class-no-unknown': [true, { ignorePseudoClasses: ['global', 'local', 'deep'] }],
'selector-pseudo-element-no-unknown': true,
}
};
tag#id、tag.class 模式,批量移除標籤限定:where() 降低重置/基礎樣式特異性:has() 實現狀態聯動,減少 JS 類名切換選擇器是 CSS 的「查詢語言」。掌握現代選擇器特性(特別是 :has()、:where()、:is()),配合 BEM/Utility-First 命名與工程化治理,能讓樣式系統保持低特異性、高復用性、強可維護性。