skip to content

In Tailwind CSS v4, how do you apply a variant such as `dark` or `hover` inside your own CSS rule, and what does it compile to?

level: middleimportance: nice to knowfreq 7%

answer

  1. same names as the prefixes
  2. a block inside the rule
  3. dark follows the project's definition
  4. hover gated on hover-capable devices
  5. colon and comma forms, 4.3

basics

~20 s

In Tailwind CSS v4, @variant dark { … } inside a rule wraps that block in the variant's condition: by default dark becomes a prefers-color-scheme media query and hover becomes &:hover inside @media (hover: hover). Stacked and comma-separated lists arrived in 4.3.0.

solid answer

~40 s

`@variant` lets hand-written CSS use the same variants as the markup, so a state is defined once by the project rather than by a selector someone typed. Inside `.permit-card`, `@variant dark { background: … }` compiles by default to a nested `@media (prefers-color-scheme: dark)` block — or to the app's class-based selector if the main stylesheet redefined `dark`. `@variant hover` becomes `&:hover` wrapped in `@media (hover: hover)`, matching what `hover:` does in v4. Since 4.3.0 you can stack variants, `@variant hover:focus`, so both must hold, or list them, `@variant hover, focus`, so either applies. In a separately compiled component style block it needs `@reference` to see the app's custom variants.

code

css · 17 lines
css
.permit-card {
  background: var(--color-white);

  @variant dark {
    background: var(--color-slate-900);
  }

  /* Tailwind CSS 4.3.0+: hovered AND focused */
  @variant hover:focus {
    outline: 2px solid var(--color-civic-600);
  }

  /* Tailwind CSS 4.3.0+: hovered OR focused */
  @variant hover, focus {
    background: var(--color-civic-50);
  }
}

go deeper

for a junior

Recall that @variant applies a Tailwind variant such as dark or hover to a block of declarations inside your own CSS rule.

for a middle

Explain what @variant dark and @variant hover compile to by default, and why the hover output is wrapped in a media query for hover-capable devices.

for a senior

Argue for @variant over hand-written media queries because it follows the project's own variant definitions, and know that stacked and comma forms need 4.3.0 and component blocks need @reference.

for a principal

Set guidance that states in hand-written CSS always go through the project's variants, so a dark-mode or breakpoint change stays a one-place edit across markup and stylesheets.

## Why `@variant` exists Markup applies a variant with a prefix: `dark:bg-slate-900`. Hand-written CSS — a `.permit-card` rule the CMS output relies on, an override for a third-party widget — has no prefix to add. Before v4 the usual answer was to repeat the media query or selector by hand, which meant the CSS and the markup could disagree about what `dark` or `md` meant. In **Tailwind CSS v4**, the `@variant` directive brings the same variants into a rule: you write the variant's name and a block of declarations, and Tailwind wraps the block in whatever condition that variant means in this project. ```css .permit-card { background: white; @variant dark { background: black; } } ``` ## What it compiles to | Inside `.permit-card { … }` | Compiled, before nesting is flattened | |---|---| | `@variant dark { … }` | `@media (prefers-color-scheme: dark) { … }` with the default `dark` variant | | `@variant hover { … }` | `&:hover { @media (hover: hover) { … } }` | | `@variant hover:focus { … }` (4.3.0+) | `&:hover { @media (hover: hover) { &:focus { … } } }` — both must hold | | `@variant hover, focus { … }` (4.3.0+) | a `&:hover` block inside `@media (hover: hover)`, plus a separate `&:focus` block — either applies | Tailwind then flattens the nested output into plain selectors and media queries that every supported browser understands. ## Variants follow the project's own definitions The reason to prefer `@variant` over writing the condition by hand is that it uses **the project's definition** of each variant: - If the main stylesheet switches `dark` to follow a `.dark` class (a theme decision made elsewhere), `@variant dark` compiles to that selector instead of the media query. Hand-written CSS and `dark:` utilities then switch together, while a typed `@media (prefers-color-scheme: dark)` would keep following the OS. - `@variant hover` carries the same `(hover: hover)` gate that `hover:` gets in v4 markup, so touch screens do not get sticky hover styles from your custom CSS either. - Breakpoint variants such as `@variant md` use the project's breakpoint values, so a breakpoint change reaches hand-written rules too. - Custom variants the project defines work the same way as built-in ones. ## Stacking and lists in Tailwind CSS 4.3 Before 4.3.0, `@variant` accepted a single variant. Tailwind CSS 4.3.0 added two forms: 1. **Stacked** — `@variant hover:focus`, written with a colon exactly as in markup and applied left to right: the block applies only when the element is hovered **and** focused. 2. **Compound** — `@variant hover, focus`, comma-separated: the same declarations are emitted once per variant, so the block applies on hover **or** on focus. On an older 4.x release, write separate `@variant` blocks for each state instead of a list. ## Inside component style blocks A Vue or Svelte `<style>` block, or a CSS Module, is compiled separately from the main stylesheet. `@variant` works there, but only sees the app's custom variants after `@reference "../app.css"`; without it, `dark` falls back to the built-in media-query definition and the component quietly ignores the app's dark-mode class. ## Common mistakes - **Typing the condition by hand.** A hand-written `@media (prefers-color-scheme: dark)` or a bare `&:hover` drifts from the project's definitions the first time someone changes them. - **Assuming the dark-mode default.** Whether `dark` means the media query or a class is decided once, in the theme configuration; `@variant dark` simply follows it. - **Using a 4.3 list on an older release.** `@variant hover:focus` or `@variant hover, focus` needs 4.3.0 or later; check the installed version before relying on it. - **Forgetting the separately compiled blocks.** A component's `<style>` block is a different compile; without `@reference` it cannot see custom variants. - **Expecting a class.** `@variant` produces no class for the markup; if markup needs `md:` on something, it must be a utility. ## `@variant` is not `@custom-variant` - `@variant name { … }` **uses** an existing variant, inside a rule. - `@custom-variant name (…);` **defines** a new variant at the top level of the stylesheet — that belongs with extending Tailwind, and the dark-mode override with theme configuration.

  • In Tailwind CSS 4.3, what does `@variant hover, focus { … }` compile to, and how does it differ from `@variant hover:focus { … }`?
    The comma form emits the block once per variant — a `&:hover` rule inside `@media (hover: hover)` and a separate `&:focus` rule — so either state applies it. The colon form stacks them left to right, `&:hover` then `&:focus` inside it, so the element must be hovered and focused at once. Both forms need 4.3.0 or later.
  • In a Tailwind CSS v4 project, why prefer `@variant dark` over writing `@media (prefers-color-scheme: dark)` by hand?
    `@variant dark` compiles to whatever `dark` means in the project. If dark mode is switched to a `.dark` class, the hand-written media query keeps following the OS while every `dark:` utility follows the class, and the page ends up half-themed. With `@variant`, one definition drives both.

  • @variant defines a new variant, the same job as @custom-variant
  • @variant hover compiles to a bare :hover that also fires on touch taps
  • @variant dark always means the OS colour scheme, whatever the project configures
  • Stacked @variant hover:focus works in every v4 release
  • @variant only works in the main stylesheet, never in component style blocks

Assumes Tailwind CSS v4 (checked against 4.3.3); @variant in custom CSS is a v4 directive, and its stacked and comma-separated forms arrived in 4.3.0.