Sub topic 10
When to Use Arbitrary Values and When to Extend the Theme
What this chapter assumes
You have worked through the earlier chapters of this tutorial, or you already know the following:
- Square-bracket arbitrary values such as
w-[317px], arbitrary properties such as[mask-type:luminance], and arbitrary variants such as[&>li]:p-2. - How underscores become spaces, and how type hints like
length:andcolor:work. - Why Tailwind needs complete class names in your source files.
- Basic HTML and CSS, including custom properties.
This chapter is about judgment. Every arbitrary value is a choice not to add a value to the theme. Sometimes that choice is right and sometimes it is not. You will learn the criteria for deciding, how to move a value into the theme, and what to do when the value is only known at runtime.
What each tool is for
The theme is your design system's vocabulary: spacing steps, colors, font sizes, breakpoints. A class like bg-brand or text-hero says what the element is, and the value lives in one place.
An arbitrary value is a one-off exception. It says "this element needs exactly this number", and the number lives in the markup.
Neither is better in general. They answer different questions:
| Question | Points to the theme | Points to an arbitrary value |
|---|---|---|
| How many places use this value? | Three or more | One |
| Does the value have a name in the design? | Yes ("brand blue", "gutter") | No, it comes from a layout quirk |
| Should it change together everywhere? | Yes | No, it is tied to this element |
| Is the value derived from other values? | Rarely | Often (`calc()`, a sibling's size) |
| Is there a utility for the property? | Yes, and the value is a token | No, so use an arbitrary property |
A decision path
The diagram below puts the criteria in order. Start at the top with the style you want to write.
The rest of the chapter explains each branch.
Branch 1: check the theme first
Before you write brackets, check whether the value already exists. A quick scan prevents a common mistake: writing p-[16px] when p-4 produces the same CSS, or w-[50%] when w-1/2 does.
Some value pairs that look different are identical:
| Arbitrary form | Theme form | Same output? |
|---|---|---|
| `p-[16px]` | `p-4` | Yes, with default spacing (`1rem` at a 16px root) |
| `w-[50%]` | `w-1/2` | Yes |
| `text-[#ef4444]` | `text-red-500` | Close. Check the palette. It may differ by version |
| `mt-[13px]` | none | No theme step matches, so brackets are reasonable |
The first row is subtle. 16px and 1rem are only equal when the user's root font size is 16px. The theme uses rem, so it follows the user's font settings. A pixel arbitrary value does not. If you want sizes that scale with user preferences, prefer rem in brackets, such as p-[1.0625rem], or use the theme.
Branch 2: a repeated value belongs in the theme
The strongest signal is repetition. Suppose you see this across a codebase:
<button class="bg-[#3b5bdb] hover:bg-[#364fc7] text-white">Save</button>
<a class="text-[#3b5bdb] underline decoration-[#3b5bdb]">Docs</a>
<div class="border-[#3b5bdb] border-2">Card</div>The same hex value appears five times. If the brand color changes, you must find every occurrence, and a search for 3b5bdb will miss any that someone wrote as #3B5BDB or as rgb(). Move it to the theme once.
Adding a value in Tailwind v4
In Tailwind v4, the theme lives in your CSS file. You declare theme variables inside @theme, and Tailwind generates utilities from the variable name:
@import "tailwindcss";
@theme {
--color-brand: #3b5bdb;
--color-brand-dark: #364fc7;
--text-hero: 4.5rem;
--breakpoint-3xl: 120rem;
}The prefix of each variable decides which utilities it creates. --color-brand gives you bg-brand, text-brand, border-brand, and decoration-brand. --text-hero gives you text-hero. --breakpoint-3xl gives you the 3xl: variant. The markup now reads:
<button class="bg-brand hover:bg-brand-dark text-white">Save</button>
<a class="text-brand underline decoration-brand">Docs</a>
<div class="border-brand border-2">Card</div>Adding a value in Tailwind v3
In v3 the same change goes in tailwind.config.js. Put it under theme.extend so you add to the defaults instead of replacing them:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {
colors: {
brand: {
DEFAULT: "#3b5bdb",
dark: "#364fc7",
},
},
fontSize: {
hero: "4.5rem",
},
screens: {
"3xl": "120rem",
},
},
},
};This produces the same class names: bg-brand, hover:bg-brand-dark, text-hero, and 3xl:.
What you gain by moving a value
- One place to change. Editing
--color-brandupdates every use. - Names that carry meaning.
bg-brandexplains intent.bg-[#3b5bdb]does not. - Variants for free. Theme values work with
hover:,md:,dark:and the rest, and so do arbitrary values, but theme names are shorter in a long variant stack. - Editor support. Autocomplete and linting know your theme names. They cannot know your one-off numbers.
Branch 3: a one-off value belongs in brackets
Not every number deserves a name. Adding --spacing-13px to fix a single icon alignment makes the theme worse, because it fills with tokens nobody can describe.
Arbitrary values are the right choice when the value:
- Appears in one place, or in one component that is itself the only copy.
- Compensates for a quirk: an optical alignment offset, a pixel-perfect match to an image, a third-party embed's fixed width.
- Is computed from other values, as in
left-[calc(50%-2rem)]. - Needs a property with no utility, as in
[mask-type:luminance].
For example, a sticky header might need a scroll offset that depends on the header height at two breakpoints. That is a local detail of one layout:
<section class="[--scroll-offset:56px] lg:[--scroll-offset:44px] scroll-mt-[var(--scroll-offset)]">
<h2>Pricing</h2>
</section>Naming --scroll-offset in the global theme would suggest every page should share it, and that is not true.
A good middle path: theme variables inside brackets
If a value has a name but you do not want a dedicated utility, a theme variable is still available as a CSS custom property. In v4, every @theme variable is emitted as a real CSS variable on :root. You can read one with var():
<div class="p-[calc(var(--spacing)*3.5)] border-[color:var(--color-brand)]"></div>This keeps one source of truth while using bracket syntax for the odd case. Note the color: hint, which is needed because border-[...] could mean a width or a color.
Branch 4: values known only at runtime
Sometimes the value comes from data: a user-chosen width, a progress percentage, a color from a database. You cannot write that value in markup ahead of time, and the previous chapter showed why you cannot build the class name from it:
// Not detected: Tailwind never sees the complete class name
const broken = `w-[${width}px]`;
// Detected: every class name appears in full in the source
const widths = {
narrow: "w-[240px]",
wide: "w-[480px]",
};
const working = widths[size];The map works when the set of values is small and known. When it is open-ended, use a static class that reads a CSS variable, and set the variable per element with the style attribute:
<div class="h-2 w-[var(--progress)] bg-brand rounded" style="--progress: 62%"></div>The class w-[var(--progress)] is complete and visible in the source, so Tailwind generates it. Your script only changes the inline variable:
const bar = document.querySelector("[data-progress-bar]");
bar.style.setProperty("--progress", `${percent}%`);This pattern keeps the data in JavaScript and the styling rules in CSS. It also avoids generating a new class for every possible number.
Costs of overusing arbitrary values
A page full of brackets is not wrong, but it carries costs that grow with scale.
- Drift. Two designers type
#3b5bdband#3b5bda. Nothing flags the mismatch. - Unreadable class lists. A long list such as
grid-cols-[1fr_500px_2fr] gap-[10px] p-[17px] mt-[13px]makes the reader decode each value. Named values read faster. - No global change. A redesign that adjusts the spacing scale will skip every pixel value you hard-coded.
- Weaker tooling. Sorting plugins, linters and autocomplete work best with theme names.
- Larger CSS output. Each distinct arbitrary value generates its own rule. A few hundred is usually harmless, but identical values written two ways (
#BADA55and#bada55) generate two rules.
The opposite mistake also exists. A theme that grows a new entry for every design tweak becomes a list of near-duplicates like --spacing-17 and --spacing-18, which is the same drift problem in a different place. The theme should hold decisions, not measurements.
A review checklist
When you review a pull request that adds brackets, ask these questions in order:
- Does a theme value produce the same result? If so, use the utility.
- Does the same bracket value already appear elsewhere? If so, promote it.
- Does the value have a name in the design file or a brand guide? If so, promote it.
- Is the value built from a template string? If so, rewrite it with a full-name map or a CSS variable.
- Is it a local fix with a reason? If so, keep the brackets. Add a short comment in the surrounding component if the reason is not obvious.
Worked example: promoting a value in stages
Start with a card that uses two one-off values.
<article class="w-[317px] rounded-[10px] bg-[#bada55] p-[18px]">
<h3 class="text-[length:var(--card-title)]">Plan</h3>
</article>A second and third card appear with the same radius and the same green. Those two values repeat and have meaning, so they move to the theme. The width is specific to this card's image and the padding matches a design that is not shared, so they stay.
In v4, the theme gains two entries:
@theme {
--color-accent: #bada55;
--radius-card: 10px;
}The markup becomes:
<article class="w-[317px] rounded-card bg-accent p-[18px]">
<h3 class="text-[length:var(--card-title)]">Plan</h3>
</article>Two of the four arbitrary values moved. The result is shorter, and a future color change touches one line. The remaining brackets still communicate "this is a local exception".
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The class is in the HTML but has no effect | The class name was built dynamically, so Tailwind never generated it | Write the full class name, or use a CSS variable with `style` |
| `bg-brand` does nothing | The theme key has the wrong prefix, such as `--brand` instead of `--color-brand` | Rename the variable to the correct namespace |
| In v3, default colors vanished | `colors` was placed outside `extend` | Move it into `theme.extend` |
| `text-[var(--x)]` sets the wrong property | Ambiguity between font size and color | Add `length:` or `color:` |
| A promoted value still shows the old color in some places | Older bracket copies remain | Search for the hex value in both cases, and for the `rgb()` form |
Summary of the rule
Use the theme for values that have a name, repeat, or should change together. Use brackets for values that are local, computed, or tied to a property with no utility. Use a CSS variable set through style for values that arrive at runtime. When you notice the second copy of a bracket value, that is the moment to promote it.
Exercise
A codebase has bg-[#0ea5e9] in six components, and mt-[13px] in one icon wrapper to align it with a text baseline. Which one should move to the theme, and which should stay as an arbitrary value? Give the reason for each.
Show answer
Move #0ea5e9 to the theme (for example --color-sky-brand in v4 or colors.skyBrand under theme.extend in v3), because it repeats six times and a color change should update all of them at once. Keep mt-[13px], because it is a one-off optical alignment fix with no design meaning, and giving it a theme name would add a token nobody could explain.
Exercise
A progress bar's width is a percentage from an API, so a developer writes ` w-[${percent}%] ` in a template string. Explain why the bar does not render correctly, then rewrite the markup and the script so it works.
Show answer
Tailwind scans source files as plain text and only generates classes that appear in full. The string w-[${percent}%] never appears as a complete class name, so no CSS rule exists for it. Use a static class that reads a variable, such as <div class="h-2 w-[var(--progress)] bg-brand" data-bar></div>, and update the value from script with bar.style.setProperty("--progress", percent + "%"). The class name is complete in the source, and only the inline variable changes.
Exercise
In a Tailwind v3 project, you add theme: { colors: { brand: "#3b5bdb" } } and bg-blue-500 stops working everywhere. What went wrong, and what is the fix?
Show answer
Placing colors directly under theme replaces the entire default palette with only brand. Move it under theme.extend, so theme: { extend: { colors: { brand: "#3b5bdb" } } }. The default colors remain and brand is added.