CSS :last-child 选择器
实例
指定属于其父元素的最后一个子元素的 p 元素的背景色:
<!DOCTYPE html><html><head><style>p:last-child {background: #ff0000;}</style></head><body><p>第1个段落。</p><p>第2个段落。</p><p>第3个段落。</p><p>第4个段落。</p></body></html>
浏览器支持
表格中的数字注明了完全支持该属性的首个浏览器版本。
| 选择器 | |||||
|---|---|---|---|---|---|
| :last-child | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
定义和用法
:last-child 选择器匹配属于其父元素的最后一个子元素的每个元素。
提示:p:last-child 等同于 p:nth-last-child(1)。
CSS 语法
:last-child {css declarations;}