當多個 HTML 元素需要套用相同樣式時,群組選擇器(Grouping Selector)讓您只需撰寫一次樣式規則,即可同時套用至所有目標元素。這不僅能大幅減少代碼量,更能確保視覺一致性、降低維護成本。
使用半形逗號 , 分隔多個選擇器,共用同一組宣告區塊:
/* 群組選擇器語法 */
選擇器1, 選擇器2, 選擇器3 {
共同樣式屬性: 值;
}
/* ❌ 重複寫法 - 難以維護 */
h1 { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
h2 { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
h3 { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
h4 { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
h5 { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
h6 { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
p { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
span { font-size: 2rem; color: #1a1a1a; font-weight: 700; font-family: 'Microsoft JhengHei', sans-serif; margin-bottom: 1rem; }
/* ✅ 群組選擇器 - 精簡易維護 */
h1, h2, h3, h4, h5, h6, p, span {
font-size: 2rem;
color: #1a1a1a;
font-weight: 700;
font-family: 'Microsoft JhengHei', sans-serif;
margin-bottom: 1rem;
}
/* 表單相關元素統一字體大小 */
input, textarea, select, button {
font-family: inherit;
font-size: 1rem;
line-height: 1.5;
}
/* 互動元素統一焦點樣式 */
a:focus, button:focus, input:focus, textarea:focus, select:focus,
[tabindex]:focus {
outline: 2px solid #0066cc;
outline-offset: 2px;
}
/* 文字排版通用類別 */
.text-primary, .heading-primary, .btn-primary {
color: var(--color-primary);
}
/* 佈局容器共同內距 */
.container, .wrapper, .section-inner, .card__body {
padding-left: 1.5rem;
padding-right: 1.5rem;
}
, 會導致選擇器失效h1, h2, h3.btn-primary,
.btn-secondary,
.btn-outline,
.link-button {
padding: 0.5rem 1rem;
border-radius: 0.375rem;
font-weight: 500;
transition: all 0.2s ease;
}
| 特性 | 群組選擇器 | CSS 繼承 |
|---|---|---|
| 適用屬性 | 所有屬性 | 僅可繼承屬性(color, font-*, line-height 等) |
| 精確控制 | 明確指定哪些元素套用 | 依賴 DOM 樹結構,可能影響非預期後代 |
| 維護性 | 需維護選擇器清單 | 自動生效,但難以追蹤來源 |
| 效能 | 瀏覽器需匹配多個選擇器 | 自然瀏覽器機制,無額外成本 |
建議策略:全域基礎樣式(字體、色彩、行高)用繼承(設在 body 或 :root);元件層級的共同視覺特徵用群組選擇器。
/* ❌ 錯誤:最後一個選擇器漏寫逗號,導致後續規則被視為同一選擇器一部分 */
h1, h2, h3
h4 { color: red; } /* 實際上是 h3 h4 { color: red; } */
/* ❌ 錯誤:群組選擇器中單一選擇器無效,整組皆失效 */
h1, ::invalid-pseudo, h2 { color: red; } /* 全部失效 */
/* ✅ 正確:分開寫或確保每個選擇器皆有效 */
h1, h2 { color: red; }
::invalid-pseudo { color: red; } /* 獨立規則,不影響其他 */
除錯技巧:瀏覽器開發者工具「Styles」面板會顯示群組選擇器完整列表,點擊可跳轉至原始碼對應行。
CSS Selectors Level 4 引入更強大的群組功能:
/* :is() - 特定度取最高者,適合元件內部簡化 */
.card :is(h1, h2, h3, .title) { color: var(--text-heading); }
/* :where() - 特定度永遠為 0,適合重置/基礎樣式,極易覆蓋 */
:where(ul, ol) :where(li) { padding-left: 1.5rem; }
/* 實際差異示範 */
:is(#header, .sidebar) a { color: red; } /* 特定度 = ID (100) 或 class (10) 取最大 */
:where(#header, .sidebar) a { color: red; } /* 特定度 = 0 (元素選擇器 a = 1) */
群組選擇器是 CSS 中最基礎也最實用的「不要重複自己」工具。掌握正確語法(半形逗號、換行排版)、理解與繼承的分工、善用現代 :is()/:where() 偽類,能讓樣式表更精簡、更易維護、更具擴展性。建立專案共用的「基礎樣式群組」(如標題、表單、互動元素)是每個專案啟動時的高回報投資。