Skip to main content

addVariant()

Register a static variant that modifies how utilities are applied.
string
required
The variant name (must be alphanumeric, lowercase, with dashes or underscores)
string | string[] | CssInJs
required
Selector transformation, media query, or CSS-in-JS object defining the variant behavior

String Selector

Use & as a placeholder for the utility selector:
Compiles to:

Array of Selectors

Object Syntax with @slot

Use @slot to mark where the utility styles should be inserted:

Media Queries

Complex Variants with Nesting

At-Rules and Supports

Advanced Example

matchVariant()

Register a dynamic variant that accepts values.
string
required
The variant name
(value: string, extra: { modifier: string | null }) => string | string[]
required
Function that returns selector transformation(s) based on the value
object
Configuration for values and sorting
Record<string, T>
Named values that can be used with the variant
(a, b) => number
Custom sorting function for variant order

Basic Example

With Named Values

Data Attributes

ARIA States

Screen Variants

With Modifiers

Group and Peer Variants

In v4, group-* and peer-* variants compound automatically, so you don’t need to use :merge() anymore.

Custom Sorting

Variant Compounding

Variants automatically work with group-* and peer-* patterns:

Stacking Variants

Variants can be stacked in any order:
Variants are applied in a specific order for consistent behavior. Custom variants follow Tailwind’s variant ordering system.

Validation

Variant names must:
  • Start with a lowercase letter or number
  • Contain only alphanumeric characters, dashes, or underscores
  • Not include :merge() in v4 (automatically handled)
Invalid examples:

Migration from v3

In v4, the :merge() pseudo-class is no longer needed. Variants automatically compound:
All built-in Tailwind variants (hover, focus, dark, responsive, etc.) work automatically with custom utilities. You only need to register custom variants for new interaction patterns.