@theme directive is used to define custom CSS variables (custom properties) that make up your design system. These variables can be referenced throughout your CSS and are automatically available to Tailwind utilities.
Syntax
Basic Usage
Defining Theme Variables
Use@theme blocks to define custom properties that will be available as theme values:
:root selector and can be used by utilities like bg-primary, text-secondary, or referenced with the theme() function.
Multiple @theme Blocks
You can have multiple@theme blocks throughout your CSS. All theme variables are merged together:
Options
The@theme directive accepts several options that control how theme variables are processed:
Mark theme variables as reference-only. These variables won’t be included in the final CSS output but can still be used by utilities.
Inline the variable values directly instead of using CSS custom properties. This is useful for values that need to be computed at build time.
Mark theme variables as default values that can be overridden by user-defined theme values.
Mark theme variables as static values that won’t change based on context.
Add a prefix to theme variables and utilities. The prefix must be lowercase ASCII letters (a-z) only.
Combining Options
You can combine multiple options:Including @keyframes
The@theme block can also contain @keyframes definitions:
@keyframes are automatically hoisted out of the :root selector and placed at the root level of your CSS.
Theme Namespaces
Theme variables follow a namespace convention using double-dash prefixes:Restrictions
Output
Theme variables are output in the:root, :host selector:
reference are excluded from the output:
Escaping Special Characters
When using special characters in variable names, they are automatically escaped:Related
- theme() function - Reference theme variables in CSS
- @utility - Create custom utilities
- @variant - Create custom variants