在 CSS3 中的结构伪类选择器是选择父级标签下的子标签的最后一个子标签,但是在下述代码中标签dd 的子标签有三个 li 标签和一个a 标签 虽然 a 标签是dd 标签的最后一个子标签。我们预想 .timebook dd li:last-child 选择的是 dd 标签下的三个 li 标签中的最后一个,代码运行结果却没有达到想要的样式效果,将 a 标签注释掉发现达到了想要的效果,说明 .timebook dd li:last-child 选择的是 dd 的最后一个子标签 即是 a 标签。这里应该用 nth-child(n) 结构伪类选择器。选择最后一个 li 标签即 选择 dd 标签的第三个子标签。
总结:当父标签下有多个不一样的标签时,last-child 结构伪类选择器选择的是最后一个子标签 即使加上限制条件 也无法改变。
<div class="timebook">
<dl>
<dt>我的课程表</dt>
<dd>
<li>
<h5>继续学习 程序语言设计</h5>
<p>正在学习-使用对象</p>
</li>
<li>
<h5>继续学习 程序语言设计</h5>
<p>正在学习-使用对象</p>
</li>
<li>
<h5>继续学习 程序语言设计</h5>
<p>正在学习-使用对象</p>
</li>
<a href="#" class="all">全部课程</a>
</dd>
</dl>
</div>
.timebook {
width: 228px;
height: 300px;
position: absolute;
right: 0;
top: 50%;
background-color: #fff;
/*transform: translate(0, -50%);*/
transform: translateY(-50%);
/*但是 y 要大写才可以*/
}
.timebook dt {
height: 50px;
background-color: #9bceea;
font-weight: 700;
color: #fff;
line-height: 50px;
font-size: 18px;
text-align: center;
border-bottom: 1px solid #ccc;
}
.timebook dd {
padding: 15px 20px 0;
}
.timebook dd li {
height: 60px;
padding-top: 6px;
border-bottom: 1px solid #ccc;
}
.timebook dd li:nth-child(3) {
border: 0;
}
/*.timebook dd li:last-child {
border: 0;
}*/
.timebook h5 {
font-size: 16px;
font-weight: normal;
}
.all {
display: block;
height: 40px;
color: #22b0ff;
border: 1px solid #22b0ff;
text-align: center;
line-height: 40px;
font-size: 16px;
}
.all:hover {
background-color: #22b0ff;
color: #fff;
}