theme() CSS function allows you to reference values from your Tailwind theme (defined via @theme) directly in your CSS.
Syntax
Basic Usage
Referencing Theme Variables
Usetheme() to reference CSS custom properties defined in @theme:
Direct Value Resolution
By default,theme() returns a var() reference. The actual value is resolved at runtime by the browser.
Inline Resolution
Use theinline modifier to resolve theme values at build time:
The theme variable itself is not included in the output when using
inline.When to Use Inline
Fallback Values
Provide a fallback value if the theme variable doesn’t exist:--color-accent is not defined:
Multiple Fallbacks
You can chain multiple fallbacks:Fallback with var()
When the theme value is avar() reference, the fallback is injected into it:
initial placeholder in the theme is replaced with the provided fallback.
Special Theme Functions
Tailwind provides additional CSS functions for working with theme values:—theme()
An alternative syntax that’s identical totheme():
—spacing()
Multiply the spacing scale by a value:The
--spacing theme variable must be defined for --spacing() to work.—alpha()
Apply opacity to a color:Using in At-Rules
Theme functions are automatically inlined when used in at-rule conditions:Media Queries
Supports Queries
Container Queries
Legacy dot-notation Syntax
The legacytheme() syntax using dot-notation is still supported:
- Requires quotes around the path
- Uses dots to separate path segments
- Maps to the same theme variables
The modern
--variable syntax is recommended for new projects.Unquoting Quoted Values
The legacy syntax automatically unquotes quoted theme values:Error Handling
Missing Theme Variable
If a theme variable doesn’t exist and no fallback is provided:Provide Fallback to Avoid Errors
Invalid Variable Name
Modern syntax requires CSS custom property names (starting with--):
Function Processing
From the source code (css-functions.ts:68-127), thetheme() function:
- Validates the variable name starts with
-- - Checks for the
inlinemodifier - Auto-inlines if used in an at-rule
- Resolves the value from the design system
- Handles fallback values
- Injects fallbacks into nested
var()ortheme()calls
Related Functions
--spacing()
Multiply the spacing scale
--alpha()
Apply opacity to colors
Related
- @theme - Define theme variables
- Customizing Colors - Color customization guide
- Theme Configuration - Theme system overview