@variant for applying existing variants within custom utilities and nested CSS, and @custom-variant for defining entirely new variant modifiers.
@variant Directive
The@variant directive applies an existing variant to a block of CSS, creating the appropriate selector transformations.
Syntax
Basic Usage
Use@variant to apply conditional styling within custom utilities:
Using with @custom-variant
You can use@variant with custom variants defined via @custom-variant:
Nested @variant
@variant can be used within custom utilities or @custom-variant bodies:
@custom-variant Directive
The@custom-variant directive defines new variant modifiers that can be used throughout your CSS.
Syntax
Creating Custom Variants
Body-less Selector Variant
Define simple variants with comma-separated selectors:Multiple Selectors
Combine different selector types:At-Rule Variants
Create variants with media queries or other at-rules:Body Syntax with @slot
For more complex variants, use the body syntax with@slot as a placeholder:
@slot directive marks where the utility CSS should be inserted.
Nested Structure
Create complex selector structures:Variant Naming Rules
string
Variant names must:
- Start with a lowercase letter or number
- Contain only alphanumeric characters, hyphens, or underscores
- Not end with a hyphen or underscore
- Match the pattern:
/^@?[a-z0-9][a-zA-Z0-9_-]*(?<![_-])$/
Compound Variants
Use custom variants with compound modifiers likegroup and peer:
Variant Dependencies
Custom variants can use other custom variants:Restrictions
Top-level Only
Selector or Body Required
Cannot Mix Selector and Body
Valid Selectors
Compound Capabilities
Custom variants can specify what types of selectors they work with:- StyleRules: Works with CSS selectors (
:hover,.class, etc.) - AtRules: Works with at-rules (
@media,@supports, etc.) - Never: Cannot be used in compound variants
Output
The@custom-variant directive is removed from the final CSS. Only the transformed utility classes are output:
Related
- @utility - Create custom utilities
- @theme - Define theme variables
- Hover, Focus & Other States - Built-in variants