Search code examples
htmlcsscss-selectorspseudo-class

:not() pseudo selector works incorrect with some tags


HTML:

<p>I`m p</p>
<a>I`m a</a>
<h2>I`m h2</h2>

CSS:

:not(p){
 color:red;}

:not() pseudo class should select all the elements inside the HTML document, that aren`t "p", and give them red color, but when i run the code "p" is red too, just like all other elements.


Solution

  • Notorious Zet you can fix this error by giving the p element a class and then using the not pseudo selector

    The HTML

    <p class = "notRed"> This is a p element </p>
    <h1>This is a h1 element</h1>
    

    The CSS

    p:not(notRed){
    color: red /*This will apply to all p elements with the class of notRed*/
    }