©
本文档使用
php中文网手册 发布
E:last-child { sRules }
要使该属性生效,E元素必须是某个元素的子元素,E的父元素最高是body,即E可以是body的子元素
E:last-child选择符,E必须是它的兄弟元素中的最后一个元素,换言之,E必须是父元素的最后一个子元素。与之类似的伪类还有E:first-child,只不过情况正好相反,需要它是第一个子元素。
通过具体的例子来进行理解:
有效的代码:
p:last-child{color:#f00;}
<div>
<h2>我是一个标题</h2>
<p>我是一个p</p>
</div>无效的代码:
p:last-child{color:#f00;}
<div>
<p>我是一个p</p>
<h2>我是一个标题</h2>
</div>在上述代码中,如果我们要设置第一个li的样式,那么代码应该写成li:first-child{sRules},而不是ul:first-child{sRules}。
| IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
|---|---|---|---|---|---|---|---|
| 6.0-8.0 | 2.0+ | 4.0+ | 3.1+ | 3.5+ | 3.2+ | 2.1+ | 18.0+ |
| IE9.0+ |
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8" />
<title>结构性伪类选择符 E:last-child_CSS参考手册_web前端开发参考手册系列</title>
<meta name="author" content="Joy Du(飘零雾雨), dooyoe@gmail.com, www.doyoe.com" />
<style>
h1 {
font-size: 16px;
}
li:last-child {
color: #f00;
}
</style>
</head>
<body>
<h1>注意是li:last-child,而不是ul:last-child</h1>
<ul>
<li>结构性伪类选择符 E:last-child</li>
<li>结构性伪类选择符 E:last-child</li>
<li>结构性伪类选择符 E:last-child</li>
<li>结构性伪类选择符 E:last-child</li>
</ul>
</body>
</html>
点击 "运行实例" 按钮查看在线实例