Overview
Uniwind provides two approaches for creating theme-aware styles: using theme variant prefixes (likedark:) or defining CSS variables with @layer theme. Each approach has its use cases and benefits.
Approach 1: Theme Variant Prefixes
The simplest way to style components based on themes is using thedark: variant prefix.
Basic Usage
By default, styles apply to all themes:dark: prefix:
Multiple Theme-Specific Styles
You can combine multiple theme-aware utilities:When to Use Theme Variant Prefixes
Pros:- Explicit and easy to understand
- No setup required
- Full control over each theme’s appearance
- Works great for small apps or prototypes
- Verbose for larger apps
- Requires repeating
dark:prefix for many properties - Difficult to maintain consistent colors across components
- Doesn’t scale well to 3+ themes
Approach 2: CSS Variables with @layer theme
For larger applications, defining theme-specific CSS variables provides a more scalable and maintainable solution.Setting Up CSS Variables
Define your theme variables inglobal.css:
global.css
Using CSS Variables
Reference your variables directly in components:bg-background automatically resolves to #ffffff in light theme and #000000 in dark theme. No dark: prefix needed!When to Use CSS Variables
Pros:- Clean, maintainable code
- Consistent colors across the entire app
- Easy to update themes in one place
- Scales well to any number of themes
- Semantic naming improves code readability
- Requires initial setup
- Less explicit than variant prefixes
- Need to define all variables upfront
Supporting Multiple Themes
CSS variables make it easy to support unlimited custom themes:global.css
Migration Guide
From Theme Variants to CSS Variables
- Identify repeated colors across your components
- Define CSS variables for these colors in
global.css - Replace theme variants with CSS variable references
- Test thoroughly in all themes
Related
Theming Basics
Learn the fundamentals of theming in Uniwind
Global CSS
Configure global styles and CSS variables
Custom Themes
Create custom themes beyond light and dark
useUniwind Hook
Access theme information in your components