Sub topic 13
Arbitrary values and when to use them
This chapter covers Tailwind's square-bracket syntax for one-off values. It explains how arbitrary values work, where they fit, and where they cause problems. It assumes you can read React and TSX, know how Tailwind finds class names in source files (covered earlier), and have set up Tailwind v4 in a Vite project with the @theme block.
What arbitrary values are
Tailwind's utilities are tied to a theme. A class like p-4 or bg-blue-600 maps to a value defined in that theme. Sometimes a design needs a value the theme does not have, such as a 320px width or a brand color used in exactly one place.
An arbitrary value is a value you write directly inside square brackets in a class name. Tailwind generates a CSS rule for that exact value.
export function Banner() {
return (
<div className="w-[320px] bg-[#1da1f2] p-4 text-white">
Fixed-width promo
</div>
)
}This produces CSS equivalent to width: 320px and background-color: #1da1f2. You did not edit the theme or write any CSS file.
The syntax is always utility-[value]. The part before the bracket is a normal utility prefix, so Tailwind knows which CSS property to set.
How the generated CSS comes about
Arbitrary values go through the same detection step as every other class. Tailwind scans your source text, finds the string w-[320px], parses the bracket contents, and emits a rule. The class name in your source must be complete, so the rule from the earlier chapter on dynamic classes still applies: you cannot assemble the bracket contents from fragments at runtime.
// Does not work: the complete class name never appears in the source.
const bad = <div className={`w-[${width}px]`} />
// Works: the full class name is written out as a string.
const good = <div className="w-[320px]" />If the value genuinely varies at runtime, such as a user-chosen color or a measured height, use a different tool. The options are covered later in this chapter.
Common forms
Arbitrary values appear in several places beyond sizing.
| Form | Example | Result |
|---|---|---|
| Length | `h-[72px]`, `top-[117px]` | Exact size or offset |
| Color | `text-[#1da1f2]`, `bg-[rgb(20_30_40)]` | Exact color |
| Grid template | `grid-cols-[200px_1fr_auto]` | Custom column layout |
| Calc expression | `h-[calc(100vh-4rem)]` | Computed size |
| CSS variable | `bg-(--my-color)` | Value read from a variable |
| Arbitrary property | `[mask-type:luminance]` | Any CSS property |
| Arbitrary variant | `[&>li]:mt-2` | Custom selector |
A few details matter here.
- Underscores stand in for spaces.
grid-cols-[200px_1fr_auto]becomesgrid-template-columns: 200px 1fr auto. To get a literal underscore, escape it with a backslash. - Inside
calc(), Tailwind adds the required spaces around operators for you, soh-[calc(100vh-4rem)]is valid. - In Tailwind v4, the shorthand
bg-(--my-color)reads a CSS variable. In v3 you wrotebg-[var(--my-color)]. The longer form still works in v4.
Ambiguity and type hints
Some utilities share a prefix. text- sets both font size and color, and border- sets both width and color. With an arbitrary value, Tailwind has to guess which one you mean, and it infers from the value's shape.
export function Ambiguous() {
return (
<>
{/* Treated as a color */}
<p className="text-[#1da1f2]">Color</p>
{/* Treated as a font size */}
<p className="text-[22px]">Size</p>
{/* A CSS variable is ambiguous, so give a type hint */}
<p className="text-[length:var(--size)]">Size from a variable</p>
<p className="text-[color:var(--tint)]">Color from a variable</p>
</>
)
}A value such as var(--size) carries no information about its type. Prefix it with a type hint like length: or color: and Tailwind picks the right property. If an arbitrary class produces no CSS and you cannot see why, a missing type hint is a common cause.
Arbitrary properties and variants
When no utility exists for a CSS property, you can write the property itself in brackets.
export function Mask() {
return (
<div className="[mask-type:luminance] [scrollbar-gutter:stable]">
Content
</div>
)
}Arbitrary properties combine with variants like any other class. hover:[mask-type:alpha] and lg:[scrollbar-gutter:stable] both work.
Arbitrary variants let you write a selector directly in the class name. The & stands for the element itself.
export function List() {
return (
<ul className="[&>li]:mt-2 [&>li:first-child]:mt-0">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
)
}This styles children from the parent without touching each child. It is useful when you render items you do not control, for example Markdown output.
Arbitrary values versus theme tokens
Arbitrary values are an escape hatch. The theme is the system. Choosing between them is a design decision, not just a syntax choice.
Use an arbitrary value when:
- The value appears once, and no other element should share it.
- The value is tied to an external constraint, such as a third-party widget's fixed size or an embedded image's exact offset.
- You need a
calc()that combines units, such ash-[calc(100vh-4rem)]. - You are prototyping and will decide later whether the value deserves a token.
Move the value into the theme when:
- It appears in two or more places.
- It carries meaning, such as a brand color or a heading size.
- A designer would refer to it by name.
Here is the same color used both ways. The first version scatters a hex code. The second names it once.
// Scattered: changing the brand color means finding every hex code.
export function Scattered() {
return (
<>
<button className="bg-[#3b82f6] text-white">Save</button>
<a className="text-[#3b82f6] underline" href="/docs">Docs</a>
</>
)
}@import "tailwindcss";
@theme {
--color-brand-500: #3b82f6;
}// Named: one definition, many uses.
export function Named() {
return (
<>
<button className="bg-brand-500 text-white">Save</button>
<a className="text-brand-500 underline" href="/docs">Docs</a>
</>
)
}The second version also reads better in review. bg-brand-500 says what the color is for. bg-[#3b82f6] only says what it looks like.
Handling values that change at runtime
Because class names must be complete strings in your source, arbitrary values cannot take a variable. There are three safe patterns for runtime values.
Inline styles
For a value that really is dynamic, such as a progress percentage, set it in the style prop. Inline styles are evaluated at runtime and are not scanned.
export function ProgressBar({ percent }: { percent: number }) {
return (
<div className="h-2 w-full rounded bg-gray-200">
<div
className="h-2 rounded bg-blue-600"
style={{ width: `${percent}%` }}
/>
</div>
)
}CSS variables
A better option when you also want variants or the full class system is to pass the value through a CSS variable and read it with an arbitrary value. The class name stays static, and only the variable changes.
export function Swatch({ color }: { color: string }) {
return (
<div
className="h-8 w-8 rounded bg-(--swatch)"
style={{ '--swatch': color } as React.CSSProperties}
/>
)
}The cast is needed because TypeScript's CSSProperties type does not include custom properties. The class bg-(--swatch) is complete and detectable. The color is supplied at runtime.
A lookup table
If the set of possible values is finite, map each one to a full class string. This is the approach from the chapter on dynamic class names, and it works for arbitrary values too.
const widths = {
narrow: 'w-[240px]',
wide: 'w-[480px]',
} as const
export function Panel({ size }: { size: keyof typeof widths }) {
return <div className={widths[size]}>Panel</div>
}Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Class has no effect | Class built from a template string | Write the full class or use a lookup table |
| `text-[var(--x)]` sets the wrong property | Ambiguous type | Add `length:` or `color:` |
| Spaces break the value | Literal space inside brackets | Use an underscore |
| `calc()` result is wrong | Mixed units without correct operators | Check the expression, for example `100vh-4rem` |
| Override does not win | Conflicting arbitrary and theme classes | Merge with `tailwind-merge` |
On the last row: tailwind-merge understands most arbitrary values, so cn('w-4', 'w-[320px]') keeps only w-[320px]. It cannot always tell the type of an ambiguous value, so use a type hint where the class could be read two ways.
Overuse and its costs
A codebase full of arbitrary values loses the main benefit of Tailwind, which is a constrained set of choices. Watch for these signs.
- Many different pixel values for the same purpose, such as
p-[13px],p-[14px]andp-[15px]. - Hex codes repeated across files.
- Long class lists dominated by bracketed values.
- Arbitrary values that duplicate an existing token, such as
w-[16px]wherew-4exists.
Each of these makes a visual change expensive, because there is no single place to edit. Review arbitrary values the way you would review magic numbers in other code.
Putting it together
The diagram shows how to decide which approach to use.
Exercise
A component receives a height prop in pixels from a parent that measures the page, and you write className={h-[${height}px]}. The element never gets the height. Why does this fail, and how would you fix it using a CSS variable?
Show answer
Tailwind scans source text for complete class names, and h-[${height}px] never appears as a complete string, so no CSS is generated. Pass the value through a variable instead. Use a static class h-(--h) and set the variable in the style prop: style={{ '--h': ${height}px } as React.CSSProperties}. The class is detectable at build time and the value is supplied at runtime.
Exercise
You see bg-[#3b82f6] in five components and text-[#3b82f6] in two more. What should you change, and which class names would replace them?
Show answer
The same value is used in seven places and has a meaning, so promote it to the theme. Add --color-brand-500: #3b82f6; inside @theme. Then replace the classes with bg-brand-500 and text-brand-500. A future color change becomes a single edit.
Exercise
What does grid-cols-[200px_1fr_auto] generate, and why is text-[var(--size)] risky?
Show answer
The first generates grid-template-columns: 200px 1fr auto, because underscores become spaces. The second is risky because var(--size) could be a color or a font size, and Tailwind cannot tell. Add a type hint, as in text-[length:var(--size)], so it sets the font size.