- 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 :where()
The CSS pseudo-class function :where() accepts a list of selectors as input and selects each element that matches any of the selectors in that list.
Syntax
:where(<complex-selector-list>) { /* css declarations */ }
CSS :where Example
The following example demonstrates the use of :where() pseudo-class.
<html> <head> <style> main :where(h1, h2, h3) { color: rgb(102, 0, 255); } :where(h2) { text-decoration-line: underline; } div { border: 3px solid black; } </style> </head> <body> <main> <h1>Heading 1</h1> <h3>Heading 3</h3> <div> <h2>Heading 2</h2> <p>Paragraph under div</p> </div> </main> </body> </html>
Difference between :where() and :is()
The difference between :where() and :is() lies in their specificity behaviour as listed below:
:where() | :is() |
---|---|
It is a CSS selector that allows you to group selectors without increasing specificity. | It is a CSS selector that allows you to group selectors, but unlike :where(), it inherits the specificity of the most specific selector within its parentheses |
It acts as a container into which you can write complex selectors without affecting the specificity of those selectors. | It is used to increase specificity only when needed, while preserving the specificity of the individual selectors |
For example, :where(div, p) { /* styles */ } groups the div and p selectors without increasing specificity. | For example, :is(div, p) { /* styles */ } would take on the specificity of either div or p, whichever is more specific. |
In summary, :where() maintains a specificity of 0, while :is() adjusts its specificity based on the most specific selector within its parentheses.
Example
This example demonstrates how :is() can be used for specific styling.
:where() can be used to add additional styles without altering the specificity or overriding styles set by :is().
The specific styles for .special-box elements are maintained, and additional styles are added using :where().
<html> <head> <style> :is(.box, .special-box) { background-color: lightgray; color: black; font-weight: bold; } :where(.box, .special-box) { border: 2px solid black; padding: 10px; margin: 10px; } :is(.special-box) { background-color: blue; color: white; } :where(.special-box) { font-style: italic; } </style> </head> <body> <div class="container"> <div class="box">Box A</div> <div class="special-box">Special Box</div> <div class="box">Box B</div> </div> </body> </html>
Forgiving Selector Parsing
The concept of forgiving selector parsing in CSS refers to how :is() and :where() selectors handle invalid selectors within a selector list.
In CSS, if a selector within a selector list is invalid, the entire list is considered invalid, and the styles associated with it are not applied.
Using :is() and :where(), an incorrect or unsupported selector within the list is ignored, and the remaining valid selectors are still applied. In other words, :is() and :where() provide a forgiving mechanism where individual invalid selectors do not destroy the entire selector list.
Example
In the following example:
:is() and :where() selectors are used for styling specific elements.
Special styles are applied to boxes with the class .special inside the .content div.
Headers inside the .container div get a unique style, ignoring invalid selectors.
All boxes inside the .container receive a common styling.
Paragraphs within the .footer inside the .container are styled differently.
An invalid selector inside :where(.box.invalid-selector) doesn't disrupt the entire rule, demonstrating forgiving selector parsing.
<html> <head> <style> :where(.content) :is(.box.special) { background-color: yellow; color: black; font-weight: bold; } :where(.container) :where(.header, .invalid-selector) { background-color: lightblue; color: white; } :where(.container) :is(.box) { border: 2px solid black; margin: 10px; padding: 10px; } :where(.container) :where(.footer) p { font-style: italic; color: gray; } :where(.container) :where(.box.invalid-selector) { text-decoration: underline; } </style> </head> <body> <div class="container"> <div class="header"> <h1>Main Heading</h1> </div> <div class="content"> <div class="box">Box 1</div> <div class="box special">Special Box</div> <div class="box">Box 3</div> </div> <div class="footer"> <p>Footer Content</p> </div> </div> </body> </html>
To Continue Learning Please Login
Login with Google