Sub topic 10
When to use arbitrary values and when to extend the theme
This chapter assumes you can write arbitrary values (w-[317px]), arbitrary properties ([mask-type:luminance]) and arbitrary variants. It also assumes you know why Tailwind needs complete class names in your source. Here you will decide which tool fits a given style.
The core question
Arbitrary values and theme values both produce utility classes. They differ in what they say about the design.
- A theme value is a named decision.
bg-brandsays "this is the brand color," and one definition controls every use. - An arbitrary value is a local decision.
bg-[#bada55]says "this element needs this exact color," and nothing links it to any other element.
If a value is a local decision, use brackets. If it is a design decision, put it in the theme.
Signs that a value belongs in brackets
Use an arbitrary value when:
- The number comes from one specific context, such as
top-[117px]to line a badge up with an image. - The value is derived from the layout, such as
w-[calc(100%-var(--sidebar-width))]. - The value appears once or twice and is unlikely to change with the design system.
- The CSS property has no utility, so an arbitrary property like
[mask-type:luminance]is the only option short of writing a stylesheet rule. - The condition is unusual, such as
[@supports(display:grid)]:grid.
<img
class="absolute top-[117px] left-[calc(50%-2rem)] [mask-type:luminance]"
src="/badge.svg"
alt=""
/>Nobody will ask for top-[117px] to be adjusted site-wide, so a theme entry would add a name with no benefit.
Signs that a value belongs in the theme
Extend the theme when:
- The same value appears three or more times. Repeated brackets are copies, and copies drift.
- The value has a meaning, such as a brand color, a card shadow or a header height.
- Designers or other developers will refer to it by name.
- You want it to change in one place, or to vary by theme (for example, dark mode).
Compare these two versions of the same button:
<button class="bg-[#2f6fed] hover:bg-[#2559c4] shadow-[0_2px_6px_rgb(0_0_0/0.2)]">Save</button>
<button class="bg-[#2f6fed] hover:bg-[#2559c4] shadow-[0_2px_6px_rgb(0_0_0/0.2)]">Send</button>
<button class="bg-[#2f6fed] hover:bg-[#2559c4] shadow-[0_2px_6px_rgb(0_0_0/0.2)]">Share</button>If the brand blue changes, you must edit every occurrence and hope you found them all. Moving the values into the theme fixes that.
Tailwind v4: `@theme` in CSS
@import "tailwindcss";
@theme {
--color-brand: #2f6fed;
--color-brand-dark: #2559c4;
--shadow-card: 0 2px 6px rgb(0 0 0 / 0.2);
}Each variable's namespace decides which utilities it creates. --color-* creates bg-brand, text-brand and so on. --shadow-* creates shadow-card.
Tailwind v3: `theme.extend` in the config
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {
colors: {
brand: { DEFAULT: "#2f6fed", dark: "#2559c4" },
},
boxShadow: {
card: "0 2px 6px rgb(0 0 0 / 0.2)",
},
},
},
};Use extend so you add to the default theme instead of replacing it. Either way, the buttons become:
<button class="bg-brand hover:bg-brand-dark shadow-card">Save</button>A decision flow
Side-by-side comparison
| Concern | Arbitrary value | Theme value |
|---|---|---|
| Change it everywhere | Edit each use | Edit one definition |
| Meaning in the markup | Raw number or color | A name such as `brand` |
| Editor autocomplete | Not suggested | Suggested |
| Setup cost | None | One definition |
| Fits a one-off layout fix | Yes | Adds clutter |
Other ways to remove repetition
Not every repeated style is a design token. Some repeated things are structure.
- A repeated cluster of classes, such as the same card layout in many places. Extract a component in your framework (a template partial, a React component) so the class list lives in one file.
- A repeated arbitrary property with no theme namespace, such as
[mask-type:luminance]. In v4, define a custom utility with@utility. In v3, use a plugin. - A repeated variable, such as
[--scroll-offset:56px]. Define it once on a parent or in:rootinstead of on every section.
Common mistakes
- Bracket values that should have been tokens. Search your project for a repeated bracket value, such as
#2f6fed. Two or more hits that mean the same thing are a theme candidate. - Theme entries for one-offs. A name like
top-badge-offsetused once makes readers jump to the config to learn a number. - Building the bracket class dynamically. Moving a value between brackets and the theme does not fix `
w-[${width}px]`. Tailwind still needs the full class name in your source. - Replacing the theme instead of extending it (v3). Putting
colorsdirectly underthemeremoves every default color.
Exercise
A project has bg-[#0b7285] on a navigation bar, a button and a footer link. A separate page has one mt-[13px] that nudges an icon next to a label. Which values should move to the theme, and which should stay as arbitrary values?
Show answer
Move #0b7285 to the theme, for example as --color-accent in v4 @theme, or under colors in theme.extend in v3, then use bg-accent. It is repeated and acts as a design decision, so one definition should control it. Keep mt-[13px] as an arbitrary value. It is a one-off alignment fix tied to a single icon and label, and a theme entry would only add a name for a number nobody will reuse.