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

前端 未结 10 529
不知归路
不知归路 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:45

    As others said, you simply put :not(.class). For CSS selectors, I recommend visiting this link, it's been very helpful throughout my journey: https://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-16048

    div:not(.success) {
      color: red;
    }
    

    The negation pseudo class is particularly helpful. Let's say I want to select all divs, except for the one which has an id of container. The snippet above will handle that task perfectly.

    Or, if I wanted to select every single element (not advised) except for paragraph tags, we could do:

    *:not(p) {
      color: green;
    }
    

提交回复
热议问题