Sub topic 3
Spaces, underscores and escaping
This chapter assumes you know basic HTML and CSS, including units and custom properties. It also assumes you can read Tailwind utilities such as p-4 and variants such as hover:. You should already know that square brackets turn a utility into an arbitrary value, such as w-[317px]. Here you will learn how to write values that contain spaces, and how to keep a literal underscore.
Why spaces need a stand-in
Tailwind reads class names from your source files as plain text. The class attribute is split on whitespace, so each space starts a new class. A value like 1fr 500px 2fr cannot sit inside one class name, because Tailwind would see three separate tokens.
Tailwind solves this with a convention. Inside square brackets, each underscore becomes a space in the generated CSS.
<div class="grid grid-cols-[1fr_500px_2fr] gap-[10px]">
<div>Sidebar</div>
<div>Content</div>
<div>Aside</div>
</div>The class grid-cols-[1fr_500px_2fr] produces this declaration:
grid-template-columns: 1fr 500px 2fr;Where you will need this
Any CSS value that takes several space-separated parts needs underscores. Common cases include:
- Grid templates, such as
grid-cols-[200px_minmax(900px,_1fr)_100px]. - Shadows, such as
shadow-[0_4px_12px_rgba(0,0,0,0.2)]. - Shorthand values, such as
[margin:4px_8px]orbg-[url('/img/hero.png')]. - Operators in
calc(), which CSS requires to have spaces around+and-.
The last case is easy to miss. In CSS, calc(50%-2rem) is invalid, because the minus sign needs spaces on both sides. Tailwind adds the spaces around the operators in calc() for you, so you can write the class without underscores:
<div class="absolute left-[calc(50%-2rem)]"></div>The generated CSS is left: calc(50% - 2rem). Writing the underscores yourself, as in left-[calc(50%_-_2rem)], gives the same result, and some teams prefer it because it matches what the CSS looks like.
Commas do not need a stand-in. minmax(900px,_1fr) uses an underscore only because CSS usually has a space after the comma. Without it, minmax(900px,1fr) is still valid CSS.
Keeping a literal underscore
Some values contain a real underscore, such as a file name or a font family name. If you write the underscore as is, Tailwind turns it into a space, which breaks the value. To keep it, put a backslash in front of it.
<div class="bg-[url('/img/hero\_banner.png')]"></div>Tailwind removes the backslash and keeps the underscore, so the CSS is url('/img/hero_banner.png').
Some cases are already handled for you. Tailwind does not convert underscores inside a url() value. This makes the common case work without any escaping:
<div class="bg-[url('/img/hero_banner.png')]"></div>Escaping is still safe in a url(), and it is the rule to remember for every other value.
A worked example
This card combines several of the ideas above. It uses a grid with three columns, a layered shadow, and a position computed with calc().
<div class="relative grid grid-cols-[1fr_500px_2fr] gap-[10px] shadow-[0_4px_12px_rgba(0,0,0,0.2)]">
<div class="absolute top-0 left-[calc(50%-2rem)]">Badge</div>
<div>Sidebar</div>
<div>Content</div>
<div>Aside</div>
</div>If you wrote shadow-[0 4px 12px rgba(0,0,0,0.2)] with real spaces, the browser's HTML parser would split it into four class names. None of them would match a utility, so the element would get no shadow and Tailwind would report no error. A missing style with no warning is the typical sign of a space where an underscore belongs.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Style does not apply, no error | Real space inside the brackets | Replace each space with `_` |
| Filename or font name has a space in the output | Underscore was converted | Write `\_` to keep it |
| `calc()` is ignored by the browser | Operators written without spaces in a form Tailwind did not adjust | Write `_+_`, `_-_`, `_*_` or `_/_` explicitly |
| Backslash appears in the CSS | Escape used where it was not needed | Remove the backslash |
You can check a result quickly by opening your browser's developer tools and reading the computed declaration on the element. If it shows 1fr 500px 2fr, the underscores were converted as expected.
Exercise
You want a utility that sets font-family to a font whose name is Open_Sans_Custom, with the underscores kept. You also want the box shadow 0 2px 6px black. Write the two arbitrary classes, font-[...] and shadow-[...], so the generated CSS keeps the underscores in the font name and uses spaces in the shadow.
Show answer
Use font-[Open\_Sans\_Custom] and shadow-[0_2px_6px_black]. The backslashes keep the underscores in the font name. The unescaped underscores in the shadow become spaces, which gives 0 2px 6px black.