On this page
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
- > Modern Functional Pseudo-Classes (`:is`, `:where`, `:not`, `:has`)
- > Structural Pseudo-Classes
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)
:is(a, b): Matches any selector in the list; takes the highest specificity in its argument list.:where(a, b): Identical to:is()but always has zero specificity(0, 0, 0)โ ideal for overridable base styles.:has(...): Relational ("parent") selector โ styles an element based on its descendants or following siblings.
/* 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
- tuts/css/selectors
- tuts/css/selectors/basic_and_combinator_selectors
- tuts/css/selectors/pseudo_classes
- tuts/css/selectors/pseudo_elements
- tuts/css/tutorials/cascade_specificity_and_inheritance
- tuts/html/elements/forms_and_inputs
- tuts/html/tutorials/accessible_forms_and_validation
- tuts/web/tutorials/progressive_enhancement_and_accessibility
Related notes
- tuts/css/selectors Linked here; links to this page; shared tags: #css, #selectors, #web
- tuts/css/selectors/basic_and_combinator_selectors Linked here; links to this page; shared tags: #css, #selectors, #web
- tuts/css/selectors/pseudo_elements Linked here; links to this page; shared tags: #css, #selectors, #web
- tuts/css/tutorials/cascade_specificity_and_inheritance Linked here; links to this page; shared tags: #css, #web
- tuts/html/tutorials/accessible_forms_and_validation Linked here; links to this page; shared tags: #web
- tuts/html/elements/forms_and_inputs links to this page; shared tags: #web
- tuts/web/tutorials/progressive_enhancement_and_accessibility links to this page; shared tags: #web
- tuts/css shared tags: #css, #web
- tuts/css/at_rules shared tags: #css, #web
- tuts/css/at_rules/keyframes_and_fonts shared tags: #css, #web