Sub topic 13
One-off values with square-bracket syntax
This chapter assumes you know the spacing scale, the padding, margin, width, and font-size utilities, and how to read a Tailwind class list. Sometimes the scale has no value that fits. Square-bracket syntax lets you pass a literal CSS value to a utility without leaving your HTML.
The syntax
Put the value in square brackets after the utility prefix. Tailwind generates a class that sets exactly that value.
<div class="w-[320px] mt-[3px] text-[18px]">One-off values</div>These classes produce the following CSS:
| Class | CSS declaration |
|---|---|
| `w-[320px]` | `width: 320px;` |
| `mt-[3px]` | `margin-top: 3px;` |
| `text-[18px]` | `font-size: 18px;` |
Any unit works: px, rem, %, vh, ch. Tailwind reads the class name as written and generates CSS for it, so the value must appear literally in your source files.
Why the scale is usually better
The scale keeps spacing consistent. If every gap is a multiple of the same step, the page looks regular. A bracket value skips that check, so mt-[3px] next to mt-4 produces an uneven rhythm that nobody chose on purpose.
Use brackets when the value is a real exception:
- Matching a fixed size from a design, such as a 320px sidebar.
- Aligning to a third-party element, such as a 3px offset to line up with an icon.
- A font size for a single marketing headline.
The value must be written whole
Tailwind scans your files for complete class names. It does not run your code. A class built from pieces will not be found, and no CSS is generated for it.
<!-- Works: the full class name appears in the source -->
<div class="w-[320px]">Fixed</div>
<!-- Does not work: Tailwind never sees "w-[320px]" as a whole -->
<div class="w-[${width}px]">Dynamic</div>If the value is only known at runtime, set it with an inline style attribute or a CSS variable instead.
Spaces and calculations
Spaces are not allowed inside a class name. Use an underscore where you would write a space. Tailwind converts it back.
<div class="w-[calc(100%_-_2rem)]">Full width minus 2rem</div>This generates width: calc(100% - 2rem);. Without the underscores the class would be split into several separate classes.
Resolving ambiguity with a type hint
Some prefixes accept more than one kind of value. text- sets font size and also color, so text-[#336699] is clear but text-[var(--x)] is not. Add a type before the value to say which you mean.
<p class="text-[length:var(--title-size)]">Sized from a variable</p>
<p class="text-[color:var(--title-color)]">Colored from a variable</p>When a value repeats, move it to the theme
One bracket value is fine. Five copies of p-[1.5rem] are a sign the value deserves a name. Add it to the theme, and the class becomes p-gutter. Customizing the theme is the next chapter's topic.
<!-- Repeated: hard to change everywhere -->
<section class="p-[1.5rem]">A</section>
<section class="p-[1.5rem]">B</section>Combining with variants
Bracket values work with any variant, so a one-off can still be responsive.
<aside class="w-full lg:w-[320px]">
Full width on small screens, 320px from lg upward.
</aside>Exercise
A component has class="w-[calc(100% - 2rem)]" and the width is not applied. What is wrong, and how do you fix it?
Show answer
The class contains spaces, so the browser splits it into separate classes and Tailwind cannot read it as one utility. Replace each space inside the brackets with an underscore: w-[calc(100%_-_2rem)].
Exercise
You build the class with a template string, ` mt-[${gap}px] `, and no margin appears. Why, and what are two alternatives?
Show answer
Tailwind scans source text for complete class names and never sees mt-[12px] as a whole, so it generates no CSS. Use an inline style attribute (style="margin-top: 12px"), set a CSS variable and use mt-[var(--gap)], or choose from a fixed set of full class names.