How to write a CSS selector selecting elements not having a certain class or attribute?

Spread the love

Sometimes, we want to write a CSS selector selecting elements not having a certain class or attribute.

In this article, we’ll look at how to write a CSS selector selecting elements not having a certain class or attribute.

How to write a CSS selector selecting elements not having a certain class or attribute?

To write a CSS selector selecting elements not having a certain class or attribute, we use the :not pseudo-class.

For instance, we write

:not(.printable) {
  /* ... */
}

:not([attribute]) {
  /* ... */
}

to select all elements that don’t have the printable class with :not(.printable).

And we select all elements that don’t have the attribute attribute with :not([attribute]).

Conclusion

To write a CSS selector selecting elements not having a certain class or attribute, we use the :not pseudo-class.

Leave a Reply

Your email address will not be published. Required fields are marked *