Sub topic 2
Arbitrary values with square brackets
This chapter assumes you know basic HTML and CSS (units, custom properties, selectors). It also assumes you have used Tailwind utilities like p-4, text-lg and bg-blue-500, and variant prefixes like hover: and md:. Here you will learn the core idea of this tutorial: putting a one-off value in square brackets. Later chapters build on it with underscores, type hints, CSS variables, arbitrary properties and arbitrary variants.
Why the theme is not always enough
Every utility class like w-64, bg-blue-500 or rounded-lg maps to a value in Tailwind's theme. The theme is the set of named design tokens: the spacing scale, the color palette, the font sizes, the breakpoints. Using only theme values keeps a design consistent, and it is the right default.
Real designs sometimes need a value that is not in the theme. A mockup may call for a 117px offset from the top, a brand color used once, or a width of exactly 317px. You have three options:
- Pick the nearest theme value and accept a small mismatch.
- Add a new token to the theme.
- Write the value directly in the class name with square brackets.
This chapter covers the third option. It is the fastest, and it works well for values that are truly one-off. Section "Deciding whether a value should stay arbitrary" at the end gives rules for when to choose option 2 instead.
The syntax
An arbitrary value is a utility name, a hyphen, and a value in square brackets:
<div class="absolute top-[117px] left-[calc(50%-2rem)] w-[317px] bg-[#bada55]"></div>Each class follows the same pattern:
| Part | Example | Meaning |
|---|---|---|
| Utility name | `top` | Which CSS property family the class controls |
| Hyphen | `-` | Separator, as in any utility |
| Bracketed value | `[117px]` | The value, used instead of a theme lookup |
Tailwind skips the theme lookup and takes the text inside the brackets as the value for the property. For the four classes above, the generated CSS is approximately this:
.top-\[117px\] { top: 117px; }
.left-\[calc\(50\%-2rem\)\] { left: calc(50% - 2rem); }
.w-\[317px\] { width: 317px; }
.bg-\[\#bada55\] { background-color: #bada55; }Two details are worth noticing.
- The class name becomes the CSS selector. The backslashes escape characters like
[,],(,%and#, which are not allowed in a plain CSS selector. You never write these backslashes yourself. You will see them when you search generated CSS or inspect an element in devtools. - Tailwind adds spaces around the minus sign in
calc(50%-2rem). CSS requires whitespace around+and-insidecalc(), and Tailwind normalizes that for math expressions. The next chapters cover spaces in more detail.
How Tailwind processes a bracketed class
The build step goes through the same stages for every arbitrary value.
Because the scanner reads your files as plain text, the full class name must appear in the source. A later chapter in this tutorial covers what that means for code that builds class names from variables.
What you can put in the brackets
Most utilities that accept a theme value also accept an arbitrary one. The value is a CSS value written without spaces.
| Class | Generated declaration |
|---|---|
| `w-[317px]` | `width: 317px` |
| `h-[100dvh]` | `height: 100dvh` |
| `top-[117px]` | `top: 117px` |
| `top-[-117px]` | `top: -117px` |
| `rounded-[12px]` | `border-radius: 12px` |
| `bg-[#bada55]` | `background-color: #bada55` |
| `text-[22px]` | `font-size: 22px` |
| `text-[#bada55]` | `color: #bada55` |
| `leading-[1.15]` | `line-height: 1.15` |
| `z-[60]` | `z-index: 60` |
| `aspect-[4/3]` | `aspect-ratio: 4/3` |
| `grid-cols-[1fr_500px_2fr]` | `grid-template-columns: 1fr 500px 2fr` |
| `bg-[url('/hero.jpg')]` | `background-image: url('/hero.jpg')` |
| `content-['Hi']` | `content: 'Hi'` |
The table shows several useful points.
- One prefix, several properties.
text-sets eitherfont-sizeorcolor. Tailwind looks at the bracketed value to decide:22pxis a length, and#bada55is a color. That works when the value itself reveals its type. When it does not (for example a CSS variable), you need a type hint, which is the subject of a later chapter. - Negative values go inside the brackets. Write
top-[-117px]rather than relying on a leading minus on the class name. - Units are required where CSS requires them.
w-[317]is not a valid width in CSS, because lengths other than zero need a unit. - Quotes inside the brackets are allowed.
url('/hero.jpg')andcontent-['Hi']pass the quotes through to CSS. - The underscore stands for a space. In
grid-cols-[1fr_500px_2fr], a class name cannot contain a space, so Tailwind converts each underscore to a space. The next chapter explains this rule and how to write a literal underscore.
Colors and opacity
An arbitrary color works with Tailwind's opacity modifier, the slash syntax:
<div class="bg-[#bada55]/50 text-[#1a1a1a]">Half-transparent green background</div>The /50 applies 50% opacity to the color. You can also write an arbitrary opacity, such as bg-blue-500/[0.37], which mixes a theme color with a one-off opacity.
Mixing with theme classes
Arbitrary values do not replace the theme. They are one more kind of utility that you can combine with the rest. A typical element uses mostly theme classes and one or two arbitrary ones.
A complete worked example
The page below places a small square in a container. The square is centered horizontally with a calc() expression and sits 117px from the top. Save it as index.html and open it in a browser. It loads the Tailwind v4 browser build from a CDN, so you need an internet connection and nothing else.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Arbitrary values</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body>
<div class="relative h-[240px] w-[480px] bg-[#f4f1ea]">
<div class="absolute top-[117px] left-[calc(50%-2rem)] h-16 w-16 rounded-[12px] bg-[#bada55]"></div>
</div>
</body>
</html>Walk through what each class does:
relative h-[240px] w-[480px] bg-[#f4f1ea]makes a 480 by 240 pixel container with a cream background. Therelativeclass matters. It makes the container the reference box for the absolutely positioned child.absolutetakes the square out of normal flow and positions it relative to the container.top-[117px]places the top edge of the square 117px below the top of the container.left-[calc(50%-2rem)]puts the left edge at the container's horizontal middle, minus 2rem. The square isw-16, which is 4rem wide in the default theme. Half of that is 2rem, so the square is centered.h-16 w-16are theme classes. They sit next to arbitrary classes without any special syntax.rounded-[12px]andbg-[#bada55]finish the look.
Try changing 117px to 20px, then to -10px, and watch the square move. Then change w-[480px] to w-[640px]. The square stays centered, because 50% is computed from the container.
Behavior to know before you rely on it
Tailwind does not validate the value
Tailwind decides which property to set from the utility name and the value's type. It does not check that your value is sensible for that property. If the browser can't parse the result, the declaration is dropped and the class appears to do nothing. If Tailwind can't tell what type the bracket holds, it may generate no rule at all.
When an arbitrary class has no effect, work through this checklist:
- Search the generated CSS (or devtools) for the escaped selector. If it is missing, Tailwind did not generate it.
- If the rule exists, check whether the browser crossed out the declaration as invalid. Look for a missing unit or a typo in a function name.
- If the declaration is valid but nothing changes, the cause is regular CSS: a positioned-element rule, a more specific selector, or a parent that clips the element.
Brackets must balance and spaces are not allowed
A class name ends at whitespace. Writing w-[calc(100% - 2rem)] in a class attribute gives the browser two separate classes, w-[calc(100% and - and so on, neither of which is valid. Remove the spaces, or use underscores where a space is required in the final CSS.
Every distinct value is a separate rule
w-[317px] and w-[318px] produce two different CSS rules. Tailwind generates only the classes it finds, so the cost of one use is small. The cost that matters is in maintenance. If the same 317px appears in twelve templates, changing it means twelve edits.
Unitless numbers for spacing in Tailwind v4
Do not build the bracket from variables
The value in brackets must be written out in full in your source files. An expression like ` w-[${width}px] ` produces a class name that never appears in your code as text, so Tailwind cannot find it. This is the most common source of "it works when I type it but not when I compute it" bugs, and a later chapter in this tutorial covers it in full.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Class has no effect and no CSS rule exists | Typo in the utility name, or a value Tailwind can't interpret | Check the utility name and write the value with its unit |
| Rule exists but the browser ignores the declaration | Invalid CSS value, such as a missing unit | Fix the value and read it in devtools |
| `top-[117px]` has no effect | The element is not positioned | Add `relative`, `absolute`, `fixed` or `sticky` as appropriate |
| Part of a `calc()` appears as separate classes | A space inside the brackets | Remove the spaces or use underscores |
| Works with a literal class, fails with a variable | Class name built by interpolation | Write every complete class name in the source |
| The same value is repeated across many files | A design token hiding as a one-off | Add it to the theme (see below) |
Deciding whether a value should stay arbitrary
A later chapter covers this decision in depth. Until then, use these quick rules.
- Keep it arbitrary when the value is used in one place, when it comes from a specific layout constraint (such as aligning with an image of known size), or when you are prototyping.
- Move it to the theme when it appears in three or more places, when it is a brand color or a spacing step, or when you want it to change in one place for the whole site.
Arbitrary values are an escape hatch. Using one is not a mistake, but a design that is mostly brackets has lost the consistency the theme provides.
Exercises
Exercise
You write <div class="absolute left-[calc(50%-2rem)] top-[117px] w-16 h-16 bg-[#bada55]"></div> directly inside <body>. The square appears at an unexpected position, not inside any container. What is the most likely reason, and what is the smallest change that makes top and left relative to a container you add?
Show answer
An absolutely positioned element is placed relative to its nearest positioned ancestor, and with none it uses the page (initial containing block). Wrap the square in a parent with relative, for example <div class="relative h-[240px] w-[480px]">, so top-[117px] and left-[calc(50%-2rem)] are measured from that parent.
Exercise
For each class, say which CSS declaration it generates: w-[37.5%], text-[#bada55], text-[22px], top-[-8px].
Show answer
w-[37.5%] gives width: 37.5%. text-[#bada55] gives color: #bada55, because the value is a color. text-[22px] gives font-size: 22px, because the value is a length. top-[-8px] gives top: -8px.
Exercise
A teammate writes <div class="w-[calc(100% - 2rem)]"> and the width never changes. Explain why, and give two ways to write it so it works.
Show answer
HTML splits the class attribute at whitespace, so the spaces turn the one intended class into several broken ones. Remove the spaces, as in w-[calc(100%-2rem)], which Tailwind normalizes to calc(100% - 2rem). Alternatively, use underscores in place of the spaces, as in w-[calc(100%_-_2rem)].