In Tailwind CSS v4, what do the `--spacing()` and `--alpha()` functions compile to in your own CSS, and why use them over hard-coded values?
answer
- replaced during the build
- one multiplier behind the scale
- calc() around a variable
- color-mix() with transparent
- theme() is the deprecated lookup
basics
~20 sIn Tailwind CSS v4, --spacing(4) compiles to calc(var(--spacing) * 4) and --alpha(var(--color-x) / 50%) to color-mix(in oklab, var(--color-x) 50%, transparent). Both keep hand-written CSS on the theme's scale and palette, so changing a theme variable updates them too.
solid answer
~40 sBoth are build-time functions Tailwind replaces in your CSS. `--spacing(6)` becomes `calc(var(--spacing) * 6)` — the same multiplier `p-6` uses — so a hand-written `.btn` stays on the spacing scale and moves with it if `--spacing` changes. `--alpha(var(--color-civic-600) / 40%)` becomes `color-mix(in oklab, var(--color-civic-600) 40%, transparent)`, a translucent version of a theme colour; because the `var()` survives, the browser resolves it at runtime, so a dark-mode override of the variable carries through. Misuse fails the build: `--spacing()` needs a `--spacing` theme variable, which a CSS Module without `@reference` lacks, and `--alpha()` needs one argument with the colour and alpha separated by a slash. `--spacing()` also works inside arbitrary values, as in `py-[calc(--spacing(4)-1px)]`. v3's `theme(spacing.6)` lookup survives only for compatibility and is deprecated.
go deeper
Recall that --spacing(n) gives a value on the theme's spacing scale and --alpha() makes a theme colour translucent, both inside your own CSS.
Explain the compiled output, calc(var(--spacing) * n) and color-mix in oklab with transparent, and why keeping var() in that output matters for theme changes.
Spot the build errors these functions raise, such as a CSS Module with no @reference, and replace literals and the deprecated theme() lookup during a migration.
Decide whether hand-written CSS must stay on theme functions and variables, so a spacing or palette change is a single edit, and how review will hold that line.
## Build-time functions for your own CSS **Tailwind CSS v4** provides two functions you can call in hand-written CSS: `--spacing()` and `--alpha()`. Tailwind replaces them during the build, so the browser only ever sees standard CSS — `calc()` and `color-mix()` — that still refers to the theme's custom properties. That last point is what makes them better than a typed value: the output stays connected to the theme. ## `--spacing()` ```css .btn { padding-inline: --spacing(4); } /* compiles to */ .btn { padding-inline: calc(var(--spacing) * 4); } ``` - `--spacing` is the single theme variable behind the whole spacing scale — `0.25rem` in the default theme — so `--spacing(4)` is exactly the value `p-4` and `gap-4` use. - Because the output keeps `var(--spacing)`, changing that one variable re-scales hand-written rules and utilities together. - It also works inside arbitrary values in markup, typically with `calc()`: `py-[calc(--spacing(4)-1px)]`. - It takes exactly one argument, and it needs a `--spacing` theme variable to exist; otherwise the build stops with an error. ## `--alpha()` ```css .notice { background: --alpha(var(--color-civic-600) / 15%); } /* compiles to */ .notice { background: color-mix(in oklab, var(--color-civic-600) 15%, transparent); } ``` - The syntax is **one argument**: a colour and an alpha separated by a slash. Passing the alpha as a second, comma-separated argument is rejected. - A unitless alpha such as `0.5` is converted to `50%`; an alpha of `100%` returns the colour unchanged. - `color-mix()` is evaluated in the browser, so the variable is resolved at runtime. If dark mode overrides `--color-civic-600`, the translucent tint follows — something a colour computed once at build time could not do. ## Compared with the alternatives | Written in your CSS | What ships | Follows a theme change? | |---|---|---| | `padding-inline: 1rem;` | the literal | no | | `padding-inline: calc(var(--spacing) * 4);` | the same as `--spacing(4)`, written longhand | yes | | `padding-inline: --spacing(4);` | `calc(var(--spacing) * 4)` | yes | | `background: rgb(30 64 175 / 15%);` | the literal | no | | `background: --alpha(var(--color-civic-600) / 15%);` | `color-mix(in oklab, …, transparent)` | yes | The longhand `calc()` is equivalent; the function is shorter and states the intent — "four steps of the scale" — rather than the arithmetic. v3's `theme(spacing.4)` lookup still compiles, but only as a deprecated compatibility feature; the documentation recommends theme variables instead. ## In practice - **Component classes and base styles.** A `.btn` in `@layer components` uses `padding-inline: --spacing(4)` so it lines up with `px-4` elsewhere on the page. - **Third-party overrides.** A widget's dropdown gets `gap: --spacing(2)` and a tinted `--alpha()` background that matches the palette instead of a guessed hex value. - **Arbitrary values.** `--spacing()` inside `calc()` in markup handles off-by-a-border cases, such as `py-[calc(--spacing(4)-1px)]`, without leaving the scale. - **Variables as inputs.** `--alpha()` takes any colour expression with a slash, so a theme variable is the usual input; the alpha itself can be a percentage or a unitless number. ## Where they fail The functions fail loudly, at build time, rather than shipping broken CSS: 1. **No theme in the compile.** A CSS Module or a Vue or Svelte style block is compiled separately; without `@reference` to the main stylesheet there is no `--spacing` variable, and `--spacing()` errors. 2. **Wrong argument count.** `--spacing()` with no argument, or with two, is an error. 3. **Wrong `--alpha()` shape.** A missing slash, or a comma-separated second argument, is an error that shows the expected form. A team adopting these usually also rewrites old `theme()` calls and stray literals during the same pass, so that every spacing and colour in hand-written CSS traces back to a theme variable.
- In Tailwind CSS v4, why does a colour made with `--alpha()` keep following a dark-mode override of the theme colour?Because the output is `color-mix(in oklab, var(--color-civic-600) 15%, transparent)`: the `var()` survives the build, and the browser evaluates the mix at runtime with whatever value the variable has on that element. A colour computed once at build time would stay fixed to the light-mode value.
- In Tailwind CSS v4, what happens when a CSS Module with no `@reference` uses `--spacing(4)`?The build fails: the module is compiled on its own with no theme, and `--spacing()` requires a `--spacing` theme variable. Add `@reference "../app.css"` at the top of the module, or write `calc(var(--spacing) * 4)` yourself, which needs no Tailwind processing.
- --spacing(4) outputs a fixed 1rem literal at build time
- --alpha() takes the alpha as a second, comma-separated argument
- These functions run in the browser, like calc() does
- theme(spacing.4) is the recommended way to read the scale in v4
- --alpha() bakes in the colour, so a dark-mode override is ignored
Assumes Tailwind CSS v4 (checked against 4.3.3); --spacing() and --alpha() are v4 build-time functions; v3's theme() lookup remains only as a deprecated compatibility feature.