Sub topic 13
Arbitrary values for one-off styles
This chapter assumes you can write Tailwind utility classes, know hex and RGB color notation, and have a working Tailwind project or playground. It covers square-bracket syntax for colors, borders, and shadows that are not in your theme.
When to use arbitrary values
The default palette and scales cover most designs. Sometimes you need a value that appears once, such as a brand color from a third-party logo or a shadow copied from a design file. Adding that value to the theme would clutter it. Tailwind lets you write the value directly in square brackets instead.
Use the theme for values you reuse. Use arbitrary values for values you use once.
The bracket syntax
Put a CSS value in square brackets after the utility prefix. Tailwind generates a class with exactly that value.
<div class="border border-[#1da1f2] bg-[#1da1f2]/10 p-4 shadow-[0_4px_12px_rgba(0,0,0,0.15)]">
One-off values
</div>This example uses three arbitrary values:
border-[#1da1f2]sets the border color to a hex value.bg-[#1da1f2]/10sets the same color as the background at 10% opacity. The slash opacity modifier works with arbitrary colors.shadow-[0_4px_12px_rgba(0,0,0,0.15)]defines a full box shadow. Underscores stand in for spaces, because class names cannot contain spaces.
Common patterns
| Goal | Class | Generated value |
|---|---|---|
| Hex text color | `text-[#336699]` | `color: #336699` |
| RGB background | `bg-[rgb(20,30,40)]` | `background-color: rgb(20,30,40)` |
| Border width | `border-[3px]` | `border-width: 3px` |
| Radius | `rounded-[10px]` | `border-radius: 10px` |
| Custom shadow | `shadow-[0_2px_8px_#00000033]` | `box-shadow: 0 2px 8px #00000033` |
Resolving ambiguity
Some prefixes accept more than one kind of value. text-[...] can set a color or a font size, and border-[...] can set a width or a color. Tailwind usually infers the type from the value. When it cannot, add a type hint before the value.
<p class="text-[length:var(--title-size)] border-[color:var(--edge)] border-2">
Type hints for CSS variables
</p>Here length: and color: tell Tailwind which property to generate. Without them, text-[var(--title-size)] is ambiguous, because a variable could hold either type.
Common errors
- Spaces in the value.
shadow-[0 4px 12px black]breaks the class into several tokens. Writeshadow-[0_4px_12px_black]. - Dynamic class names. Tailwind scans your source for complete class strings. A class built as `
bg-[${color}]` is never generated. Write out the full class, or use a CSS variable. - Overuse. Many repeated arbitrary values suggest a missing theme entry. Move the value to the theme when it appears in more than a few places.
- Contrast. Arbitrary colors skip the tested shade scale, so check text and border contrast yourself.
Choosing between the options
Exercise
You need a card with a 2px border in the color #1da1f2 and a shadow of 0 4px 12px at 15% black. Write the utility classes, and say why the shadow class uses underscores.
Show answer
Use border-2 border-[#1da1f2] shadow-[0_4px_12px_rgba(0,0,0,0.15)]. Class names cannot contain spaces, so underscores stand in for the spaces in the CSS value, and Tailwind converts them back when it generates the rule.
Exercise
A teammate writes <div class="bg-[${brand}]"> inside a JavaScript template string, and no background appears. What is wrong and how do you fix it?
Show answer
Tailwind reads source files as text and only generates classes that appear as complete strings, so the interpolated class is never created. Write each full class name, such as bg-[#6366f1], or set a CSS variable and use bg-[var(--brand)].