liveBook · Manning

:is()

As explained in CSS in Depth, Second Edition

The CSS :is() pseudo-class matches an element if it matches at least one selector in its argument list and contributes the specificity of its most specific argument.

:is() groups alternative selectors inside one function, matching an element when it matches any argument. It replaces repetitive selector lists with a shorter, clearer rule. section 8.3

Styling related form controls

A contact form can apply the same padding, border, and radius to inputs, textareas, selects, and buttons with .contact-form :is(input, textarea, select, button). section 8.3

.contact-form :is(input, textarea, select, button) {
  padding: 5px 10px;
  border: 1px solid var(--brand-color);
  border-radius: 5px;
}

Compare the grouped selector with the equivalent repetitive selector list. section 8.3

Use :is() with these rules in mind:

  • Calculate specificity from the most specific argument, regardless of which argument matches; :is(input, #login-password) therefore carries ID-level specificity. section 8.3
  • Keep pseudo-elements out of the argument list, because selectors such as :is(div::before, div::after) are invalid; pseudo-classes remain valid. section 8.3
  • Use forgiving lists for progressive enhancement: unsupported arguments such as :user-invalid do not invalidate supported alternatives inside :is(). section 8.3.1
  • Account for nesting: replacing a parent selector with :is() can raise specificity and broaden which descendants match. section 8.4.2
input:is(.invalid, :user-invalid) {
  border: 1px solid red;
}

See how an unsupported validation pseudo-class can coexist with a supported fallback class. section 8.3.1

How CSS in Depth, Second Edition covers it

How deep it goes
Explains matching, specificity, invalid arguments, and nesting desugaring, including effects on targeted elements.
Examples
Uses contact-form controls, invalid-input fallbacks, and nested button, input, textarea, and select rules.
What you can do after
Apply it via CSS selectors to shorten grouped rules, preserve supported fallbacks, and reason about specificity.
How it connects
Connects selector lists with CSS specificity, progressive enhancement, CSS nesting, :where(), and cascade layers.

Also explained in

Compare how 2 books explain :is() →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.