Sub topic 2
Arbitrary values with square brackets
This chapter assumes you know basic HTML and CSS, including units and selectors. It also assumes you have used Tailwind utilities such as p-4 and text-lg, and variant prefixes such as hover: and md:.
When the theme has no matching value
Most Tailwind utilities read their values from the theme. w-64 works because the theme defines a spacing scale, and bg-blue-500 works because the theme defines a color palette. A design sometimes needs a value that is not in the theme. Examples are a banner that must be 317px wide, a brand color used once, or a position taken from a design file.
Square brackets cover these cases. Put the value in brackets after the utility name, and Tailwind generates a rule with exactly that value. This syntax is called an arbitrary value.
The syntax
An arbitrary value has the form utility-[value]. Tailwind keeps the utility name and replaces the theme lookup with your value.
<div class="absolute top-[117px] left-[calc(50%-2rem)] w-[317px] bg-[#bada55]"></div>Each class maps to one declaration:
| Class | Generated declaration |
|---|---|
| `top-[117px]` | `top: 117px` |
| `left-[calc(50%-2rem)]` | `left: calc(50% - 2rem)` |
| `w-[317px]` | `width: 317px` |
| `bg-[#bada55]` | `background-color: #bada55` |
The calc() class is only a preview here. Tailwind adds spaces around the operators for you. The details of calc() come in a later chapter.
The bracket contents can be any valid CSS value for that property. Lengths, percentages, colors, and keywords all work. Tailwind decides which CSS property to set from the utility name, so w-[...] always sets width and top-[...] always sets top.
How Tailwind turns a class into CSS
Tailwind reads each class name, splits it into a utility and a bracketed value, and writes one CSS rule. The class name is escaped in the selector, so the brackets are legal.
The generated rule for w-[317px] looks like this:
.w-\[317px\] {
width: 317px;
}The backslashes escape the bracket characters in the selector. You never write them yourself.
A working example
Save this file and open it in a browser. It loads Tailwind v4 from a CDN, which is meant for experiments and not for production.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Arbitrary values</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body>
<div class="relative h-[200px] w-[400px] border">
<div class="absolute top-[117px] left-[24px] h-[41px] w-[317px] bg-[#bada55]"></div>
</div>
</body>
</html>The green bar is 317px wide and 41px tall. It sits 117px below the top of its 200px-tall container and 24px from the left edge. None of these numbers exist in the default theme, and nothing was added to a config file.
Mixing theme values and arbitrary values
Arbitrary values sit next to normal utilities in the same class attribute. Use the theme for everything it covers and brackets only for the exceptions.
<div class="flex items-center gap-4 p-6 w-[317px] bg-white">
Card
</div>Here flex, gap-4, and p-6 come from the theme. Only the width is a one-off.
Common errors
- Missing unit.
w-[317]produceswidth: 317, which browsers ignore because the value has no unit. Writew-[317px]. - Negative values. Put the minus sign inside the brackets:
top-[-117px]. - Spaces inside brackets. A space ends the class name, so
grid-cols-[1fr 500px]is read as two separate classes. The next chapter shows how to write spaces. - Unknown utility names.
size-[3px]works becausesizeis a utility. A made-up name such aspad-[3px]generates nothing, and Tailwind gives no error. - Wrong property chosen. For some utilities, such as
text-[...], the same name can set more than one property. Tailwind has to guess which one you mean from the value. Hex colors resolve clearly, but other values can be ambiguous. A later chapter covers this.
If a class has no effect, inspect the element in your browser's developer tools. If the rule is missing from the stylesheet, check the utility name and the syntax. If the rule is present but crossed out, the value is invalid CSS or another rule overrides it.
Exercise
A design needs a box 317px wide and 41px tall with the background color #bada55. Write the Tailwind classes using arbitrary values.
Show answer
w-[317px] h-[41px] bg-[#bada55]
Exercise
A developer writes w-[317] and the element does not get a width. What is wrong, and what is the fix?
Show answer
The value has no unit, so Tailwind generates width: 317, which is invalid CSS and ignored. Write w-[317px].