- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - Pseudo-class :not()
The CSS pseudo-class function :not() represents or matches the elements that do no match a given list of selectors. It is also known as negation pseudo-class, as it avoids the selection of specific items from a list.
The pseudo-class :not() is a very tricky pseudo-class and renders unexpected results. So you should be aware of it.
Few of the unexpected and unusual results of using :not() are as follows:
The :not() pseudo-class is used to write the useless selectors, like :use(*) shows that it will match any element, which is not an element. This doesn't make any sense, henc no rule will be applied.
It can increase the specificity of a rule; such as #foo.not(#bar), will match the same element as simple as #foo, with increased specificity of two selector ids.
The specificity of most specific selector, from the list of comma-separated selectors, replaces that of the :not() pseudo-class.
If you pass something like this :not(.foo), the pseudo-class will match everything, including the basic HTML tags such as <html> or <body>.
If you pass :not(table), it will result is matching of tr, tbody, th, td, caption, etc, as they can all match the given argument :not(table).
At a time multiple selectors can be negated, like :not(.foo, .bar), means :not(.foo) :not(.bar)
When any selector that is passed as a part of the list, proves to be invalid or unsupported, will result in invalidating the whole rule. To avoid this situation, you should use :is(), as it has the property of forgiving selector list.
Syntax
:not(<complex-selector-list>) { /* ... */ }
The pseudo-class :not() needs a comma-separated list of one or more selectors, to be passed as its argument. The list should not include another :not() or pseudo-element.
CSS :not Example
Here is an example of :not() pseudo-class.
In this example:
a class (.sample) is declared with some CSS styling.
:not() pseudo-class is used with .sample class on h1 element.
:not() pseudo-class is used with h1 as argument, and thus CSS styling applied to all the other elements, except h1 element.
<html> <head> <style> .sample { text-shadow: 2px 2px 3px yellow; } /* <h1> elements that are not in the class '.sample' */ h1:not(.sample) { background-color: lightblue ; } /* Elements that are not <h1> elements */ body :not(h1) { text-decoration-line: line-through; } /* Elements that are not <div> and not <span> elements */ body :not(div):not(span) { font-weight: bold; } </style> </head> <body> <h1>heading with :not(.sample) pseudo-class applied</h1> <h1 class="sample">heading with styling applied</p> <p>Paragraph, hence :not(h1) and :not(div):not(span) applied.</p> <div>div element, hence :not(h1) applied.</div> </body> </html>
Following is another example for :not(), where another pseudo-class :nth-child is used to select the alternate p element in the container, where styling is applied.
<html> <head> <style> p:not(:nth-child(2n+1)) { font-size: 3em; } </style> </head> <body> <h1>Heading</h1> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <p>Paragraph 4</p> </body> </html>
In the following example :not() is applied on <li> with class .sample. So the styling is applied on the <li> with no .sample class.
<html> <head> <style> li:not(.sample) { font-size: 2.5em; color: red; } .sample { color: green; font-weight: 800; background-color: lightyellow; } </style> </head> <body> <h1>Unordered List</h1> <ul> <li>list item 1</li> <li class="sample">list item 2</li> <li>list item 3</li> </ul> </body> </html>
To Continue Learning Please Login
Login with Google