Definition and Usage
The :not(selectorX) functional pseudo-class, which represents negation, selects elements that are NOT selected by the selectorX selector specified as its argument.
The value inside the parentheses () of :not() is called an argument.
This selector is useful when you want to apply styles to elements while excluding those that match a specific selector.
Syntax
/* :not(selectorX)
selects elements that are NOT selected by the selectorX selector specified as its argument. */
:not(selectorX) {
/* ... */
}
Basic Example
The following example selects the <li> elements that are NOT selected by the class selector .not-bg and sets their background color:
li:not(.not-bg) {
background-color: yellowgreen;
}
<ul>
<li>I am an li element.</li>
<li class="not-bg">I am an li element with the .not-bg class selector.</li>
<li>I am an li element.</li>
<li>I am an li element.</li>
<li>I am an li element.</li>
</ul>
- I am an li element.
- I am an li element with the .not-bg class selector.
- I am an li element.
- I am an li element.
- I am an li element.
The :not(selectorX) pseudo-class is useful when you want to style a group of elements using the same selector while excluding specific ones.
When You Need to Exclude Multiple Selectors
Depending on the situation, you may need to exclude multiple selectors. In such cases, you can consider the following two approaches:
Approach 1
You can exclude multiple selectors by specifying them as the argument of the :not() pseudo-class. To do this, use a selector list.
The selectors inside the parentheses (selectorX) can be separated by commas (,) to specify multiple selectors at once.
For example, (selectorX1, selectorX2, selectorX3) specifies selectorX1, selectorX2, and selectorX3 together.
The following example applies a background color to all <li> elements except those with the class selectors .not-bg-1 and .not-bg-2:
li:not(.not-bg-1, .not-bg-2) {
background-color: yellowgreen;
}
<ul>
<li>I am an li element.</li>
<li class="not-bg-1">I am an li element with the .not-bg-1 class selector.</li>
<li class="not-bg-2">I am an li element with the .not-bg-2 class selector.</li>
<li>I am an li element.</li>
<li>I am an li element.</li>
</ul>
- I am an li element.
- I am an li element with the .not-bg-1 class selector.
- I am an li element with the .not-bg-2 class selector.
- I am an li element.
- I am an li element.
Approach 2
Use the :not() pseudo-class multiple times:
li:not(.not-bg-1):not(.not-bg-2) {
background-color: yellowgreen;
}
Approach 1 vs. Approach 2
Both Approach 1, which excludes multiple selectors using the argument of the :not() pseudo-class, and Approach 2, which uses the :not() pseudo-class multiple times, can be used when you need to exclude multiple selectors.
However, there is a slight difference between these two approaches.
They have different specificity values, which determine the priority of CSS selectors.
li:not(.not-bg-1, .not-bg-2) { /* Specificity = 0, 0, 1, 1 */
background-color: yellowgreen;
}
li:not(.not-bg-1):not(.not-bg-2) { /* Specificity = 0, 0, 2, 1 */
background-color: yellowgreen;
}
Approach 2, which uses the :not() pseudo-class multiple times, has higher specificity than Approach 1, which excludes multiple selectors through its argument.
Since both approaches select the same elements, apart from their specificity, you can choose either approach based on code readability and maintainability, unless specificity needs to be taken into account.
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
:not()
|
1 | 12 | 1 | 3.1 |
| Selector list argument | 88 | 88 | 84 | 9 |
Specifications
| Specification | |
|---|---|
:not()
|
Selectors Level 4 #negation |