:where()
Quick Summary for :where
The :where() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list.
Code Usage for :where
/* Selects any paragraph inside a header, main    or footer element that is being hovered */ :where(header, main, footer) p:hover {   color: red;   cursor: pointer; }  /* The above is equivalent to the following */ header p:hover, main p:hover, footer p:hover {   color: red;   cursor: pointer; } 
More Details for :where

:where()

The :where() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list.

/* Selects any paragraph inside a header, main    or footer element that is being hovered */ :where(header, main, footer) p:hover {   color: red;   cursor: pointer; }  /* The above is equivalent to the following */ header p:hover, main p:hover, footer p:hover {   color: red;   cursor: pointer; } 

The difference between :where() and :is() is that :where() always has 0 specificity, whereas :is() takes on the specificity of the most specific selector in its arguments.

Forgiving Selector Parsing

The specification defines :is() and :where() as accepting a forgiving selector list.

In CSS when using a selector list, if any of the selectors are invalid then the whole list is deemed invalid. When using :is() or :where() instead of the whole list of selectors being deemed invalid if one fails to parse, the incorrect or unsupported selector will be ignored and the others used.

:where(:valid, :unsupported) {   ... } 

Will still parse correctly and match :valid even in browsers which don't support :unsupported, whereas:

:valid, :unsupported {   ... } 

Will be ignored in browsers which don't support :unsupported even if they support :valid.

Examples

Comparing :where() and :is()

This example shows how :where() works, and also illustrates the difference between :where() and :is().

Take the following HTML:

<article>   <h2>:is()-styled links</h2>   <section class="is-styling">     <p>Here is my main content. This <a href="https://mozilla.org">contains a link</a>.   </section>    <aside class="is-styling">     <p>Here is my aside content. This <a href="https://developer.mozilla.org">also contains a link</a>.   </aside>    <footer class="is-styling">     <p>This is my footer, also containing <a href="https://github.com/mdn">a link</a>.   </footer> </article>  <article>   <h2>:where()-styled links</h2>   <section class="where-styling">     <p>Here is my main content. This <a href="https://mozilla.org">contains a link</a>.   </section>    <aside class="where-styling">     <p>Here is my aside content. This <a href="https://developer.mozilla.org">also contains a link</a>.   </aside>    <footer class="where-styling">     <p>This is my footer, also containing <a href="https://github.com/mdn">a link</a>.   </footer> </article> 

In this somewhat-contrived example, we have two articles that each contain a section, an aside, and a footer. They differ by the classes used to mark the child elements.

To make selecting the links inside them simpler, but still distinct, we could use :is() or :where(), in the following manner:

html {   font-family: sans-serif;   font-size: 150%; }  :is(section.is-styling, aside.is-styling, footer.is-styling) a {   color: red; }  :where(section.where-styling, aside.where-styling, footer.where-styling) a {   color: orange; } 

However, what if we later want to override the color of links in the footers using a simple selector?

footer a {   color: blue; } 

This won't work for the red links, because the selectors inside :is() count towards the specificity of the overall selector, and class selectors have a higher specificity than element selectors.

However, selectors inside :where() have specificity 0, so the orange footer link will be overridden by our simple selector.

Note: You can also find this example on GitHub; see is-where.

Syntax

:where( <complex-selector-list> )

where <complex-selector-list> = <complex-selector>#

where <complex-selector> = <compound-selector> [ <combinator>? <compound-selector> ]*

where <compound-selector> = [ <type-selector>? <subclass-selector>* [ <pseudo-element-selector> <pseudo-class-selector>* ]* ]!<combinator> = '>' | '+' | '~' | [ '||' ]

where <type-selector> = <wq-name> | <ns-prefix>? '*'<subclass-selector> = <id-selector> | <class-selector> | <attribute-selector> | <pseudo-class-selector><pseudo-element-selector> = ':' <pseudo-class-selector><pseudo-class-selector> = ':' <ident-token> | ':' <function-token> <any-value> ')'

where <wq-name> = <ns-prefix>? <ident-token><ns-prefix> = [ <ident-token> | '*' ]? | <id-selector> = <hash-token><class-selector> = '.' <ident-token><attribute-selector> = '[' <wq-name> ']' | '[' <wq-name> <attr-matcher> [ <string-token> | <ident-token> ] <attr-modifier>? ']'

where <attr-matcher> = [ '~' | | | '^' | '$' | '*' ]? '='<attr-modifier> = i | s

Specifications

Specification
Selectors Level 4 # zero-matches

See also

:is() Selector list Web components

Last modified: Jan 17, 2022, by MDN contributors

Select your preferred language English (US)Français日本語Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986
Random CSS Property

linear-gradient()

The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the <gradient> data type, which is a special kind of <image>.
linear-gradient() css reference