Skip to main content

CSS Functions

Tailwind CSS provides several CSS functions for accessing theme values and manipulating colors.

theme() Function

The theme() 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:
CSS variables cannot be used in certain contexts like media queries. Tailwind automatically inlines theme values in these cases.

Implementation Details

From css-functions.ts:68-127:

—alpha() Function

Apply opacity to colors using the --alpha() function.
Implementation from css-functions.ts:19-40:
The result uses color-mix():

—spacing() Function

Multiply the theme’s base spacing value:
Implementation from css-functions.ts:42-66:

Directives

@apply Directive

The @apply directive extracts utility classes into custom CSS.

Basic Usage

Use @apply to extract repeated utility patterns into semantic component classes.

With Variants

In Custom Utilities

Circular Dependency Detection

Tailwind prevents circular dependencies:

Implementation Highlights

From apply.ts:10-325:
  • Topological sorting ensures utilities are applied in dependency order
  • Circular dependencies are detected and reported with helpful errors
  • @apply inside @utility is fully supported
  • Nested @apply is handled correctly
@apply Restrictions:
  • Cannot be used inside @keyframes
  • Must be used within a rule context (has a parent selector)
  • Cannot apply unprefixed utilities when a prefix is configured
  • Cannot apply blocklisted classes

@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

Use not to exclude patterns:

Inline Content

The @source directive is useful when you need fine-grained control over content scanning, especially in monorepo setups or when integrating with specific frameworks.

Combining Functions and Directives

Custom Component Pattern

Responsive Utilities

Best Practices

Recommendations:
  1. Theme Organization: Group related theme values by namespace (colors, spacing, typography)
  2. Reference Mode: Use @theme reference for values only needed in calculations
  3. Fallback Values: Always provide fallbacks for critical theme values
  4. @apply Sparingly: Use @apply for extracting repeated patterns, not for everything
  5. Inline Resolution: Be aware of when theme values are inlined vs. remaining as CSS variables

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