演示效果:第三个标题没有下边框
以前写代码是这样:
<style>
.posts {
list-style: none;
margin: 0;
padding: 0;
}
.post {
border-bottom: 1px solid #eee;
margin-bottom: .5rem;
padding-bottom: .5rem;
}
.post:last-child {
border-bottom: 0;
margin-bottom: 0;
padding-bottom: 0;
}
</style>
<!-- This is what your html would look like -->
<ul class="posts">
<li class="post">
<a href="/link-to-post/" title="Permalink to post">
<h2>Post Title</h2>
<small>Thurs, Feb 16, 2017<small>
</a>
</li>
</ul>
现在是这样:
<style>
.posts {
list-style: none;
margin: 0;
padding: 0;
}
.post:not(:last-of-type) {
border-bottom: 1px solid #eee;
margin-bottom: .5rem;
padding-bottom: .5rem;
}
</style>
<!-- This is what your html would look like -->
<ul class="posts">
<li class="post">
<a href="/link-to-post/" title="Permalink to post">
<h2>Post Title</h2>
<small>Thurs, Feb 16, 2017<small>
</a>
</li>
</ul>
使用not(:last-of-type),我们省掉了5行代码。
本文介绍了如何使用CSS3的选择器`:not(:last-of-type)`来简化CSS代码,通过实例展示了如何用一行代码替换原本五行的代码,从而提高代码的简洁性和效率。
604

被折叠的 条评论
为什么被折叠?



