To select an element and all its descendant elements:
.media, .media * {color: #f00;}
Is there just one selector I can use instead of two selectors separated by a comma? I'm looking for a more efficient way to type this.
To select an element and all its descendant elements:
.media, .media * {color: #f00;}
Is there just one selector I can use instead of two selectors separated by a comma? I'm looking for a more efficient way to type this.
In 2023, recent additions to CSS syntax and available pseudo-classes bring some interesting alternative ways to tackle this (although still not as ideal as a truly native self-or-descendant combinator).
Let's consider a hypothetical .prose
class that identifies a site's authored content, which can be delivered either in e.g. <article>
or <section>
blocks with nested structure, or in e.g. <p>
or <blockquote>
elements that directly contain text.
With only strict descendant combinators and traditional CSS syntax, elements such as <p>
and <blockquote>
require the addition of a (potentially un-semantic) wrapper, such as a <div>
element.
Alternatively, the CSS rules can be written in a way that matches both self and descendants, but this involves repetitive (and error-prone) boilerplate:
h1.prose, .prose h1 {...}
h2.prose, .prose h2 {...}
h3.prose, .prose h3 {...}
h4.prose, .prose h4 {...}
h5.prose, .prose h5 {...}
h6.prose, .prose h6 {...}
p.prose, .prose p {...}
blockquote.prose, .prose blockquote {...}
/* And so forth... */
Leveraging recent CSS developments, the above can be re-written in the following way:
.prose, .prose * {
&:is(h1) {...}
&:is(h2) {...}
&:is(h3) {...}
&:is(h4) {...}
&:is(h5) {...}
&:is(h6) {...}
&:is(p) {...}
&:is(blockquote) {...}
/* Alternatively, the :where() pseudo-class can also be used,
which differs in how specificity is calculated. */
}
Obviously, the &:is(...)
part is still boilerplate, but repetition has been cut down significantly, which can be considered an improvement.
Caveat: As of May 2023, browser support for native CSS nesting is still a work-in-progress (see https://caniuse.com/css-nesting) and CSS code that takes advantage of it should be converted to non-nested syntax (using, for example, the postcss-nesting plugin). The :is()
pseudo-class enjoys more widespread support (see https://caniuse.com/?search=is).