skip to content

In Tailwind CSS v4, in which order are stacked variants such as `*:first:font-semibold` applied, and what CSS does a stacked class compile to?

level: seniorimportance: should knowfreq 8%

answer

  1. reads like nested CSS
  2. leftmost variant is the outer wrapper
  3. v3 read the other way
  4. order matters when the subject changes
  5. the upgrade tool reorders combinators

basics

~20 s

Tailwind CSS v4 applies stacked variants left to right, like nested CSS: the leftmost variant is the outermost condition. v3 read them right to left, so order-sensitive classes such as first:*: had to be reversed to *:first: in v4.

solid answer

~40 s

In Tailwind CSS v4 a stacked class such as `*:first:font-semibold` reads **left to right, like CSS nesting**: the leftmost variant becomes the outermost wrapper and each variant to its right nests inside it. So `*:first:` compiles to a rule nested as `:is(& > *) { &:first-child { … } }`, which matches direct children that are also first children. v3 applied stacked variants right to left, so the same intent was written `first:*:`; in v4 that spelling means "children of an element that is itself a first child". Order only changes the meaning when a variant moves the subject of the selector (`*`, `**`, `group-*`, `peer-*`, `in-*`, pseudo-elements); pure conditions such as `dark:`, `md:` and `hover:` just combine. Compound variants and v4's newer variants stack the same way: `hover:not-focus:`, `starting:open:`.

code

html · 12 lines
html
<!-- v3 wrote this stack as first:*:font-semibold -->
<ul class="*:py-2 *:first:font-semibold">
  <li>Arborio rice</li>
  <li>Lemon</li>
</ul>

<!-- conditions only: order does not change the meaning -->
<a class="dark:md:hover:underline" href="#method">Jump to method</a>

<!-- compound and arbitrary variants read left to right too -->
<button class="bg-indigo-600 hover:not-focus:bg-indigo-700">Save recipe</button>
<li class="[&.is-dragging]:active:cursor-grabbing">Step 3</li>

go deeper

for a junior

Recall that several variants can prefix one utility and that, in v4, you read them left to right like nested CSS.

for a middle

Explain how a stacked class compiles to nested rules with the leftmost variant outermost, and which variants change the selected element.

for a senior

Audit a v3 to v4 migration for silent stacking regressions in :, group-, peer-* and pseudo-element stacks, and explain the *: specificity trap from the emitted order.

for a principal

Decide how a codebase guards against order-sensitive stacks: run the upgrade tool once, then rely on review rules or linting rather than re-running migrations.

## Stacking reads like nested CSS Any number of **variants** can prefix one utility, and Tailwind CSS v4 applies them **left to right**, in the same direction you would write nested CSS. The leftmost variant becomes the **outermost** wrapper and each variant to its right nests one level deeper. Internally the compiler wraps the rule with the rightmost variant first and works outward, which is what leaves the leftmost one on the outside. A recipe page's ingredient list makes this concrete: ```html <ul class="*:py-2 *:first:font-semibold"> <li>Arborio rice</li> <li>Lemon</li> </ul> ``` `*:first:font-semibold` compiles to a nested rule: ```css .\*\:first\:font-semibold { :is(& > *) { &:first-child { /* the utility's declarations */ } } } ``` Flattened, that is `:is(.class > *):first-child`: a **direct child** that is also a **first child**, so only the first ingredient is bold. ## What changed from v3, and why it matters Tailwind CSS v3 applied stacked variants **right to left**. The same intent was written `first:*:font-semibold`, and the v4 upgrade guide tells you to reverse order-sensitive stacks. In v4, `first:*:font-semibold` is still valid, but it now means "the children of an element that is itself a first child". Nothing errors; the style silently lands somewhere else. The official upgrade tool knows this. When it migrates a v3 project, its variant-order step reverses a stack only when one of the variants **adds a combinator** (a space, `>`, `~`, `+`), keeps at-rule variants such as `dark:` at the front and pseudo-element variants at the end, and it deliberately skips the step on a project that is already v4, so the order never flip-flops on a second run. ## Why the direction changed The upgrade guide gives the reason in one line: v4 applies variants left to right "to look more like CSS syntax". Nested CSS is written outside-in, a media query around a rule around a `&:hover` block, and a v4 class now reads the same way: `md:hover:` says "at `md`, on hover", in the order you would nest it. The cost is a one-time migration for stacks where order carries meaning, and the guide expects most projects to have few of them, mostly child-selector stacks. When in doubt, inspect the generated rule in the browser's developer tools: the nesting shows exactly which element the selector lands on. ## When order changes the meaning | Kind of variant | Examples | Does order matter? | |---|---|---| | At-rule conditions | `dark:`, `md:`, `@sm:`, `motion-safe:`, `starting:` | no, they combine | | Pseudo-classes on the same element | `hover:`, `focus-visible:`, `open:`, `disabled:` | rarely, they combine | | Variants that move the subject | `*:`, `**:`, `group-*`, `peer-*`, `in-*` | yes | | Pseudo-elements | `before:`, `after:`, `placeholder:` | yes, keep them rightmost | `dark:md:hover:underline` therefore means "in dark mode, at `md` and up, on hover", whichever way you order those three. `hover:before:` gives `&:hover { &::before { … } }`, a pseudo-element of a hovered element, which is what you want; reversing it asks for a hovered pseudo-element. ## Compound and newer variants stack the same way v4's compound variants, which take another variant as their argument, and its newer static variants read left to right too: - `hover:not-focus:bg-indigo-700`: hovered **and** not focused, since `not-*` negates the variant after it; - `in-focus:opacity-100`: any ancestor is focused, compiled as `:where(*:focus) &`, with no `group` marker needed; - `starting:open:opacity-0`: inside `@starting-style`, while the element is open (`&:is([open], :popover-open, :open)`), the first-frame value for an entry transition; - arbitrary variants stack with built-ins: `[&.is-dragging]:active:cursor-grabbing` nests `&.is-dragging { &:active { … } }`. ## A specificity trap with `*:` Child-selector variants like `*:bg-sky-50` are emitted **after** plain utilities and have the **same specificity**, so a plain `bg-red-50` written on one child cannot override them. When one child must differ, stop styling the children from the parent and put the utilities on each child instead; the docs' own advice is to prefer utilities on the children and keep `*:` for markup you do not control. ## How to reason about any stacked class 1. Read the variants left to right as nesting levels. 2. Ask whether any of them changes **which element** the rule applies to. 3. If one does, the order is the meaning; write it the way you would nest the CSS. 4. When migrating a v3 project, check every stack that contains `*`, `group-*`, `peer-*` or a pseudo-element.

  • After upgrading to v4, `first:*:pt-0` on a list no longer removes the first item's top padding. What happened?
    v4 applies variants left to right, so `first:*:` now means the children of a list that is itself a first child. In v3 the same class meant the first child among the children. Reverse it to `*:first:pt-0`. The upgrade tool reverses such stacks when migrating from v3 because `*` adds a combinator.
  • Does `md:dark:hover:underline` behave differently from `dark:hover:md:underline`?
    No. `md:` and `dark:` are at-rule conditions and `hover:` is a pseudo-class on the same element, so every order nests the same conditions around the same element and they simply combine. Order changes meaning only when a variant moves the subject, such as `*:`, `group-*`, `peer-*`, `in-*` or a pseudo-element.
  • Why can a plain `bg-red-50` on one list item not override `*:bg-sky-50` on the list?
    The `*:` rule is emitted after plain utilities and has the same specificity, so it wins the cascade for every direct child. Move the background utilities onto the children themselves, keeping `*:` for children whose markup you do not control.

  • Tailwind CSS v4 applies stacked variants right to left, as v3 did
  • the order of stacked variants never changes what a class matches
  • a reversed v3 stack fails the build in v4 instead of matching elsewhere
  • dark:md: and md:dark: compile to different conditions
  • a utility on a child always overrides the parent's *: variant

Assumes Tailwind CSS v4 (checked at 4.3.3); v4 applies stacked variants left to right, where v3 applied them right to left.