Sub topic 13
Arbitrary values for one-off styles
This chapter assumes you know basic HTML and CSS, can install Tailwind and run its build, and have seen how utilities map to the default scale. It covers what to do when the scale has no value that fits.
Why arbitrary values exist
Tailwind's default scale covers most needs, and you can extend it in your theme. Sometimes a design needs a value that appears once: a brand color from a third party, a 320px sidebar, or a 13px nudge to line up with an icon. Adding each of these to the theme clutters your design tokens. An arbitrary value lets you write the value directly in the class name, in square brackets.
<div class="w-[320px] bg-[#1da1f2] p-[13px]">
One-off values written directly in square brackets.
</div>Each class is still a utility. w-[320px] generates a rule that sets width: 320px. bg-[#1da1f2] sets the background color. p-[13px] sets padding on all sides.
How the syntax works
Write the utility name, a dash, then the value in square brackets. Tailwind puts the value into the CSS it generates for that class.
- Lengths:
h-[72px],top-[117px],max-w-[40rem]. - Colors:
text-[#0f172a],border-[rgb(10,20,30)]. - Grid templates:
grid-cols-[200px_1fr]. Underscores stand in for spaces. - Whole properties:
[mask-type:luminance]sets a property that has no utility.
Some utilities share a prefix across different properties. text-[14px] could mean a font size or a color. Tailwind usually infers the type from the value. When it cannot, add a type hint: text-[length:var(--my-size)] or text-[color:var(--my-color)].
You can combine arbitrary values with variants. The prefixes from earlier chapters work as usual.
<button class="bg-[#1da1f2] hover:bg-[#0c85d0] md:w-[280px]">
Follow
</button>Arbitrary variants
You can also write a selector in brackets to make a variant. [&:nth-child(3)]:underline underlines an element only when it is the third child. The & stands for the element itself. This is useful for a selector that no built-in variant covers.
Arbitrary values still need to be complete
Arbitrary values follow the same detection rule you saw in the dynamic class names chapter. Tailwind scans source text for complete class names, so the whole class, brackets included, must appear literally.
// Does not work: the value is assembled at runtime.
const bad = (px) => `w-[${px}px]`;
// Works: every complete class appears in the source.
const widths = {
narrow: 'w-[240px]',
wide: 'w-[480px]',
};
const good = (size) => widths[size];If a value really changes at runtime, set a CSS variable with the inline style attribute and reference it from a fixed class, such as w-[var(--panel-width)].
When to use them, and when not to
Arbitrary values are an escape hatch. If you write the same bracketed value in many places, it is a design token in disguise.
| Situation | Better choice |
|---|---|
| A value used once, such as a pixel nudge | Arbitrary value |
| A brand color used across the site | Theme token such as `--color-brand` |
| A spacing step used everywhere | Theme token in `@theme` |
| A value computed at runtime | CSS variable set with `style` |
Common errors
- Using spaces inside brackets. Class names cannot contain spaces, so write
grid-cols-[200px_1fr], notgrid-cols-[200px 1fr]. - Building the class from a variable, as in the first JavaScript example above.
- Leaving out a type hint when
text-[...]or similar is ambiguous. - Using an arbitrary value where a default step is close enough, which adds noise for no gain.
Exercise
A dashboard needs a sidebar exactly 264px wide, and the same 264px appears in four other components. Should you write w-[264px] in each place? What would you do instead, and how would a width loaded from an API at runtime be handled?
Show answer
Repeating w-[264px] four times means the value is really a design token. Define it in the theme, for example a --spacing-sidebar variable inside @theme, and use the generated utility, so one edit changes all uses. A width that arrives at runtime cannot be written as w-[${px}px], because Tailwind cannot detect that class. Set a CSS variable through the style attribute, such as style="--panel-width: 264px", and use the fixed class w-[var(--panel-width)].