English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS reference manual

CSS @rules

CSS attribute大全

CSS3 :not selector

The CSS pseudo-class :not() is used to match elements that do not match a set of selectors. Since its function is to prevent certain elements from being selected, it is also known as a negation pseudo-class (negation pseudo-)-class).

Complete CSS Selector Reference Manual

Note:

  • :not() Pseudo-classes cannot be nested, which means :not(:not(...)) Is invalid.

  • Since pseudo-elements are not simple selectors, they cannot be treated as :not() with parameters, such as :not(p::before) This selector will not work.

  • This pseudo-class can be used to write a completely useless selector. For example, :not(*) matches any non-element element, so this rule will never be applied.

  • This pseudo-class can be used to increase the priority of the rule. For example, #foo:not(#bar) and #foo will match the same elements, but the former has a higher priority.

  • :not(.foo) will match any non- .foo elements,including <html> and <body>.

  • This selector will only apply to one element and cannot be used to exclude all parent elements. For example, body :not(table) a will still apply to table elements <table> internal <a> , because <tr>will be :not(table) This part of the selector matches.

Online Example

Set the background color for each element that is not a <p> element:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>Basic Tutorial(oldtoolbag.com)</title>
<style>
p {
    color: #000000;
}
:not(p) {
    color: #ff0000;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<div>This is some text of the div element.</div>
<a href="//www.oldtoolbag.com/" target="_blank">Link to Basic Tutorial</a>
</body>
</html>
Test to see ‹/›

Definition and Usage

:not() pseudo-class can take one or more selector lists separated by commas as its parameters. The selector should not contain another negation selector or pseudo-element.

Browser Compatibility

The numbers in the table indicate the version number of the first browser that supports the selector.

Selector




:not()4.09.03.53.29.6

Complete CSS Selector Reference Manual