Master :not CSS Selector with Complete 101 Guide

Written by Shan Shah
Published on July 22, 2021
4 mins read
learn :not css selector with examples

The :not css selector is an incredibly powerful selector that every web developer should know about 🚀.

Many web developers don't know about this selector and don't use it because they don't understand its power.

This guide 📕 is going to change all that. In this guide, I'm going to share with you everything you need to know to get started using the :not css selector... right away!

You'll learn how to use the :not css selector with 4 different examples, which means you'll be able to start using it immediately in your next project.

All you need is to read this guide without skipping any steps.

There are so many use-cases where :not css selector can be helpful.

What if you want to add some margin-top to all elements except the first child? Or have a desire to skip the specific element or class for styling?

So, what is :not css selector?

It's formula is very simple & straightforward. Consider :not css as except or skip.  

When you want to apply the styles to all the elements by skipping the specific class or element, then :not css selector is the way to go with.

Let's dive into several examples or use-cases, so you can understand the actual power of :not css selector.

:not css selector with another pseduo class

Add the margin-top to all the list items except the first child. It's a really common case, and we use it from time to time.

So, we have list items where ul has a class name list, and li items are assigned common class list-item.

We selected the list-item class and added :not selector

After that, using the :first-child, which is also another selector, to select the first child from the list of elements. 

Now our command looks like .list-item:not(:first-child), which means, apply the styles to all the list-item except first-child.

Below is a working example with output:

<h2 class="heading">Example #1</h2>
<ul class="list">
   <li class="list-item">PHP</li>
   <li class="list-item">CSS</li>
   <li class="list-item">HTML</li>
   <li class="list-item">JS</li>
</ul>
.list-item:not(:first-child){
  margin-top:1rem;
  background-color: #ff5852;
}
:not css selector

:not css selector with the class or id

Add the background color to all the list items except the .active class.

We have the same case as in the first example. The only difference is instead of using first-child, we will target the class which is with a list-item, and that is .active class.

Our command looks like .list-item:not(.active), which means, apply the styles to all the list-item except the list-item with another class which is .active.

Just a quick note here, you can also use the id, .list-item:not(#active). For this to work, you will need to put on an id of active on the element of the list-item.

Below is a working example with output:

Tip:

:not css selector can be used for a specific element, specific child element, a specific attribute, specific class name inside the element, and so on.

:not css selector with the attribute 

Apply the background color to all the list items except the one with attribute attr=php.

So, we just added attr for demonstration purposes, you can add or select any type of attribute, and yes, attr is just a random name.

Our command looks like .list-item:not([attr=php]), which means, apply the styles to all the list-item except the list-item with an attribute value set to php.

Below is a working example with output:

:not css selector with the child element

Apply background-color to all the inner child (anchor tag) of list-items except the one with the class .active

Our command looks like .list-item:not(.active) a, which means, apply the styles to all the anchors (a) of list items except the one which has .active class.

Below is a working example with output:

I hope you found this article helpful and that it has given you another skill set to add to your coding arsenal! 

If you are interested in learning more about CSS Tips & Tricks, we encourage you to subscribe to our newsletter.

We are always happy to hear your feedback, so please feel free to let us know by sharing it with others.

Master :not CSS Selector with Complete 101 Guide Click To Tweet

As a web developer, you will definitely love this guide as well: Quick tips to build your own website as a web developer

Yes, we do have YouTube Channel too for project based video content tutorials & courses at: Coder Champ

Become a better Web Developer in 15 minutes a week. Join Now 📩
We hate spam, unsubscribe anytime.
Let's Discuss 🚀
JOIN FACEBOOK GROUP
Coder Champ © 2021
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram