Sub topic 13
One-off values with square-bracket syntax
This chapter covers arbitrary values: how to write them, where they work, and when to replace them with a theme value. It assumes you can read Tailwind class lists, know the default spacing scale (p-4 is 1rem), and have used responsive prefixes such as md:. Examples target Tailwind v3.3 or later and note v4 differences where they matter.
Why arbitrary values exist
The default scale covers most layouts, but design specs sometimes ask for 320px, 3px, or 18px. Without an escape hatch you would write a custom CSS rule for each one. Tailwind lets you put a literal CSS value in square brackets inside the class name. The generator reads the bracket content and emits a rule using it.
<div class="w-[320px] mt-[3px] text-[18px]">One-off values</div>This produces roughly the following CSS:
.w-\[320px\] { width: 320px; }
.mt-\[3px\] { margin-top: 3px; }
.text-\[18px\] { font-size: 18px; }The class name is the whole contract. There is no entry in a config file, so the value lives next to the element that uses it.
How the syntax works
An arbitrary value is the utility prefix, a hyphen, and a bracketed value: utility-[value]. Any utility that takes a length can accept one.
| Class | CSS produced |
|---|---|
| `w-[320px]` | `width: 320px` |
| `h-[50vh]` | `height: 50vh` |
| `p-[7px]` | `padding: 7px` |
| `gap-[0.8125rem]` | `gap: 0.8125rem` |
| `max-w-[42rem]` | `max-width: 42rem` |
| `leading-[1.35]` | `line-height: 1.35` |
| `tracking-[0.02em]` | `letter-spacing: 0.02em` |
| `-mt-[3px]` | `margin-top: -3px` (see the negative values section) |
Rules to keep in mind:
- The value must include a unit where CSS requires one.
w-[320]is not valid for width. - Spaces are not allowed inside the class name. Use an underscore where CSS needs a space. Tailwind converts it back, so
grid-cols-[1fr_2fr]becomesgrid-template-columns: 1fr 2fr. - Class names must appear in your source as complete strings. Tailwind scans files as text, so it cannot see a class built by string concatenation.
Modifiers combine with brackets
Variants such as md: and hover: work the same way they do with scale values.
<section class="p-4 md:p-[52px] lg:max-w-[72ch]">
<p class="text-base md:text-[19px]">Larger text from the md breakpoint.</p>
</section>You can use ch in max-w-[72ch]. The ch unit is the width of the 0 glyph in the current font, so it tracks the type size.
Disambiguating the type of a value
Some utilities share a prefix. text- sets font size and also text color. text-[18px] and text-[#336699] are both valid, and Tailwind decides which one you mean from the value. When the value is ambiguous, for example a CSS variable, give it a type hint before the value.
<p class="text-[length:var(--hero-size)]">Font size from a variable</p>
<p class="text-[color:var(--hero-color)]">Color from a variable</p>Without the hint, text-[var(--hero-size)] is read as a color, because Tailwind cannot inspect the variable's contents. The same pattern applies to other shared prefixes: bg-[length:200px_100px] sets background size and bg-[url(...)] sets an image.
Variables in v3 and v4
In Tailwind v3 you write the variable inside brackets and wrap it in var(), as above. Tailwind v4 also accepts a shorthand with parentheses, w-(--my-width), which expands to width: var(--my-width). Brackets with var() still work in v4, so code written for v3 keeps working.
Using calc() and math
Brackets accept calc(), which is where arbitrary values do their most useful work. Inside calc(), Tailwind needs spaces around + and - operators, and since class names cannot contain spaces you write underscores.
<div class="h-[calc(100vh-4rem)]">Fills the screen below a 4rem header</div>
<div class="w-[calc(100%_-_2rem)]">Full width minus 2rem</div>Tailwind adds the required spaces around operators in calc() for you in many cases, so h-[calc(100vh-4rem)] works. Underscores are the safe form when you need spaces explicitly, and * and / never need them.
You can also refer to a scale value through CSS in v4, where spacing is driven by a single variable. w-[calc(var(--spacing)*12)] is the same as w-12. That is rarely better than using w-12, but it shows that the scale is just a multiplier.
Negative and arbitrary margins
A leading hyphen on the utility negates the value. With brackets, put the minus either before the utility or inside the brackets. Both forms are valid, and the first is more common.
<div class="-mt-[3px]">Pulled up 3px</div>
<div class="mt-[-3px]">Also pulled up 3px</div>Arbitrary properties
If no utility exists for a property, you can write the property itself in brackets: [property:value]. This is called an arbitrary property.
<div class="[mask-type:luminance] [--card-gap:12px]">
Property and custom property set inline
</div>Arbitrary properties work with variants, so md:[--card-gap:24px] changes a custom property at a breakpoint. Use this sparingly. If you set the same property on several elements, a component or a theme value is a better fit.
When to use arbitrary values
Arbitrary values suit a value that is truly specific to one place. Typical cases:
- A pixel measurement from a design that is not on the scale and does not recur, such as an icon that needs to sit 3px lower.
- A layout that depends on another element's size, such as
h-[calc(100vh-4rem)]. - A third-party widget that needs an exact width.
- A prototype where you have not decided the final scale.
When not to use them
A value that appears in more than a few places should become a theme value. Arbitrary values have three costs.
- They bypass the scale, so spacing drifts. Padding of
13pxin one card and14pxin another looks like a mistake, not a decision. - They are harder to change. Renaming a design token is one edit in the theme. Changing
w-[320px]means a search across files, and a search can miss a value written asw-[20rem]. - They make class lists longer and harder to scan.
A quick test: if you type the same bracketed value a third time, add it to the theme.
Moving a repeated value into the theme
The same 1.5rem gutter, written as an arbitrary value in each place, becomes a named value in the theme. In Tailwind v3, use theme.extend.spacing.
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
spacing: {
gutter: '1.5rem',
},
},
},
}In Tailwind v4, declare a variable in an @theme block.
/* app.css (Tailwind v4) */
@import "tailwindcss";
@theme {
--spacing-gutter: 1.5rem;
}Either way, p-gutter, mt-gutter, and gap-gutter replace p-[1.5rem], mt-[1.5rem], and gap-[1.5rem]. Using extend keeps the default scale. Writing spacing directly under theme in v3 replaces the whole scale, and p-4 would stop working.
Dynamic values: the style-variable pattern
When a value really is computed at runtime, pass it through a CSS variable. The class stays static, so Tailwind can generate it.
<div class="w-[var(--bar-width)]" style="--bar-width: 62%">
Progress bar
</div>Your code changes only the style attribute. The CSS rule exists from the build, and the browser resolves the variable.
Common errors
- Missing unit.
p-[12]produces no rule. Writep-[12px]orp-[0.75rem]. - Spaces in the class.
grid-cols-[1fr 2fr]is split into two classes. Usegrid-cols-[1fr_2fr]. - Text ambiguity.
text-[var(--x)]is treated as a color. Addlength:when you mean size. - Dynamic names. Interpolated class names are never generated, as in the warning above.
- Overriding conflicts.
p-4 p-[7px]on one element has no guaranteed winner by source order of your classes. The order in the generated stylesheet decides. Remove one. - Unexpected escaping in selectors. If you target a bracket class from custom CSS, you must escape the brackets:
.w-\[320px\]. Prefer a component class instead.
Putting it together
This card uses a few arbitrary values, each for one specific reason, alongside the scale.
<article class="mx-auto max-w-prose p-4 md:p-8">
<img
src="/cover.jpg"
alt="Cover"
class="h-[220px] w-full object-cover"
/>
<h2 class="mt-4 text-2xl font-semibold leading-snug">Article title</h2>
<p class="mt-2 text-base/7 text-gray-600">
Summary text that stays at a comfortable line length.
</p>
<a href="#" class="mt-6 inline-block text-sm font-medium tracking-[0.02em]">
Read more
</a>
</article>h-[220px] fixes the image height to match a design, and tracking-[0.02em] applies a slight letter spacing that the preset steps (tracking-wide is 0.025em) do not provide. Everything else uses the scale. If the 220px cover height shows up on every card, it belongs in the theme.
Exercise
A design needs a sidebar that is exactly 18.5rem wide on desktop and a main area that fills the rest of the screen height below a 5rem header. Write the class lists for both using arbitrary values, and say which value you would move into the theme if the sidebar width appeared on six different pages.
Show answer
Sidebar: lg:w-[18.5rem]. Main area: h-[calc(100vh-5rem)]. The sidebar width repeats, so move it into the theme. In v3 add extend.width.sidebar = '18.5rem' and use lg:w-sidebar. In v4 add --width-sidebar: 18.5rem; inside @theme, which produces the same w-sidebar class. The header offset can stay arbitrary unless the header height is used in more places.
Exercise
Why does ` class={mt-[${offset}px]} ` fail to style the element, and what is a working alternative?
Show answer
Tailwind scans source files as text and does not run your code. The string mt-[${offset}px] never appears as a complete class, so no CSS rule is generated. Use a static class that reads a variable, mt-[var(--offset)], and set style="--offset: 12px" (or the equivalent from your framework) with the computed value.