Skip to content

Menu

CSS Selectors Cheat Sheet: :is, :where, :has, Specificity and @layer

Checked against CSS Selectors Selectors Level 4 and CSS Cascade Level 5 via MDN; specificity run with the specificity 1.0.0 npm package on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers who want to know why a rule loses, not just add another class until it wins. It covers Selectors Level 4 and CSS Cascading Level 5 as documented on MDN and the W3C on 28 November 2026. Specificity numbers were computed with the specificity 1.0.0 npm package and the matching rows were run with jsdom 29.1.1 on Node 24; rows marked untested were checked in the docs only.

When to use what

Keep selectors short and flat. One class per component part is easy to override; #app .sidebar ul li a is not. When you need to group alternatives, choose by how much weight you want the group to carry. :is() takes the weight of its heaviest argument, so :is(.a, #b) costs an ID even when it matched the class. :where() weighs nothing, which makes it the right wrapper for resets and library defaults that users should be able to override with a single class.

Use :has() when the style depends on what an element contains or what follows it: a form row with an invalid input, a card with an image. Anchor it to a specific class and constrain the inner selector with > or +; MDN warns that broad anchors such as body:has(...) are costly to evaluate.

When specificity fights get out of hand, reach for @layer before !important. Layers let you say "reset, then library, then components, then utilities" once, and later layers win regardless of selector weight. Unlayered CSS beats every layer, which is useful for quick overrides and surprising when you forget a file. Our modern selectors post and the nesting and cascade layers post go further, and the specificity calculator scores any selector you paste in.

The i flag makes the value comparison case-insensitive for ASCII; s forces case-sensitive.

.btn.btn-primary

Element with both classes; scores 0-2-0

a[href^="https"]

Links whose href starts with https; an attribute selector weighs the same as a class (0-1-1 here)

a[href$=".pdf" i]

Links ending in .pdf in any letter case; without i it misses .PDF

Run in jsdom: matched a.PDF only with the i flag

[class~="logo"]

Attribute holding a space-separated word list that includes logo

From the docs, not run by us
[lang|="en"]

Attribute equal to en or starting with en-, for language subcodes

From the docs, not run by us
a[href*="example"]

Attribute containing the substring anywhere

From the docs, not run by us

Combinators add nothing to specificity: * + * scores 0-0-0.

nav a

Descendant: any a nested anywhere inside a nav

From the docs, not run by us
ul > li

Child: only li elements that are direct children of the ul

From the docs, not run by us
h2 + p

Next sibling: the one p straight after an h2 with the same parent

From the docs, not run by us
h2 ~ p

Subsequent siblings: every p that comes after an h2 in the same parent

From the docs, not run by us
* + *

Any element that follows a sibling, the base of the "lobotomised owl" spacing rule; 0-0-0

:is() and :where() use forgiving lists: an unsupported selector inside them is skipped. :not() and plain comma lists are not forgiving; one bad selector drops the whole rule. :has() can't be nested inside :has() and can't contain pseudo-elements.

#app :is(.a, #b) p

Group alternatives; the group weighs as much as its heaviest member, so this scores 2-0-1

:where(#app) p

Same grouping with zero weight; scores 0-0-1 and any class can override it

button:not(.primary, #x)

Buttons that match neither selector; takes the heaviest argument's weight, 1-0-1

section:has(> img)

Sections with an img as a direct child; scores 0-0-2

jsdom: matched only the section with a direct img child

h1:has(+ h2)

An h1 immediately followed by an h2, the previous-sibling selector CSS lacked for years

body a:not(table a)

Links that are not inside a table; putting :not() on the ancestor instead would still match

From the docs, not run by us

Columns are ID - CLASS - TYPE and are compared left to right; any number of type selectors never beats one class. Pseudo-elements count as types. Inline style beats all normal author selectors.

ul li a

Three type selectors: 0-0-3

.card > h2::before

One class, one type, one pseudo-element: 0-1-2

#nav .item a:hover

One ID, a class plus a pseudo-class, one type: 1-2-1

input[type="text" i]

Type plus attribute: 0-1-1; the flag adds nothing

#myElement vs .a .b .c [id="myElement"]

1-0-0 beats 0-4-0 because the ID column is compared first

From the docs, not run by us

The cascade sorts by origin and importance, then context (shadow DOM), then element-attached styles, then layers, then specificity, then order of appearance. Specificity only matters once everything above it ties.

@layer reset, base, components, utilities;

Declare layer order up front; for normal rules a later layer beats an earlier one whatever the specificity

From the docs, not run by us
@layer components { .btn { } }

Add rules to a named layer; repeat the block anywhere to append more

From the docs, not run by us
@layer framework.theme { }

Add to a layer nested inside framework, named by its dotted path

From the docs, not run by us
@import url("lib.css") layer(lib);

Put a whole imported stylesheet into a layer

From the docs, not run by us
!important inside a layer

Order flips for important rules: an earlier layer's !important beats a later layer's, and unlayered !important loses to all layered ones

From the docs, not run by us
color: revert-layer

Roll the property back to what lower layers set, ignoring this layer

From the docs, not run by us

Worked example: Work out why a button style loses, then fix it with layers

A third-party stylesheet ships this, and your component rule doesn't apply:

#app .toolbar button:not(.link) { background: gray; }   /* 1-2-1 */
.btn-primary { background: blue; }                       /* 0-1-0 */

Score them column by column. The library selector has one ID, two class-level parts (.toolbar and .link, which :not() passes through) and one type, so 1-2-1. Your rule is 0-1-0. The ID column decides it before classes are even compared, so gray wins, and adding more classes to yours will never catch up.

Instead of !important, put the library in a layer:

@layer vendor, components;
@import url("vendor.css") layer(vendor);
@layer components {
  .btn-primary { background: blue; }
}

Now the comparison stops at the layer step. components is declared after vendor, so its normal declarations win, and specificity between the two rules is never consulted. If the vendor file used !important on that background, the order would flip and the vendor would win again; in that case add your own !important in an earlier layer, or ask the library to stop shipping important rules. Note the @layer statement must come before the @import you want to order, since @import rules have to sit near the top of the file.

Common mistakes

  • Wrapping a reset in :is(#app) * and then needing IDs everywhere to override it.

    :is() keeps the ID weight. Use :where(#app) * so the reset scores zero.

  • Adding one unsupported pseudo-class to a comma list and losing the whole rule.

    Plain lists aren't forgiving. Put the alternatives inside :is() or :where(), which skip the unknown part.

  • Writing div :not(.special) a and still styling links inside .special.

    Every wrapper between them matches :not(.special). Put the negation on the target: a:not(.special a).

  • Moving framework CSS into a layer and finding your unlayered resets now beat everything.

    Unlayered rules win over all layers for normal declarations. Put your own CSS in layers too, or accept that ordering.

  • Using !important in a late utilities layer and expecting it to beat an early layer's !important.

    Important declarations reverse layer order, so the earliest layer wins. Avoid !important inside layers unless you mean that.

  • Anchoring :has() on body or * for a small component style.

    Anchor it to the component's class and use > or + inside, as MDN advises for performance.