CSS Functions
Tailwind CSS provides several CSS functions for accessing theme values and manipulating colors.theme() Function
Thetheme() function resolves values from your theme configuration.
Basic Usage
In Tailwind CSS v4, theme values are accessed using CSS variable names that start with
--.With Fallback Values
Inline Resolution
Some contexts require inline resolution rather than CSS variable references:Implementation Details
Fromcss-functions.ts:68-127:
—alpha() Function
Apply opacity to colors using the--alpha() function.
css-functions.ts:19-40:
color-mix():
—spacing() Function
Multiply the theme’s base spacing value:css-functions.ts:42-66:
Directives
@apply Directive
The@apply directive extracts utility classes into custom CSS.
Basic Usage
With Variants
In Custom Utilities
Circular Dependency Detection
Tailwind prevents circular dependencies:Implementation Highlights
Fromapply.ts:10-325:
- Topological sorting ensures utilities are applied in dependency order
- Circular dependencies are detected and reported with helpful errors
@applyinside@utilityis fully supported- Nested
@applyis handled correctly
@theme Directive
Define theme values using the@theme directive.
Basic Theme Definition
Reference Mode
Use@theme reference to define theme values that can be referenced but won’t be output as CSS variables:
Reference mode is useful for values that are only used in specific contexts (like breakpoints in media queries) and don’t need to be available as CSS custom properties.
Namespace Organization
Theme Keys for Utilities
Utilities look up values in specific theme namespaces:@source Directive
The@source directive controls which files are scanned for utility classes.
Basic Usage
Excluding Content
Usenot to exclude patterns:
Inline Content
Combining Functions and Directives
Custom Component Pattern
Responsive Utilities
Best Practices
Source Code Reference
The functions and directives are implemented in:/packages/tailwindcss/src/css-functions.ts- CSS function implementations/packages/tailwindcss/src/apply.ts- @apply directive/packages/tailwindcss/src/theme.ts- Theme resolution/packages/tailwindcss/src/utilities.ts:174-191- withAlpha helper