Sub topic 5
CSS variables and calc()
This chapter assumes you know the square-bracket syntax from the previous chapters, such as w-[317px]. It also assumes you know CSS custom properties (--name: value, read with var(--name)) and the calc() function. Here you will put variables and calc() inside arbitrary values and avoid the usual mistakes.
Using calc() in a bracket
Anything valid as a CSS value can go between the brackets, including calc(). This is useful when a layout mixes units that a theme scale can't express.
<div class="absolute top-[117px] left-[calc(50%-2rem)] w-[317px] bg-[#bada55]"></div>Here left-[calc(50%-2rem)] centers the element's left edge, then shifts it back by 2rem. CSS requires spaces around + and - inside calc(). Tailwind adds them for you when it generates the rule, so you can write calc(50%-2rem) without spaces. Spaces are not allowed inside a class name, so you could also write calc(50%_-_2rem). Both forms produce the same CSS.
<div class="grid grid-cols-[1fr_500px_2fr] gap-[10px]">
<div>Sidebar</div>
<div>Content</div>
<div>Aside</div>
</div>The underscores in grid-cols-[1fr_500px_2fr] become spaces, so the generated rule is grid-template-columns: 1fr 500px 2fr. This is the same rule you saw in the chapter on underscores.
Reading a CSS variable with var()
A value in brackets can read a custom property with var(). The variable can come from your own stylesheet, from a parent element, or from the Tailwind theme.
<section class="[--scroll-offset:56px] lg:[--scroll-offset:44px] scroll-mt-[var(--scroll-offset)]">
...
</section>This example uses two features together:
[--scroll-offset:56px]is an arbitrary property that declares the variable. Itslg:version replaces the value at thelgbreakpoint.scroll-mt-[var(--scroll-offset)]is an arbitrary value that reads the variable.
You write the breakpoint logic once, on the variable. Every utility that reads the variable follows it, so you don't repeat lg: on each one.
If you use Tailwind v4, there is a shorter form for variables: scroll-mt-(--scroll-offset). Parentheses replace [var(...)]. The long form works in every version, so this chapter uses it.
flowchart TDType hints for variables
Some utilities accept more than one kind of value. text-* sets either font-size or color. With a literal such as text-[14px] or text-[#336699], Tailwind can tell which one you mean from the value. With var(--something) it can't, because a variable could hold anything.
Add a type hint before the value, separated by a colon:
<p class="text-[length:var(--hero-size)] text-[color:var(--hero-tone)]">Headline</p>length: makes the first class set font-size. color: makes the second set color. Other utilities have the same ambiguity, for example border-[...] (width or color) and bg-[...] (color, image or size). Add a hint whenever a variable is used in a utility that could mean two things.
Setting a variable per element
Variables let you pass a runtime value into a class that Tailwind generated at build time. This is the safe alternative to building class names from strings, which is covered in a later chapter. The class stays complete and fixed, and only the variable changes:
<div class="h-2 w-[var(--progress)] bg-[#2563eb]" style="--progress: 40%"></div>The style attribute can be generated by your framework from data. Tailwind only needs to see the finished class w-[var(--progress)] in the source.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `text-[var(--x)]` has no effect or sets the wrong property | Ambiguous utility | Use `text-[length:var(--x)]` or `text-[color:var(--x)]` |
| Spacing is wrong inside `calc()` | A missing operator or unit mismatch, such as `calc(50%-2)` | Give every term a valid unit and check the generated CSS |
| The variable seems undefined | It is set on a child or sibling, not on the element or an ancestor | Declare it on the same element or a parent |
| Value in the `grid-cols` template is one token | Spaces were typed instead of underscores | Use `1fr_500px_2fr` |
Exercise
A card should have 1.5rem of horizontal padding on small screens and 3rem from the md breakpoint up. Write the classes so that the breakpoint is written only once, using an arbitrary property and an arbitrary value. Then explain what changes if the variable is read by two other utilities.
Show answer
Use [--gutter:1.5rem] md:[--gutter:3rem] px-[var(--gutter)]. The [--gutter:...] classes declare the variable, and md: replaces it at the md breakpoint. px-[var(--gutter)] reads it. Any other utility that reads var(--gutter), such as scroll-mx-[var(--gutter)], follows the same change without needing its own md: class.