Overview
Tailwind CSS v4 uses CSS variables (custom properties) for theming, enabling dynamic theme switching, dark mode, and customization without rebuilding your CSS.Theme Architecture
Tailwind v4’s theme system is built on CSS variables defined in the:root selector.
Basic Theme Structure
Tailwind applies theme variables to both
:root and :host to support Web Components.Reference Mode
Use@theme reference for values that shouldn’t be output as CSS variables:
Theme Namespaces
Theme values are organized by namespace prefixes:Color Namespace
--color- prefix:
Spacing Namespace
--spacing value is used for bare number utilities:
Typography Namespace
Other Namespaces
Dynamic Theme Switching
Because Tailwind v4 uses CSS variables, you can switch themes dynamically with JavaScript or additional CSS.Multiple Color Schemes
Component-Level Themes
Dark Mode
Tailwind v4’s dark mode is implemented using CSS variables and thedark: variant.
Default Dark Mode
The default implementation usesprefers-color-scheme:
Custom Dark Mode Implementation
Override the dark variant for class-based dark mode:Per-Component Dark Mode
Semantic Color Systems
Create semantic color systems that can be themed:Scoped Themes
Create themes scoped to specific sections:Theme Resolution
Understanding how Tailwind resolves theme values:Resolution Order
Fromtheme.ts, theme resolution follows this order:
- Check the exact key in the specified namespace
- Check bare key (without namespace prefix)
- Return null if not found
Inline vs. Variable Resolution
Theme Functions
Access theme values using thetheme() function:
Performance Considerations
CSS Variable Performance
CSS variables have excellent performance characteristics:- Variables are resolved at runtime (no recompilation needed)
- Inheritance is efficient (browsers optimize this)
- Theme switching is instant (just update the variable value)
- No JavaScript required for theme switching
When to Use Reference Mode
Use@theme reference to:
- Reduce CSS output size
- Avoid polluting the global CSS variable scope
- Prevent values from being overridden
- Keep intermediate calculation values private
Migration from v3
Config to @theme
Tailwind v3 config:Accessing Theme in CSS
v3 approach:Best Practices
Examples
Complete Multi-Theme System
Source Code Reference
The theming system is implemented in:/packages/tailwindcss/src/theme.ts- Theme class and resolution/packages/tailwindcss/src/css-functions.ts:68-127- theme() function/packages/tailwindcss/src/variants.ts:1143- dark variant