返回到:CSS 伪类(Pseudo-classes) | CSS教程
定义和用法
:last-child 选择器用来匹配父元素中最后一个子元素。
提示: p:last-child 等同于 p:nth-last-child(1)。
浏览器支持
表格中的数字表示支持该属性的第一个浏览器版本号。
| 选择器 | 谷歌 | IE | 火狐 | 苹果 | opera |
|---|---|---|---|---|---|
| :last-child | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
实例
指定父元素 ul 中最后一个 li 元素,并设置背景颜色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程(web176.com)</title>
<style>
li:last-child {
background-color: #ff0000;
}
</style>
</head>
<body>
<ul>
<li>此元素背景色不是红色。</li>
<li>我的也不是红色。</li>
<li>我的才是红色。</li>
</ul>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/cssprop/5359.html
支付宝
微信