Sub topic 13
Arbitrary values for one-off styles
This chapter assumes you can read basic HTML and CSS, know how Tailwind utility classes work, and have a working Tailwind project or playground. It also assumes you can read hex and RGB color notation. The examples work in Tailwind v3 (3.1 or later) and in v4.
What arbitrary values are
The default theme covers most needs, but sometimes a design calls for a value that is not in it. Examples are a brand color from a third-party guideline, a border exactly 3px wide, or a shadow copied from a design file. Adding each of these to the theme would clutter it.
An arbitrary value is a value you write directly inside square brackets in a class name. Tailwind reads the bracket contents, generates a CSS rule for that exact class, and adds it to your stylesheet.
<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 one element 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.shadow-[0_4px_12px_rgba(0,0,0,0.15)]sets a custom box shadow.
The brackets work with most utilities, such as w-[137px], top-[3px] and text-[15px]. This chapter focuses on colors, borders, and shadows.
Colors in brackets
Any color format that CSS accepts can go inside the brackets. These are all valid:
<p class="text-[#1da1f2]">Hex</p>
<p class="text-[rgb(29,161,242)]">RGB</p>
<p class="text-[hsl(203,89%,53%)]">HSL</p>
<p class="text-[oklch(0.7_0.15_240)]">OKLCH</p>The slash opacity modifier from the earlier chapter still works. Tailwind adds the opacity to the color for you, so bg-[#1da1f2]/10 produces a background that is 10% opaque. The modifier works on hex colors, and in current versions also on rgb(), hsl() and similar functions.
Underscores replace spaces
Class names cannot contain spaces, because a space ends the class. Tailwind therefore turns every underscore inside brackets into a space when it generates the CSS. This is why the shadow above is written with underscores.
| You write | CSS produced |
|---|---|
| `shadow-[0_4px_12px_rgba(0,0,0,0.15)]` | `box-shadow: 0 4px 12px rgba(0,0,0,0.15)` |
| `border-[3px]` | `border-width: 3px` |
| `bg-[#0f172a]` | `background-color: #0f172a` |
If you need a literal underscore, for example in a URL, escape it with a backslash: \_. Tailwind leaves underscores alone inside URLs in many cases, but escaping makes your intent clear.
Telling Tailwind what type the value is
Several utilities share a prefix. border- can set a color or a width. text- can set a color or a font size. shadow- can set a size or a color. When the bracket contents are ambiguous, Tailwind has to guess, and it can guess wrong.
For example, text-[var(--my-value)] could be a color or a size. A CSS variable gives Tailwind no hint. You resolve this by adding a type hint before the value, followed by a colon:
<p class="text-[color:var(--brand)]">Colored text</p>
<p class="text-[length:var(--title-size)]">Sized text</p>
<div class="border-[length:var(--rule)] border-[color:var(--brand)]">Box</div>
<div class="shadow-[color:var(--glow)]">Tinted shadow</div>Common hints are color:, length:, percentage: and shadow:. Values like #1da1f2 or 3px are unambiguous and do not need a hint.
Borders and the width/color split
A border needs both a width and a color, and they are two different utilities. This pair works as expected:
<div class="border-[3px] border-[#1da1f2] border-solid p-4">
3px solid custom-blue border
</div>Here border-[3px] is read as a width because of the unit, and border-[#1da1f2] is read as a color because it is a hex value. Each one generates its own declaration, so the two do not conflict.
Per-side forms work the same way. border-t-[3px] sets only the top width, and border-b-[#16a34a] sets only the bottom color.
Shadows in brackets
A shadow value is a list of parts: horizontal offset, vertical offset, blur radius, optional spread, and color. Write them in that order, joined by underscores.
<div class="rounded-lg bg-white p-6 shadow-[0_8px_30px_rgb(0,0,0,0.12)]">
Soft custom shadow
</div>Several shadows can be stacked with commas. The comma needs no escaping.
<div class="rounded-lg bg-white p-6 shadow-[0_1px_2px_rgba(0,0,0,0.1),0_8px_24px_rgba(0,0,0,0.12)]">
Layered shadow
</div>An inset shadow puts the word inset first:
<div class="rounded-md bg-slate-100 p-4 shadow-[inset_0_2px_4px_rgba(0,0,0,0.1)]">
Pressed look
</div>One limit to know about: a full shadow-[...] value that contains its own color is not affected by a separate shadow-indigo-500/30 class. In Tailwind v3 and v4, the colored-shadow utilities work by setting a variable that the named shadow sizes use. A raw bracket shadow already has its color baked in. If you want a custom shadow whose color can still be changed, keep the color out of the bracket and use a named size with a color class, as in the earlier chapter on colored shadows.
Reading a value from a CSS variable
Brackets can reference a CSS variable. This is useful when a value comes from a design system outside Tailwind, or changes at runtime.
In Tailwind v3, write the variable with var():
<div class="bg-[var(--surface)] border-[color:var(--edge)] p-4">
Variable-driven colors
</div>Tailwind v4 also accepts a shorthand with parentheses, so bg-(--surface) means the same as bg-[var(--surface)]. When the utility could take more than one type, add the hint: border-(color:--edge).
:root {
--surface: #f8fafc;
--edge: #cbd5e1;
}Using arbitrary values with variants
Arbitrary values combine with every variant. Put the variant first, then the class.
<button
class="bg-[#1da1f2] text-white
hover:bg-[#0c85d0]
focus:ring-2 focus:ring-[#1da1f2]/50
dark:bg-[#0c85d0]">
Follow
</button>This button uses one blue at rest, a darker blue on hover, a translucent ring on focus, and a darker background in dark mode.
When to use them, and when not to
Arbitrary values are a good fit when:
- The value appears once, or in one component.
- It comes from an external source and you do not control it.
- You are prototyping and have not decided on a design token yet.
They are a poor fit when:
- The same value appears in many places. A typo in one copy, such as
#1da1f3instead of#1da1f2, produces a color that is nearly right and hard to spot. - The value carries meaning, such as "primary" or "danger". A named theme color documents that meaning and can change in one place.
A practical rule: if you write the same bracket value a third time, move it into the theme. In v3 that means theme.extend.colors in tailwind.config.js. In v4 it means a --color-* variable inside @theme, as covered in the previous chapter on custom colors.
Common errors
- Spaces inside brackets.
shadow-[0 4px 12px black]breaks into several classes and none of them work. Use underscores. - Building class names dynamically. Tailwind finds classes by scanning your source files for complete strings. `
bg-[${color}]` is never seen as a full class, so no CSS is generated. Write whole class names, or switch between complete classes with a lookup table. - Ambiguous prefixes.
text-[var(--x)]might be treated as the wrong type. Addcolor:orlength:. - Forgetting the type for a CSS variable inside a border. A variable alone gives no hint whether it is a width or a color. Add a hint.
- Missing contrast checks. A bracket color skips the palette, so nobody has checked its contrast. Test text and border colors against their backgrounds, as discussed in the contrast chapter.
The second error is the most common. This version fails:
<div class="bg-[${userColor}]">Never generated</div>This version works, because every class is written out in full:
const colors = {
blue: "bg-[#1da1f2]",
green: "bg-[#16a34a]",
};
const className = colors[userChoice];If the color truly comes from user input at runtime, set it with an inline style attribute or a CSS variable instead:
<div class="bg-[var(--user-color)]" style="--user-color: #ff6600">
Runtime color
</div>The class bg-[var(--user-color)] is a complete string that Tailwind can find, and the variable carries the changing part.
Exercise
A teammate writes <div class="border-[var(--rule)]"> expecting a 4px border, with --rule: 4px defined in CSS. The border does not appear as expected. What is the likely cause, and what is the fix?
Show answer
Tailwind cannot tell whether var(--rule) is a width or a color, so it may generate the wrong declaration (a border color). Add a type hint: border-[length:var(--rule)]. Also make sure the border has a style, for example by adding the border class or border-solid, because a width alone does not draw a line when the style is none.
Exercise
Rewrite this broken class so it produces a stacked shadow with a 1px/2px layer and an 8px/24px layer, both black at low opacity: shadow-[0 1px 2px rgba(0,0,0,0.1), 0 8px 24px rgba(0,0,0,0.12)].
Show answer
Replace the spaces with underscores: shadow-[0_1px_2px_rgba(0,0,0,0.1),0_8px_24px_rgba(0,0,0,0.12)]. Spaces end a class name, so the original is split into separate invalid classes.