【CSS3】使用:not(:last-of-type)简化你的css代码

本文介绍了如何使用CSS3的选择器`:not(:last-of-type)`来简化CSS代码,通过实例展示了如何用一行代码替换原本五行的代码,从而提高代码的简洁性和效率。

演示效果:第三个标题没有下边框

这里写图片描述

以前写代码是这样:

<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行代码。

@charset "UTF-8"; /*-------------------------------------------画面レイアウトの調整(位置と幅)【Start】-----------------------------------------------------*/ #PART_sidI_RIYO_ID { min-width: 1387px; } #sidI_NM { width: 240px; } #sidI_POSI { width: 190px; } #_i_ins_type_str_item { width: 450px; } #_i_ins_type_str_radio { display: inline-block; width: 450px; } #_i_ins_type_str_radio label:nth-last-child(2), #_i_ins_type_str_radio label:nth-last-child(3), #_i_ins_type_str_radio label:nth-last-child(4) { display: inline-block; width: 130px; } #PART_sidG1 { height: 536px; width: 1387px; margin-top: 30px; border: 1px solid lightgray; } #TH_g1 { width: 1397px; border-collapse: collapse; margin: -4px 0px 0px -8px; } #TH_g1 th, #TH_g1 td:not([colspan="5"]) { border: 1px solid lightgray; } *[id$="sidG1_LINE_NO_field"] { width: 60px; padding-left: 6px; } #sidG1_LINE_NO_label { width: 60px; padding-left: 4px; } *[id$="sidG1_RIYO_ID_field"] { width: 82px; padding-left: 10px; } #sidG1_RIYO_ID_label { width: 82px; padding-left: 10px; } *[id$="sidG1_SHOKUIN_NO_field"] { width: 84px; padding-left: 10px; } #sidG1_SHOKUIN_NO_label { width: 84px; padding-left: 10px; } *[id$="sidG1_NM_field"] { width: 190px; } #sidG1_NM_label { width: 190px; padding-left: 20px; } *[id$="sidG1_POSI_field"] { width: 140px; } #sidG1_POSI_label { width: 140px; padding-left: 10px; } *[id$="sidG1_SHZK_NM_field"] { width: 190px; } #sidG1_SHZK_NM_label { width: 190px; padding-left: 10px; } *[id$="sidG1_START_DATE_field"] { width: 159px; padding-left: 15px; } #sidG1_START_DATE_label { width: 159px; padding-left: 10px; } *[id$="sidG1_END_DATE_field"] { width: 159px; padding-left: 15px; } #sidG1_END_DATE_label { width: 159px; padding-left: 10px; } *[id$="sidG1_INS_TYPE_NAME_field"] { width: 66px; padding-left: 12px; } #sidG1_INS_TYPE_NAME_label { width: 66px; padding-left: 10px; } #sidAH_SHZK { visibility: hidden; } #sidAH_CLR { visibility: hidden; } #sidA_NEW_INS { margin-left: -62px; } #sidA_CNG { margin-left: 2px; } #sidA_DEL { margin-left: 3px; } #sidA_INQ { margin-left: 4px; } #sidA_END { margin-left: 834px; } #sidA_HYJ { margin-left: 440px; } fieldset.part_box { min-width: 0px; } /*-------------------------------------------画面レイアウトの調整(位置と幅)【End】-----------------------------------------------------*/ /*-------------------------------------------画面レイアウトの調整(スタイル)【Start】-----------------------------------------------------*/ #TH_g1 tr.error td { border-bottom: none; } /*-------------------------------------------画面レイアウトの調整(スタイル)【End】-----------------------------------------------------*/ 代码有什么问题 有哪些需要改进的地方
10-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值