Frontend & CSS ReferenceUpdated: 2026-11-06

CSS Selectors, Combinators, Pseudo-Classes & Specificity Reference

Master modern CSS selectors including :has(), :is(), :where(), :nth-child(An+B), attribute matching, and cascade scoring.

CSS selectors define which DOM elements styling rules apply to. With modern CSS features like the relational :has() pseudo-class, :is(), and advanced attribute matching, writing clean stylesheets without JavaScript class toggling is more powerful than ever.

Combinators & Hierarchical Selectors

Relationships between parent, child, and sibling elements.

PatternSelector TypeExplanation & Target
`div p`Descendant selectorMatches any `<p>` element nested inside a `<div>`, regardless of depth.
`ul > li`Direct child selectorMatches only `<li>` elements that are direct children of `<ul>`.
`h2 + p`Adjacent sibling selectorMatches `<p>` immediately preceded by an `<h2>` element.
`h2 ~ p`General sibling selectorMatches any `<p>` that shares the same parent and appears after `<h2>`.

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

Next-generation relational and grouping selectors widely supported in all evergreen browsers.

SelectorSpecificity ImpactUse Case & Example
`article:has(img)`Inherits inner selectorParent selector! Styles `<article>` only if it contains an `<img>` tag.
`:is(h1, h2, h3)`Takes specificity of most specific argumentReduces repetition when styling multiple elements with common children.
`:where(h1, h2, h3)`ALWAYS ZERO specificity (0,0,0)Perfect for CSS resets and base styles that need easy overriding.

Specificity Calculation Formula

How browsers resolve competing stylesheet declarations.

CategoryWeight ScoreExamples
Inline styles1,0,0,0`style="color: red;"`
ID selectors0,1,0,0`#header`, `#sidebar`
Classes, attributes, pseudo-classes0,0,1,0`.btn`, `[type="text"]`, `:hover`, `:first-child`
Elements, pseudo-elements0,0,0,1`div`, `p`, `::before`, `::after`

Frequently Asked Questions

What is the "parent selector" in CSS?

The `:has()` pseudo-class acts as the long-awaited CSS parent selector. For example, `figure:has(figcaption)` allows you to apply styling to `<figure>` only when a caption is present.

What is the difference between :is() and :where()?

Both take a list of selectors to match, but `:where()` always assigns 0 specificity to the selector list, making it trivially easy to override without `!important`.