CSS3 :not selector
Complete CSS Selector Reference Manual
Example
Set the background color for every element that is not a
element:
:not
(
p
)
{
color:
#ff0000
;
}
Try it »
Definition and Usage
:not(selector) selector matches every element that is not the specified element/selector.
The CSS :not pseudo-class allows you to select all elements except the specified selector. This is a way to apply styles by excluding specific elements.
Basic Syntax
css
:not(selector) {
/* Style rules */
}
- selector: This is the selector you want to exclude.
** 1. Exclude elements of a specific type**
body :not(p) {
color: blue;
}
This code sets the text color of all elements inside the body element except p elements to blue.
2. Exclude elements with a specific class
div :not(.highlight) {
background-color: gray;
}
This code sets the background color of all elements inside div elements that do not have the highlight class to gray.
** 3. Exclude multiple selectors**
:not(div, span) {
font-size: 14px;
}
This code sets the font size of all elements on the page that are neither div nor span to 14 pixels.
4. Exclude pseudo-classes
a:not(:hover) {
color: black;
}
This code sets the text color of all links that are not in the hover state to black.
** 5. Exclude attribute selectors**
input:not([type="text"]) {
border: none;
}
This code removes the border from all input elements that do not have type="text".
Note
:notPseudo-classes are part of CSS3 selectors and are widely supported by browsers.:notPseudo-classes can improve the flexibility of CSS code, allowing you to control the application of styles more precisely.- You can
:notA single selector or multiple selectors separated by commas can be used inside the pseudo-class.
Browser support
The numbers in the table indicate the first browser version that supports the selector.
| Selector | |||||
|---|---|---|---|---|---|
| :not() | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |