Is there a CSS selector for element without any class?

前端 未结 1 1190
挽巷
挽巷 2020-11-30 09:51

Is there a CSS selector for element without any class? For example in HTML

Section A
相关标签:
1条回答
  • 2020-11-30 10:19

    With section:not([class]) you select every section without the class attribute. Unfortunately, it won't select those sections with an empty class attribute value. So in addition, we have to exclude these sections:

    section:not([class]) { /* every section without class - but won't select Section C */
      color: red;
    }
    
    section[class=""] { /* selects only Section C */
      font-weight: bold;
    }
    <section>Section A</section>
    <section class="special">Section B</section>
    <section class="">Section C</section>

    Further reading

    • CSS attribute selector, browser support
    • :not
    0 讨论(0)
提交回复
热议问题