【问题标题】:How to use :not and :first-child pseudo selectors simultaneously如何同时使用 :not 和 :first-child 伪选择器
【发布时间】:2015-06-17 02:27:29
【问题描述】:

我想使用:not:first-child 伪类选择第一个li 没有 class="test"。在此示例中,我尝试将蓝色设置为 <li>Third</li>:

ul li {
    color:red;
}

ul li:not(.test):first-child {
    color:blue;
}
<ul>
    <li class="test">First</li>
    <li class="test">Second</li>
    <li>Third</li>
    <li>Fourth</li>
</ul>

【问题讨论】:

    标签: css css-selectors pseudo-class


    【解决方案1】:

    为什么不执行以下操作,它使用规则组合来过滤您需要的项目(您可以组合规则 #1 和 #3)。

    这有一个额外的好处,它与类.test的第一个项目的索引无关

    ul li { /* <-- select all list items */
      color: red;
    }
    li:not(.test) { /* <-- select all list which arent test */
      color: blue;
    }
    ul li:not(.test) ~ li { /* <-- select all list items which follow an item without test */
      color: red;
    }
    <ul>
      <li class="test">First</li>
      <li class="test">Second</li>
      <li>Third</li>
      <li>Fourth</li>
    </ul>

    【讨论】:

    猜你喜欢
    • 2014-10-14
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    相关资源
    最近更新 更多