CSS :not(selector) property
The :not(selector)
pseudo-class in CSS is used to apply styles to elements that do not match a given selector. This pseudo-class allows for the exclusion of specific elements from certain styles, providing a way to apply styles to a broader range of elements while excluding others.
Syntax
selector:not(excluded-selector) {
/* styles */
}
selector
: The main selector that targets elements you want to style.:not(excluded-selector)
: The pseudo-class that excludes elements matching theexcluded-selector
from receiving the styles applied to theselector
.
How It Works
Basic Usage:
- The
:not(selector)
pseudo-class can be used to exclude specific elements from a broader set of styles.
p:not(.exclude) { color: blue; }
- In this example, all
<p>
elements that do not have the classexclude
will be styled with blue text.<p>
elements with theexclude
class will not be affected by this rule.
- The
Combining Selectors:
- You can combine
:not()
with other selectors to refine your styling.
div:not(.highlight) { background-color: lightgray; }
- This example applies a light gray background to all
<div>
elements except those with the classhighlight
.
- You can combine
Using Multiple Selectors:
- You can also use multiple selectors inside
:not()
, but only one:not()
is allowed in a selector group.
ul li:not(.active):not(.completed) { color: gray; }
- This targets
<li>
elements within<ul>
that do not have the classactive
and do not have the classcompleted
, applying gray color to them.
- You can also use multiple selectors inside
Examples
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Not Selector Example</title>
<style>
p:not(.highlight) {
color: gray;
}
div:not(.special) {
border: 1px solid black;
}
ul li:not(:last-child) {
margin-bottom: 10px;
}
</style>
</head>
<body>
<p>This paragraph will be gray.</p>
<p class="highlight">This paragraph will not be gray.</p>
<div>This div will have a border.</div>
<div class="special">This div will not have a border.</div>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
Explanation:
p:not(.highlight)
:- Applies gray text color to all
<p>
elements that do not have the classhighlight
.
- Applies gray text color to all
div:not(.special)
:- Applies a border to all
<div>
elements that do not have the classspecial
.
- Applies a border to all
ul li:not(:last-child)
:- Applies a bottom margin to all
<li>
elements within<ul>
except for the last child.
- Applies a bottom margin to all
Important Points
Single Level Only:
:not()
only works on a single level of selectors and cannot be nested within other pseudo-classes like:nth-child
.
Performance Considerations:
- Using
:not()
with complex selectors can impact performance, particularly with large documents. Keeping:not()
selectors simple can help maintain performance.
- Using
Combining with Other Pseudo-Classes:
:not()
can be used with other pseudo-classes to create more specific and refined styling rules.
div:not(:first-child) { padding: 10px; }
- This example applies padding to all
<div>
elements that are not the first child of their parent.