Modern CSS Selectors: Nesting, :is(), :where(), @scope
Selectors have grown from a flat list of matching rules into a small language. Along the way, two different problems got solved: how to write selectors that group and repeat less (:is(), :where(), :has()), and how to structure whole stylesheets so component styles stay local without a naming convention holding them together (& nesting, @scope). A third idea â user-defined selector aliases via @custom-selector â was specified, prototyped, and quietly abandoned; understanding why it died is useful, because the jobs it was meant to do are now covered by better tools. This lesson treats selectors as an organizational system: what nesting actually compiles to, how specificity behaves inside it, when @scope beats a BEM suffix, and how attribute selectors keep growing as a styling API.
What nesting actually compiles to
Native CSS nesting lets rules nest inside rules, and the & parent selector splices the outer selector into any position. Without &, a nested bare selector is an implicit descendant:
.card {
background: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 14px;
.title {
font-size: 1.1rem;
font-weight: 700;
margin: 0;
}
.actions {
display: flex;
gap: 0.5rem;
padding: 0.75rem 1rem;
}
&:hover {
box-shadow: 0 8px 24px #0f172a14;
}
&:has(:focus-visible) {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
> .media {
border-start-start-radius: 14px;
border-start-end-radius: 14px;
overflow: clip;
}
&--featured {
border-color: #2563eb;
}
}
.title inside .card means .card .title â a descendant, exactly as you would write it flat. & allows anything: compound additions (&:hover, &.is-open), prefixes (& .title when you want explicitness), and substitutions where & is replaced by the entire parent selector list (:is() semantics when the parent list has multiple entries). The detail most people miss: when the parent selector is a list, & matches any of them, and in a compound position :is(a, b) semantics apply â which quietly changes specificity compared to hand-written alternatives. Nesting solves file organization, not selector naming; keep depth to two or three levels before the output becomes harder to reason about than flat rules.
:is() and :where() as selector functions
Both accept a selector list and match any of its arguments; they differ in what they do to specificity. :is() takes the specificity of its most specific argument, :where() is always zero. That single difference governs where each belongs:
| Construct | Specificity | Read as | Use for |
|---|---|---|---|
| h1, h2, h3 (comma list) | Each rule keeps its own | Alternate selectors | Small alternates with no shared extras |
| :is(h1, h2, h3) | Highest argument's weight | Any of these, as one unit | Base styles that must beat defaults |
| :where(h1, h2, h3) | Zero | Any of these, weightless | Resets, design-system defaults |
| :not(a, b) | Highest argument's weight | Anything else | Exclusions that still count as weight |
:where(.prose) :where(h1, h2, h3, h4) {
line-height: 1.2;
font-weight: 700;
text-wrap: balance;
margin-block: 1.4em 0.5em;
}
:where(.prose) :where(p) {
margin-block: 0 1em;
}
:where(.prose) :where(ul, ol) {
padding-inline-start: 1.4em;
}
.prose-title {
font-weight: 800;
}
Because the entire :where() chain contributes zero specificity, a single-class author rule like .prose-title overrides the defaults without !important or escalation tricks. Convert the same defaults to :is() and they suddenly outrank one-class selectors â a common reason designs become impossible to override. The general strategy: library and reset code in :where(), component code in plain selectors, :is() reserved for grouping inside component code where you want the weight of the most specific argument.
@scope: locality without naming conventions
Descendant selectors couple every ancestor: styling .card .title means the rule fires anywhere a .title sits inside any .card, and a nested card can accidentally inherit styles meant for its parent's children. @scope draws an explicit boundary: a root selector defines the scope's outer edge, an optional second selector defines the inner edge (the part excluded from scoping), and rules inside match only within that region:
@scope (.accordion) to (.accordion-item) {
.heading {
font-weight: 600;
padding: 0.85rem 1rem;
border-block-end: 1px solid #e2e8f0;
}
.panel {
padding: 1rem;
color: #475569;
}
}
Scoping changes matching, not just organization: .heading inside the scope root matches; a .heading outside does not; and the exclusion boundary stops nested instances of the component from being re-styled by their ancestor's rules â the exact failure that forced defensive suffixes in BEM and CSS Modules. :scope inside the block refers to the boundary element itself, and & still refers to the scoped rule. Because scope proximity participates in the cascade â closer scopes win â layered component styles behave predictably when nested. Support is the newest of the features covered here, so treat @scope as progressive enhancement layered over namespaced class names, not yet a total replacement for them.
Why @custom-selector died â and what replaced it
The original proposal for this lesson's topic was @custom-selector :--heading h1, h2, h3; â define a custom pseudo-class that expands wherever written, then style :--heading { ... }. It addressed real repetition: heading groups, interactive-element groups, shared component lists. It never shipped in browsers, for reasons worth internalizing: aliases hide their expansion (you cannot grep :--heading and see what it matches), they interact badly with specificity (what weight does an alias carry?), and they add an indirection layer to a language whose main virtue is that a selector means what it says. The specification work moved on to features with cleaner semantics. Today the aliasing jobs are covered by :is() and :where() for matching, by nesting plus & for organization, by custom properties for shared values, and by @scope for locality. None of them let you invent new pseudo-classes, but collectively they remove most of the motivation for doing so.
Attribute selectors as a styling API
While aliases stalled, attribute selectors quietly became more powerful â and they are the piece of the language that grows with every HTML pattern. Matching by presence ([hidden]), exact value ([data-state="open"]), prefixes ([data-icon^="chevron-"]), suffixes (a[href$=".pdf"]), substrings ([class*="is-"]), and whitespace lists ([aria-selected~="true"]) gives you an entire state vocabulary driven by markup:
[data-state="open"] .chevron {
transform: rotate(90deg);
}
[data-theme="dark"] {
--surface: #0f172a;
--text: #e2e8f0;
}
a[href$=".pdf"]::after {
content: " (PDF)";
font-size: 0.8em;
color: #64748b;
}
[aria-disabled="true"] {
opacity: 0.55;
cursor: not-allowed;
}
button:not([data-icon=""]) .label {
margin-inline-start: 0.5rem;
}
This is the styling half of the data-attributes pattern: JavaScript toggles data-state, data-theme, or aria-*, and CSS reacts â no derived class names like is-open cluttering the tree, and the attributes remain queryable by script and meaningful to assistive technology. Combined with :has() for structural reactions and nesting for layout of the file, the modern selector language covers the organizational ground that preprocessor mixins and custom pseudo-aliases used to occupy.
@layer: ordering whole stylesheets
Selector craft organizes rules; @layer organizes priority between groups of rules. Declaring @layer base, components, utilities; establishes an explicit cascade order â later layers beat earlier ones regardless of selector specificity â and every rule assigned to a layer participates in that order while unlayered styles stay on top of all of them. This inverts the usual specificity arms race: a base layer containing resets and typography defaults can use weighty selectors freely, because no component-layer rule can lose to it, and utility overrides remain possible without !important:
@layer base, components, utilities;
@layer base {
:where(h1, h2, h3) { line-height: 1.2; margin-block: 1.2em 0.4em; }
:where(p) { margin-block: 0 1em; }
}
@layer components {
.card .title { font-weight: 700; font-size: 1.15rem; }
}
@layer utilities {
.text-center { text-align: center; }
}
Layer names are declared once in order, then reused in any number of @layer blocks â including inside @media and @supports rules, which is how responsive overrides join the same priority scheme instead of fighting specificity at query time. Layers interact with the rest of this lesson cleanly: :where() inside a base layer contributes zero specificity on top of the layer's own position, :is() grouping inside a components layer keeps weight where it is needed, and @scope still controls matching locally within whichever layer its rules live in. Adoption order matters for existing stylesheets â unlayered legacy rules outrank every layer until they are migrated â so introduce layers at the architectural boundary (reset, theme, component, utility) rather than per-file.
Common Mistakes
Overrides stop working after moving defaults into :is()
Fix: :is() carries the highest specificity of its arguments â move design defaults into :where() so zero-specificity rules can still be overridden by ordinary component selectors
Deeply nested rules become unreadable and unexpectedly specific
Fix: Cap nesting at two or three levels; flatten the rest with plain selectors, or restructure with & only where the compound relationship is the point
Scoped styles leak into nested instances of the same component
Fix: Define the @scope exclusion boundary (the second selector) so inner instances stop matching the ancestor's rules
Waiting for @custom-selector to deduplicate heading lists
Fix: Use :is(h1, h2, h3) or a :where() group â the alias feature never shipped and will not
Best Practices
- Put resets and design-system defaults in :where() to keep the cascade overridable
- Prefer data attributes over derived utility classes for state driven by script
- Keep component nesting shallow; let & express compound conditions, not file hierarchy alone
- Layer @scope over namespaced classes while browser support matures
- Group selectors with :is() when the group must retain weight, and with :where() when it must not
Interview Questions
Q1: What does & refer to in nested CSS, and what happens when the parent is a list?
& refers to the parent selector. Where the parent is a list, & expands as :is() semantics â the compound position matches any parent, and specificity takes the most specific parent in that position. Writing a bare nested selector without & implies a descendant relationship instead.
Q2: Give a rule of thumb for choosing between :is() and :where().
Use :where() for defaults and library code that author styles must be able to override without !important, because it contributes zero specificity. Use :is() when the grouped selector should still count with the weight of its most specific argument, such as component-level variants.
Q3: Why did @custom-selector never become usable CSS?
It was specified as an editor's draft but never implemented in browsers. Aliases obscure what a selector expands to, muddy specificity, and add indirection to a language where direct readability is the point. The use cases were absorbed by :is/:where grouping, nesting, custom properties, and @scope instead.
Key Takeaways
- Native nesting with & compiles to the selectors you would have written flat; keep depth shallow
- :is() keeps specificity, :where() zeroes it â defaults belong in :where()
- @scope defines explicit boundaries so nested instances stop inheriting ancestor component rules
- @custom-selector was abandoned; heading groups, shared lists, and locality are covered by :is/:where, nesting, and @scope
- Attribute selectors on data-* and aria-* give CSS a state API that script toggles without derived classes