Skip to main content
Variants in Tailwind CSS allow you to apply styles conditionally based on state, viewport size, or other conditions. Tailwind v4 includes a comprehensive set of built-in variants and provides APIs for creating custom ones.

Built-in Variants

Tailwind includes many variants out of the box:

Pseudo-class Variants

Pseudo-element Variants

Media Query Variants

Variant Categories

State Variants

pseudo-class
Applied on hover (with @media (hover: hover) wrapper)
pseudo-class
Applied when element has focus
pseudo-class
Applied when element is being activated
pseudo-class
Applied to visited links
pseudo-class
Applied when element is disabled

Form Variants

Positional Variants

Group & Peer Variants

Apply styles based on parent or sibling state:

Responsive Variants

The default breakpoints:
Usage:

Custom Variants with Plugins

Create custom variants using the plugin API:

Static Variant

Add a simple static variant:
tailwind.config.ts
Usage:

Functional Variant

Create variants that accept values:
Usage:

Compound Variants

Create variants that modify other variants:

Functional Variants

Some variants accept arbitrary values:

Stacking Variants

Combine multiple variants:

Variant Order

Variants are applied in a specific order (roughly):
  1. Responsive variants (sm:, md:, etc.)
  2. State variants (hover:, focus:, etc.)
  3. Compound variants (group-*:, peer-*:)
  4. Pseudo-elements (before:, after:, etc.)
The order of variants in your class name doesn’t matter - Tailwind handles ordering automatically based on CSS specificity rules.

Advanced Variant Examples

Custom Dark Mode

Implement a custom dark mode strategy:

RTL Support

Usage:

Container Queries

Use container query variants:

Not Variant

Negate other variants:

Variant Modifiers

Some variants support modifiers:

Best Practices

  1. Use semantic variants - hover:, focus:, etc. are more maintainable than arbitrary selectors
  2. Leverage responsive variants - Build mobile-first with progressive enhancement
  3. Group related states - Use group and peer for parent/sibling relationships
  4. Be mindful of specificity - More variants = higher specificity
Stacking too many variants can create overly specific selectors that are hard to override. Consider extracting complex variant combinations into component classes.