:nth-last-child()
Baseline 广泛可用 *
试一试
p {
font-weight: bold;
}
li:nth-last-child(-n + 3) {
border: 2px solid orange;
margin-top: 1px;
}
li:nth-last-child(even) {
background-color: lightyellow;
}
<p>Eight deadliest wildfires:</p>
<ol reversed>
<li>Matheson Fire</li>
<li>Miramichi Fire</li>
<li>1997 Indonesian fires</li>
<li>Thumb Fire</li>
<li>Great Hinckley Fire</li>
<li>Cloquet Fire</li>
<li>Kursha-2 Fire</li>
<li>Peshtigo Fire</li>
</ol>
语法
:nth-last-child(<nth> [of <complex-selector-list>]?) {
/* ... */
}
参数
:nth-last-child() 伪类指定一个参数,该参数表示匹配元素的模式,从末尾开始计数。
关键字值
函数式表示法
<An+B>-
表示在同级元素系列中,数值位置匹配
An+B模式的元素,其中n为所有正整数或零值,并且A是一个整数步长,B是一个整数偏移量,n是所有非负整数,从 0 开始。
它可以理解为列表中的第
An+B个元素。从末尾开始计数的第一个元素的索引是1。A和B都必须是<integer>值。
of <selector> 语法
通过传递选择器参数,我们可以选择匹配该选择器的倒数第 n 个元素。例如,以下选择器匹配最后三个带 class="important" 的重要列表项。
:nth-last-child(-n + 3 of li.important) {
}
注意:这与将选择器移到函数外部不同,例如
li.important:nth-last-child(-n + 3) {
}
此选择器将样式应用于列表项,如果它们也在最后三个子元素中。
示例
选择器示例
tr:nth-last-child(odd)或tr:nth-last-child(2n+1)-
表示 HTML 表格的奇数行:1、3、5 等,从末尾开始计数。
tr:nth-last-child(even)或tr:nth-last-child(2n)-
表示 HTML 表格的偶数行:2、4、6 等,从末尾开始计数。
:nth-last-child(7)-
表示第七个元素,从末尾开始计数。
:nth-last-child(5n)-
表示第 5、10、15 等元素,从末尾开始计数。
:nth-last-child(3n+4)-
表示第 4、7、10、13 等元素,从末尾开始计数。
:nth-last-child(-n+3)-
表示同级元素组中的最后三个元素。
p:nth-last-child(n)或p:nth-last-child(n+1)-
表示同级元素组中的每个
<p>元素。这与简单的p选择器相同。(由于n从零开始,而最后一个元素从一开始,所以n和n+1都将选择相同的元素。) p:nth-last-child(1)或p:nth-last-child(0n+1)-
表示同级元素组中,从末尾开始计数的每个
<p>元素。这与:last-child选择器相同。
表格示例
HTML
<table>
<tbody>
<tr>
<td>First line</td>
</tr>
<tr>
<td>Second line</td>
</tr>
<tr>
<td>Third line</td>
</tr>
<tr>
<td>Fourth line</td>
</tr>
<tr>
<td>Fifth line</td>
</tr>
</tbody>
</table>
CSS
table {
border: 1px solid blue;
}
/* Selects the last three elements */
tr:nth-last-child(-n + 3) {
background-color: pink;
}
/* Selects every element starting from the second to last item */
tr:nth-last-child(n + 2) {
color: blue;
}
/* Select only the last second element */
tr:nth-last-child(2) {
font-weight: 600;
}
结果
数量查询
数量查询根据元素的数量来设置样式。在此示例中,当列表中至少有三个列表项时,它们会变为红色。这是通过结合 nth-last-child 伪类和 后续兄弟组合器 的功能来实现的。
HTML
<h4>A list of four items (styled):</h4>
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ol>
<h4>A list of two items (unstyled):</h4>
<ol>
<li>One</li>
<li>Two</li>
</ol>
CSS
/* If there are at least three list items,
style them all */
li:nth-last-child(n + 3),
li:nth-last-child(3) ~ li {
color: red;
}
结果
of <selector> 语法示例
在此示例中,有一个无序的姓名列表。某些项目应用了 noted 类,然后用粗底边框突出显示。
HTML
<ul>
<li class="noted">Diego</li>
<li>Shilpa</li>
<li class="noted">Caterina</li>
<li>Jayla</li>
<li>Tyrone</li>
<li>Ricardo</li>
<li class="noted">Gila</li>
<li>Sienna</li>
<li>Titilayo</li>
<li class="noted">Lexi</li>
<li>Aylin</li>
<li>Leo</li>
<li>Leyla</li>
<li class="noted">Bruce</li>
<li>Aisha</li>
<li>Veronica</li>
<li class="noted">Kyouko</li>
<li>Shireen</li>
<li>Tanya</li>
<li class="noted">Marlene</li>
</ul>
CSS
* {
font-family: sans-serif;
}
ul {
display: flex;
flex-wrap: wrap;
list-style: none;
font-size: 1.2rem;
padding-left: 0;
}
li {
margin: 0.125rem;
padding: 0.25rem;
border: 1px solid tomato;
}
.noted {
border-bottom: 5px solid tomato;
}
在下面的 CSS 中,我们针对标记为 class="noted" 的奇数列表项。
li:nth-last-child(odd of .noted) {
background-color: tomato;
border-bottom-color: seagreen;
}
结果
带有 class="noted" 的项目具有粗底边框,并且项目 1、7、14 和 20 具有实心背景,因为它们是带有 class="noted" 的奇数列表项。
规范
| 规范 |
|---|
| 选择器 Level 4 # nth-last-child-pseudo |
浏览器兼容性
加载中…