CSS pseudo-classPseudo-classes

:is()

Matches an element against any selector in a list. Shortens long, repetitive selectors into a single notation.

On this page

Example

#
CSS
.link:is(:hover, :focus-visible) {
  color: #fff;
  text-decoration-color: #a78bfa;
  text-decoration-thickness: 2px;
}
Preview

Definition and usage

#

The :is() pseudo-class takes a list of selectors and matches when the element matches any one of them. Its main advantage is a shorter notation: instead of article h2, article h3, section h2, section h3, :is(article, section) :is(h2, h3) is enough.

The list inside :is() is forgiving. If one of the selectors is invalid or unsupported, the browser simply skips it, and the rest still works. A plain comma-separated list would reject the whole rule in that situation.

The specificity of :is() is the weight of the strongest selector in the list, regardless of which element it actually matches through. :is(#hero, p) has a weight of (1, 0, 0) even for plain paragraphs. If you want the same notation with zero weight, use :where().

Pseudo-elements canโ€™t appear inside the parentheses: :is(::before, ::after) wonโ€™t work, so pseudo-elements have to be listed separately.

Syntax

#
Syntax
:is( <selector list> )

Example selectors

#

Example selectors in practice

#

:is(article, section) h4

Headings inside article or section. One selector instead of two.
:is(article, section) h4 { background: #7c3aed; color: #fff; }

aside :is(h4, p)

The list can also sit at the end: a heading and a paragraph inside aside.
aside :is(h4, p) { background: #7c3aed; color: #fff; }

:is(article, aside) :is(h4, p)

Two lists give four combinations in one short notation.
:is(article, aside) :is(h4, p) { background: #7c3aed; color: #fff; }

:is(section, :unknown-selector) p

The unknown selector :unknown-selector gets skipped, and the rest of the list still works. A plain comma-separated list would reject the whole rule.
:is(section, :unknown-selector) p { background: #7c3aed; color: #fff; }

More examples

#
Nested lists in any combination
CSS
:is(ul, ol) :is(ul, ol) {
  margin: 4px 0;
  color: #a1a1aa;
  font-size: 14px;
}
Preview

One rule covers four cases: ul ul, ul ol, ol ul, and ol ol.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Use :is() to group states: .btn:is(:hover, :focus-visible). You get the mouse and keyboard-focus effect in one rule.
  • Watch the weight. A single ID in the list raises the specificity of the whole pseudo-class, even for elements that match through a class.
  • When writing base styles meant to be easy to override, swap :is() for :where().

Related entries

#

Spotted a mistake or missing an example? Write to us.