CSS the :not() selector

Hi, my name is Kars van Iersel. I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
Search for a command to run...

Hi, my name is Kars van Iersel. I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
If you are an SEO newbie or have some experience but would like to get a step-by-step SEO guide on how to avoid common website promotion mistakes, here's a list of SEO tasks based on best practices that you should take into account when creating and ...

Introduction Roughly a year ago I had to make a very large scale application and I couldn't decide on a backend framework. However, I knew I wanted to use Vue.js as my front-end. So I began my long quest the ideal framework for my backend. I had doub...

6 Tips on how to be more focused as a web developer

Introduction No skill is more important for programmers than the ability to write code that works. But all too often, developers don't think about whether their code will work until it's finished, and by then they've already created many bugs that ar...

In my previous post
I wrote a bit about the :not() selector and I got a lot feedback that people never heard of this element. So I figured I would dedicate a post just to the :not() CSS selector.
The :not() is a CSS pseudo-class that targets elements that do not match the selector given. Since it prevents specific items from being selected, it is known as the negation pseudo-class.
In essence you can target anything except what you put in the :not() selector.
There are a couple of rules when using the :not() selector. You should keep this in mind when using the :not() selector in your projects, cause they could cause unsual outcomes that have you burn hours trying to figure them out.
:not() pseudo-class, which means you cannot do :not(:not(...))::before and ::after. It will work with :first-child and :last-child since they are simple selectors.:not() selector can increase the specificity of a rule. #foo:not(#bar) will match the same element as the simpler #foo but with a higher specificity.:not(.class) will match anything that isn't .class including <html> and <body>.:not() selector only applies to one element; you cannot use it to exclude all ancestors. body :not(table) a will still apply to links inside of a table, since <tr> will amtch with the :not()It is possible to use the :not() selector with multiple classes.
Normally you would just want to do:
p:not(.foo) {
}
But maybe you want to avoid multiple classes? There are no real combinators with :not() and you cannot nest them. But you can chain them, which works similar to and.
p:not(.foo):not(.bar):not(.bold):not(.italic) {
}
I use the :not() CSS selector most often with the :first-child or :last-child pseudo-class.
Think of having a list that you want to add some spacing to, but you don't want to last item to also have spacing at the bottom right? Well with :not() that is super easy to solve!
li:not(:last-child) {
margin-bottom: 20px;
}
This CSS will be applied to all <li> elements except the last child.
You could also do the reverse with :first-child
li:not(:first-child) {
margin-top: 20px;
}
Or use it with :nth-child()
li:not(:nth-child(2)) {
margin: 20px 0;
}
A lot of handy things can be achieved by using the :not() CSS selector. I know I use it a lot of times, for menus, list items and what not. Even flexbox grids!
I hope you learned something from this post, and hopefully you can enhance your CSS skills with this knowledge.
Let me know how you apply the :not() selector, I'm always eager to new learn tricks with it.