css 简化,CSS小技巧:使用:not(:last-of-type)简化你的css代码

终于找到了一个好方法,使用:not(:last-of-type)简单方便,再也不要麻烦的单独使用:last-of-type了,不错!

我以前也没有想到过这种用法,这个小小的技巧能省去很多时间和代码。下面让我解释一下,比如说,我们要美化一个文章列表。

  • Post Title

    Thurs, Feb 16, 2017

我以前的实现方法是这样的,其实是很笨的方法!

.posts {

list-style: none;

margin: 0;

padding: 0;

}

.post {

border-bottom: 1px solid #eee;

margin-bottom: .5rem;

padding-bottom: .5rem;

&:last-child {

border-bottom: 0;

margin-bottom: 0;

padding-bottom: 0;

}

}

这css的目的是要让列表的文章之间有间隔,但最后一个不需要。这个时候,:not(:last-of-type)就大限神威了!

.post:not(:last-of-type) {

border-bottom: 1px solid #eee;

margin-bottom: .5rem;

padding-bottom: .5rem;

}

使用not(:last-of-type),我们省掉了5行代码。试想在我们编写的其它代码中都使用这种技巧,不知道要省掉多少工夫。下面是一个实例演示。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值