Sub topic 13
Arbitrary values and when to use them
This chapter assumes you can read Tailwind class lists in React components. It also assumes you know what a theme token is from the previous chapter, such as bg-brand-500 from an @theme block.
What arbitrary values are
Tailwind's built-in scale cannot cover every number a design asks for. Square brackets let you put any CSS value directly in a utility class. Tailwind generates the CSS for that exact value.
export function Banner() {
return (
<div className="w-[320px] bg-[#1da1f2] p-4 text-white">
Fixed-width promo
</div>
)
}w-[320px] becomes width: 320px, and bg-[#1da1f2] becomes a background color with that hex value. Other utilities work the same way, for example top-[117px] or text-[22px].
Syntax details
A few rules cover most cases:
- Spaces are not allowed inside the brackets. Use an underscore instead, and Tailwind converts it to a space:
grid-cols-[1fr_auto]. - Functions such as
calc()work:h-[calc(100vh-4rem)]. - In Tailwind v4, a CSS variable can be referenced with parentheses:
bg-(--banner-color). - Some utilities share a prefix, such as
text-for both size and color. A type hint tells Tailwind which one you mean:text-[length:var(--title-size)]ortext-[color:var(--title-color)].
You can also write a whole one-off declaration with an arbitrary property, such as [mask-type:luminance]. Brackets work in variants too, so [&>p]:mt-2 styles direct child paragraphs.
export function Toolbar() {
return (
<div className="grid grid-cols-[1fr_auto] gap-2 [&>button]:rounded-md">
<input className="h-[calc(2.5rem+2px)] border px-2" />
<button className="bg-(--accent) px-4 text-white">Go</button>
</div>
)
}Here --accent must be defined somewhere in your CSS, or the button has no background.
When to use them
Use an arbitrary value when the value is genuinely a one-off. Typical cases are a brand color from a third-party widget, a pixel offset that matches an image, or a calc() that depends on a fixed header height.
If the same value appears in three or more places, it is a design token. Move it into the theme:
@import "tailwindcss";
@theme {
--color-twitter: #1da1f2;
}Then write bg-twitter everywhere. A change to the color then happens in one file, and your class lists stay readable.
flowchart TDArbitrary values and dynamic data
Brackets are still plain text that Tailwind reads from your source. They follow the same detection rule as every other class, so you cannot build one from a runtime variable.
export function Bar({ width }: { width: number }) {
// Does not work: Tailwind never sees a complete class such as w-[40px].
const bad = <div className={`w-[${width}px]`} />
// Works: the runtime value goes through an inline style.
const good = <div className="h-2 bg-blue-600" style={{ width }} />
return good
}For values that come from props, state or an API, use the style prop. Another option is to set a CSS variable in style and read it with a class such as w-(--bar-width).
Common errors
- Typing a space inside brackets, such as
grid-cols-[1fr auto]. Use_. - Building the bracket class with a template string, as in the
Barexample above. - Writing
text-[#333]andtext-[14px]and expecting Tailwind to infer the type in every case. When it cannot, add thelength:orcolor:hint.
Exercise
A component receives a height prop in pixels at runtime, and your teammate writes className={h-[${height}px]}. Why does this fail, and what are two ways to fix it?
Show answer
Tailwind scans source text for complete class names, and h-[${height}px] is assembled at runtime, so no CSS is generated. Fix it by passing the value through the style prop (style={{ height }}), or by setting a CSS variable in style and using a class such as h-(--box-height).