Sub topic 13
Arbitrary values for one-off styles
This chapter assumes you know the default scale from the earlier chapters (spacing, colors, breakpoints). It also assumes you know how Tailwind scans source files for class names. The examples use Tailwind v4 syntax. Where v3 differs, the text says so.
What an arbitrary value is
The default scale covers most design needs. Sometimes a design calls for a value that is not on it: a 320px sidebar, a brand color from another team's style guide, or a 13px offset that a designer measured. Arbitrary values let you write that value directly in the class name, inside square brackets.
<div class="w-[320px] bg-[#1da1f2] p-[13px]">
One-off values written directly in square brackets.
</div>Tailwind sees w-[320px] while scanning your files and generates a rule with exactly that value. You write no separate CSS file and invent no class name. The generated rule is the same kind of rule as any other utility, so it takes part in the same ordering and variants.
The working model
An arbitrary value is still a utility. The part before the brackets picks the CSS property, and the bracketed part supplies the value. The scanner reads the full class name as plain text, so the value must appear literally in your source.
| Class | Generated declaration |
|---|---|
| `w-[320px]` | `width: 320px` |
| `bg-[#1da1f2]` | `background-color: #1da1f2` |
| `top-[117px]` | `top: 117px` |
| `text-[22px]` | `font-size: 22px` |
| `grid-cols-[200px_1fr_auto]` | `grid-template-columns: 200px 1fr auto` |
Three rules govern the bracket contents:
- Spaces are not allowed in a class name, so write an underscore where CSS needs a space. Tailwind converts
_to a space in the output. - If you need a literal underscore, escape it with a backslash:
content-['snake\_case']. - The value must be valid CSS for that property. Tailwind passes it through and does not check it.
Combining with variants
Arbitrary values work with every variant prefix, in the same order as any other utility. Variants come first, then the utility.
<div class="w-full md:w-[480px] hover:bg-[#0d8bd9] dark:bg-[#0b1220] bg-[#1da1f2] p-4">
Responsive, interactive, and dark-aware, with custom values.
</div>You can also use negative values by putting the minus sign in front of the utility name, as in -top-[5px], or inside the brackets, as in top-[-5px]. Both produce the same CSS.
Math and layout with arbitrary values
The most common reason to reach for brackets is a calculation that mixes units. The scale cannot express "full width minus a fixed gutter", but calc() can.
<main class="min-h-[calc(100vh-4rem)] w-[calc(100%-2rem)] mx-auto">
Fills the viewport below a 4rem header.
</main>Tailwind adds the required spaces around +, -, *, and / inside calc() for you, so calc(100%-2rem) is valid in a class name. You can write calc(100%_-_2rem) if you prefer to see the spaces.
Grid templates are the other frequent case. The underscore rule applies:
<div class="grid grid-cols-[200px_minmax(0,1fr)_auto] gap-4">
<aside>Sidebar</aside>
<section>Content</section>
<nav>Actions</nav>
</div>Pseudo-element content is a third case. Quote the string and use underscores for spaces:
<a class="after:ml-1 after:content-['_↗']" href="https://example.com">
External link
</a>Disambiguating with type hints
Some utilities share a prefix. text- sets both font size and color, and bg- can set a color, an image, or a size. When the value in brackets is ambiguous, Tailwind guesses from its contents, and that guess can be wrong, especially for CSS variables. A type hint before a colon removes the guess.
<p class="text-[length:var(--hero-size)] text-[color:var(--hero-color)]">
Size and color both come from variables.
</p>Without the length: hint, Tailwind cannot tell whether text-[var(--hero-size)] means a size or a color. Other hints include color:, length:, percentage:, url:, and position:. Add a hint whenever a variable is involved and the output looks wrong.
CSS variables as values
Referencing a CSS variable is the cleanest way to combine arbitrary values with dynamic data. In Tailwind v4, parentheses are a shorthand for var():
<div class="w-(--sidebar-width) bg-(--panel-bg) p-4">
Reads its size and color from CSS variables.
</div>w-(--sidebar-width) is equivalent to w-[var(--sidebar-width)]. The bracket form is the only one that works in v3, and it still works in v4. If you need a type hint with the shorthand, put it first: text-(length:--hero-size).
Arbitrary properties
If Tailwind has no utility for a CSS property, you can write the whole declaration in brackets. The property name goes before the colon and the value after it.
<div class="[mask-type:luminance] [--card-gap:12px] hover:[mask-type:alpha]">
Any CSS property, with variants if you need them.
</div>Arbitrary properties are also a handy way to set a CSS variable on one element so that children can read it.
Arbitrary variants
The same bracket idea works for selectors. Put a selector in brackets, using & for the current element, and write utilities after the colon.
<ul class="[&>li]:py-2 [&_a]:underline [&:nth-child(3)]:font-bold">
<li><a href="/one">One</a></li>
<li><a href="/two">Two</a></li>
<li><a href="/three">Three</a></li>
</ul>Here [&>li]:py-2 styles direct li children, and [&_a]:underline styles any descendant link. Arbitrary variants are useful for markup you do not control, such as rendered Markdown. Use them sparingly. Styling descendants from a parent splits the styles for one element across two places, which makes them harder to find.
Using arbitrary values with dynamic data
The detection rule from the previous chapter applies fully here. Tailwind reads the class name as text at build time, so a value that is only known at runtime cannot be put in brackets through string interpolation.
// Does not work: the final class name never appears in the source.
const bad = (px) => `w-[${px}px]`;
// Works: set a variable at runtime, and read it with a fixed class.
const good = (px) => ({ style: `--box-width: ${px}px` });<div class="w-(--box-width) bg-slate-100 p-4" style="--box-width: 240px">
The class is fixed. Only the variable changes.
</div>Your code, not Tailwind, sets --box-width at runtime. The utility class stays constant and gets scanned normally. This pattern covers progress bars, chart sizes, and user-chosen colors.
Common errors
- Interpolated class names.
w-[${n}px]andbg-[${color}]are never generated. Use the variable pattern above or map to complete class strings. - Spaces inside brackets.
grid-cols-[200px 1fr]is two class names to the browser. Use underscores. - Missing type hint. A variable passed to
text-[...]that applies the wrong property needslength:orcolor:. - Invalid CSS.
w-[320]has no unit, so the browser drops the declaration. Tailwind does not warn you, so inspect the element if nothing happens. - Unneeded brackets. In v4,
p-13already produces3.25rembecause spacing utilities accept any number. Check whether the default scale covers the value before writingp-[13px]. This is also true forw-,h-,gap-, andm-. - Wrong variant order. Put variants before the utility:
hover:bg-[#0d8bd9], notbg-[#0d8bd9]:hover.
When to prefer a theme token
Arbitrary values are right for something that happens once. They become a cost when the same value appears in many places. A color written as bg-[#4f46e5] in twelve files means a design change needs twelve edits, and nothing tells a reader the value is deliberate.
A good rule is to promote a value to a theme token when it appears a third time or when it carries meaning, such as a brand color.
@import "tailwindcss";
@theme {
--color-brand: #4f46e5;
--spacing-gutter: 1.25rem;
}After this change, bg-brand, text-brand, and p-gutter are real utilities, and one edit updates every use. Keep arbitrary values for pixel-measured oddities, one-time layout math, and runtime variables.
Summary of the syntax
| Need | Syntax | Example |
|---|---|---|
| One-off value | `utility-[value]` | `w-[320px]` |
| Space in value | underscore | `grid-cols-[1fr_2fr]` |
| CSS variable (v4) | `utility-(--name)` | `bg-(--panel-bg)` |
| Type hint | `utility-[type:value]` | `text-[length:var(--s)]` |
| Any property | `[property:value]` | `[mask-type:luminance]` |
| Custom selector | `[selector]:utility` | `[&>li]:py-2` |
Exercise
A progress bar's width comes from a number between 0 and 100 returned by your JavaScript code at runtime. Why does the class w-[${percent}%] fail, and what is a working alternative that keeps a fixed class in the markup?
Show answer
Tailwind scans source files as plain text at build time. The string w-[${percent}%] never appears as a complete class name, so no CSS rule is generated for it. Instead, write a fixed class that reads a variable, such as class="w-(--progress)", and set the value at runtime with style="--progress: 40%" (in v3, use w-[var(--progress)]). The class is detected normally, and only the variable changes.
Exercise
You write <p class="text-[var(--hero-size)]"> and the text renders in the wrong style: the size does not change. What causes this, and how do you fix it?
Show answer
The class name is ambiguous, because text- can set font size or color, and Tailwind cannot tell which one a bare variable should be. Add a type hint: text-[length:var(--hero-size)], or in v4 text-(length:--hero-size).
Exercise
The color #4f46e5 appears as bg-[#4f46e5] in nine components. What should you do, and what does the code look like?
Show answer
Promote it to a theme token so one edit changes every use. Add @theme { --color-brand: #4f46e5; } after @import "tailwindcss";, then replace each bg-[#4f46e5] with bg-brand.