On this page
  1. Pseudo-Classes
  2. Contents
  3. Interaction & Focus
  4. Modern Functional Pseudo-Classes (:is, :where, :not, :has)
  5. Structural Pseudo-Classes

Pseudo-Classes

Prev: Basic & Combinator Selectors | Up: selectors | Next: Pseudo-Elements
Rel: Cascade, Specificity & Inheritance, Accessible Forms & Validation
Ref: MDN Web Docs โ€” Pseudo-classes
Tags: #public #web #css #selectors


Contents


Interaction & Focus

Prefer :focus-visible over :focus so keyboard users always see clear focus rings without showing outlines on mouse clicks:

button:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

Modern Functional Pseudo-Classes (:is, :where, :not, :has)

/* Zero-specificity reset */
:where(h1, h2, h3) { margin-block: 0.5em; }

/* Style a card only when it contains an image */
.card:has(> img) {
  grid-template-rows: auto 1fr;
}

Structural Pseudo-Classes

tr:nth-child(even) { background: rgba(0, 0, 0, 0.03); }
li:first-child     { margin-top: 0; }
li:last-child      { margin-bottom: 0; }

Linked from

Related notes