:nth-child() 是 CSS 中最強大的結構性偽類之一,能精確選中父元素下的特定子元素。本文提供完整語法解析、常用模式速查表、視覺化演示與實戰應用模式。
:nth-child(An + B)
/* A = 步長 (間隔),B = 偏移量 (起始位置),n = 0,1,2,3... 自然數 */
/* 瀏覽器計算:n=0 → B, n=1 → A+B, n=2 → 2A+B ... 只保留 >0 的整數索引 */
| 需求 | 選擇器 | 選中索引 (1-based) | 視覺示意 |
|---|---|---|---|
| 第 1 個 | :nth-child(1) / :first-child | 1 | 🟢⚪⚪⚪⚪ |
| 第 5 個 | :nth-child(5) | 5 | ⚪⚪⚪⚪🟢 |
| 最後 1 個 | :last-child | 最後 | ⚪⚪⚪⚪🟢 |
| 倒數第 2 個 | :nth-last-child(2) | 倒數 2 | ⚪⚪⚪🟢⚪ |
| 所有奇數行 | :nth-child(odd) / :nth-child(2n+1) | 1,3,5,7... | 🟢⚪🟢⚪🟢 |
| 所有偶數行 | :nth-child(even) / :nth-child(2n) | 2,4,6,8... | ⚪🟢⚪🟢⚪ |
| 前 5 個 | :nth-child(-n+5) | 1,2,3,4,5 | 🟢🟢🟢🟢🟢⚪... |
| 第 6 個及之後 | :nth-child(n+6) | 6,7,8... | ⚪⚪⚪⚪⚪🟢🟢... |
| 第 2-5 個 | :nth-child(n+2):nth-child(-n+5) | 2,3,4,5 | ⚪🟢🟢🟢🟢⚪... |
| 每 3 個第 1 個 | :nth-child(3n+1) | 1,4,7,10... | 🟢⚪⚪🟢⚪⚪🟢 |
| 每 4 個第 2 個 | :nth-child(4n+2) | 2,6,10,14... | ⚪🟢⚪⚪⚪🟢... |
| 每 3 個最後 1 個 | :nth-child(3n) | 3,6,9... | ⚪⚪🟢⚪⚪🟢... |
| 前 3 個 & 每 5 個後 1 個 | :nth-child(-n+3), :nth-child(5n+4) | 1,2,3,9,14... | 🟢🟢🟢⚪⚪⚪⚪⚪🟢... |
tbody tr:nth-child(odd) { background: #f9f9f9; }
tbody tr:nth-child(even) { background: #fff; }
/* 或現代寫法 */
tbody tr { background: #fff; }
tbody tr:nth-child(odd) { background: #f9f9f9; }
.grid-item {
float: left;
width: 23%;
margin-left: 2.66%;
}
.grid-item:nth-child(4n+1) { margin-left: 0; clear: left; }
/* Flex/Grid 時代無需此 hack */
.grid-modern { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2.66%; }
.list-item { border-bottom: 1px solid #eee; }
.list-item:nth-child(5n) { border-bottom: 2px solid #ddd; } /* 組分隔更粗 */
.list-item:last-child { border-bottom: none; }
.stagger-item {
opacity: 0;
transform: translateY(20px);
animation: fadeInUp 0.5s ease-out forwards;
}
.stagger-item:nth-child(n) { animation-delay: calc(var(--index, 0) * 0.1s); }
/* 配合 JS 設置 --index 或純 CSS :nth-child 計算 */
@keyframes fadeInUp {
to { opacity: 1; transform: translateY(0); }
}
/* 純 CSS 交錯 (無需 JS 變量) */
.stagger-item:nth-child(1) { animation-delay: 0s; }
.stagger-item:nth-child(2) { animation-delay: 0.1s; }
.stagger-item:nth-child(3) { animation-delay: 0.2s; }
/* ... Sass/PostCSS 循環生成 */
.form-field.invalid:nth-child(-n+3) {
border-color: #dc3545;
}
.form-field.invalid:nth-child(n+4) {
border-color: #ffc107; /* 後續警告色 */
}
<div class="container">
<h2>標題</h2> <!-- 子元素 1 -->
<p>段落 1</p> <!-- 子元素 2 -->
<p>段落 2</p> <!-- 子元素 3 -->
<span>標籤</span> <!-- 子元素 4 -->
<p>段落 3</p> <!-- 子元素 5 -->
</div>
| 選擇器 | 匹配元素 | 索引計算基準 |
|---|---|---|
p:nth-child(2) | ❌ 無 (第 2 個是 h2) | 所有子元素 |
p:nth-child(3) | ✅ 段落 1 | 所有子元素 |
p:nth-of-type(1) | ✅ 段落 1 | **僅 p 元素** |
p:nth-of-type(2) | ✅ 段落 2 | **僅 p 元素** |
建議:語義化結構中優先用 :nth-of-type 針對特定標籤;混合結構需精確控制位置用 :nth-child。
:nth-child(), :nth-last-child(), :nth-of-type(), :nth-last-of-type()browserslist 目標瀏覽器自動處理// 在 Console 執行,高亮所有匹配 :nth-child(3n+1) 的元素
$$(':nth-child(3n+1)').forEach(el => {
el.style.outline = '2px solid red';
console.log(el.tagName, el.textContent.trim().slice(0, 30));
});
// Sass 生成前 10 項交錯動畫延遲
@for $i from 1 through 10 {
.stagger-item:nth-child(#{$i}) {
animation-delay: #{$i * 0.1}s;
}
}
// PostCSS (postcss-for) 生成網格清除 margin
@for $i from 1 to 20 {
.grid-item:nth-child(4n+1) { margin-left: 0; clear: left; }
}