--- name: core-variants description: Using variants to apply utilities conditionally based on states, pseudo-classes, and media queries --- # Variants Variants let you apply utility classes conditionally based on states, pseudo-classes, pseudo-elements, media queries, and more. ## Overview Add a variant prefix to any utility class to apply it conditionally: ```html ``` Variants can be stacked to target specific situations: ```html ``` ## Pseudo-Class Variants ### Interactive States ```html ``` Common interactive variants: - `hover:` - `:hover` pseudo-class - `focus:` - `:focus` pseudo-class - `active:` - `:active` pseudo-class - `focus-visible:` - `:focus-visible` - `focus-within:` - `:focus-within` - `visited:` - `:visited` ### Structural Variants ```html ``` Common structural variants: - `first:` - `:first-child` - `last:` - `:last-child` - `odd:` - `:nth-child(odd)` - `even:` - `:nth-child(even)` - `only:` - `:only-child` ### Form States ```html ``` Common form variants: - `required:` - `:required` - `optional:` - `:optional` - `invalid:` - `:invalid` - `valid:` - `:valid` - `disabled:` - `:disabled` - `enabled:` - `:enabled` - `checked:` - `:checked` ## Pseudo-Element Variants ```html ``` Common pseudo-element variants: - `before:` - `::before` - `after:` - `::after` - `placeholder:` - `::placeholder` - `selection:` - `::selection` - `first-line:` - `::first-line` - `first-letter:` - `::first-letter` ## Media Query Variants ### Responsive Variants ```html
Responsive text
``` ### Dark Mode ```html
Content
``` By default uses `prefers-color-scheme`, but can be customized to use a class or data attribute. ### Reduced Motion ```html
Animated content
``` ## Attribute Selector Variants ```html
Content
Details
``` ## Arbitrary Variants Use arbitrary variants for custom selectors: ```html ``` ## Child Selector Variants ```html

Paragraph 1

Paragraph 2

``` ## Key Points - Variants prefix utilities to apply them conditionally - Variants can be stacked: `dark:md:hover:bg-blue-600` - Use variants for states, pseudo-classes, media queries, and more - Arbitrary variants enable custom selector patterns - Child selector variants target descendant elements