Can I write a CSS selector selecting elements NOT having a certain class or attribute?

前端 未结 10 614
不知归路
不知归路 2020-11-22 10:47

I would like to write a CSS selector rule that selects all elements that don\'t have a certain class. For example, given the following HTML:



        
10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 11:51

    :not([class])
    

    Actually, this will select anything that does not have a css class (class="css-selector") applied to it.

    I made a jsfiddle demo

        h2 {color:#fff}
        :not([class]) {color:red;background-color:blue}
        .fake-class {color:green}
        

    fake-class will be green

    empty class SHOULD be white

    no class should be red

    fake-class2 SHOULD be white

    empty class2 SHOULD be white

    no class2 SHOULD be red

    Is this supported? Yes : Caniuse.com (accessed 02 Jan 2020):

    • Support: 98.74%
    • Partial support: 0.1%
    • Total:98.84%

    Funny edit, I was Googling for the opposite of :not. CSS negation?

    selector[class]  /* the oposite of :not[]*/
    

提交回复
热议问题