Sub topic 1
Where utilities end: the theme and its limits
Tailwind CSS gives you utility classes such as p-4, text-lg and bg-blue-500. Each one is a small CSS rule. Most of those rules read their values from a central design system called the theme. This chapter explains how that mapping works and where it stops. Knowing the edge of the theme tells you when arbitrary values, arbitrary properties and arbitrary variants are the right tool. The rest of this tutorial covers them in detail.
Prerequisites
This chapter assumes you already know:
- Basic HTML and CSS, including units (
px,rem,%), custom properties (CSS variables such as--gap: 8px) and selectors. - Tailwind utility classes such as
p-4,text-lgandbg-blue-500. - Variant prefixes such as
hover:andmd:.
You also need a project where Tailwind is already installed and building CSS. Tailwind has two configuration styles. Version 4 defines the theme in CSS with @theme. Version 3 defines it in tailwind.config.js. This chapter shows both. The square-bracket syntax in later chapters works the same in either version.
What a utility class really is
A utility class is a name that Tailwind maps to one or a few CSS declarations. The name usually has two parts: a prefix that picks the CSS property, and a suffix that picks the value.
| Class | Prefix (property) | Suffix (value source) | CSS produced (roughly) |
|---|---|---|---|
| `p-4` | `p` (padding) | `4` from the spacing scale | `padding: 1rem` |
| `text-lg` | `text` (font size) | `lg` from the font size scale | `font-size: 1.125rem` plus a line height |
| `bg-blue-500` | `bg` (background color) | `blue-500` from the color palette | `background-color` set to that blue |
| `md:flex` | `md:` (variant) | `flex` is a fixed utility | `display: flex` inside a min-width media query |
The suffix is a key into a lookup table. That table is the theme.
The theme is a lookup table
The theme is the set of named design values your project uses: colors, spacing steps, font sizes, breakpoints, shadows, radii and so on. When Tailwind reads bg-blue-500, it asks the theme for a color named blue-500. If the theme has it, Tailwind writes the rule. If not, it writes nothing.
That last point matters. A class name that does not match the theme is not an error. Tailwind ignores it, and the element simply has no style from that class.
<!-- "117px" is not a key in the spacing scale, so no rule is generated -->
<div class="absolute top-117px"></div>Nothing breaks in the build, and nothing shows up in the browser. This silent failure is the first limit of the theme, and you will meet it again in the chapter on dynamic class names.
Why the theme is a good default
The lookup table is the main reason Tailwind projects stay consistent. A limited set of values gives you these benefits:
- Consistency. Every card uses the same padding steps, so layouts line up.
- Shared vocabulary. A teammate reading
rounded-lgknows it matches the otherrounded-lgelements. - Smaller CSS. Tailwind only generates rules for classes it finds, and a small vocabulary means fewer distinct rules.
- Central change. Edit one theme value and every use of it changes.
For these reasons, a theme value is the right choice whenever one fits.
Extending the theme
You add values to the theme when your design system needs a new named value. In Tailwind v4, you do it in your CSS file with the @theme directive. Variables in certain namespaces create utilities automatically:
@import "tailwindcss";
@theme {
--color-brand: #bada55;
--breakpoint-3xl: 120rem;
}After this, bg-brand, text-brand and border-brand exist, and so does the 3xl: variant. In Tailwind v3, you extend the theme in the configuration file:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {
colors: {
brand: "#bada55",
},
screens: {
"3xl": "120rem",
},
},
},
};Note the extend key. Putting colors directly under theme in v3 replaces the whole default palette. Putting it under theme.extend adds to the palette. Forgetting extend is a common mistake that makes every default color vanish.
Extending the theme is the right move for values that your design reuses. It is not the right move for a value you need exactly once.
Where the theme runs out
A design rarely matches a fixed scale. These are the situations where the theme cannot help you, grouped into three kinds.
1. A one-off value
A designer places a badge 117 pixels from the top of a hero image. Another design needs a column exactly 317 pixels wide. A brand color appears on a single promotional banner and nowhere else.
You could add top-117 or color-promo to the theme. That has a cost: the theme gets longer, every entry needs a name, and the name hides the real value. If the value is used in one place, a theme entry is overhead.
Arbitrary values solve this. You write the value in square brackets inside the class name:
<div class="absolute top-[117px] w-[317px] bg-[#bada55]"></div>The prefix still picks the property, but the bracketed suffix is used as written instead of being looked up. The next chapter covers this syntax in detail.
2. A CSS property with no utility
Tailwind ships utilities for the properties most projects need. CSS has hundreds of properties, and some have no utility class. mask-type is one example, and many newer properties are others. Utility coverage also lags behind new CSS features.
Arbitrary properties let you write a declaration directly in brackets:
<div class="[mask-type:luminance]"></div>You can also use them to set CSS variables, which gives you a way to pass values into other arbitrary values.
3. A condition with no variant
Tailwind provides variants for common conditions: hover:, focus:, md:, dark:, first:, odd: and more. Some conditions are not covered. You might want to style every direct child of a list, or apply a utility only when the browser supports display: grid.
Arbitrary variants let you write the selector or at-rule yourself:
<ul class="[&>li:nth-child(odd)]:bg-gray-100 [@supports(display:grid)]:grid">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>Here & stands for the element the class is on.
A map of the escape hatches
The three kinds of gap map to three forms of square-bracket syntax. They share one idea: put real CSS in brackets and let Tailwind generate the rule for you.
| Gap in the theme | Tool | Example | Chapter focus |
|---|---|---|---|
| No named value for a size, color or position | Arbitrary value | `top-[117px]` | Arbitrary values with square brackets |
| No utility for a CSS property | Arbitrary property | `[mask-type:luminance]` | Arbitrary properties |
| No variant for a selector or at-rule | Arbitrary variant | `[&>li]:p-2` | Arbitrary variants |
These tools sit on top of the theme. They do not replace it, and you can mix them in one class list with ordinary utilities such as p-4 and hover:underline.
How Tailwind finds your classes
Tailwind builds only the CSS you use. To know what you use, it scans your source files as plain text and looks for strings that could be class names. It does not run your code, and it does not understand JavaScript, templates or components. It only sees text.
This has a direct consequence for everything in this tutorial. A class name must appear in full, as one unbroken string, somewhere in a scanned file. If your code builds the name from pieces at runtime, Tailwind never sees the final name and never generates the rule.
// 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 same rule applies to theme classes, not only arbitrary ones. The string ` bg-${color}-500 ` fails for the same reason. The theme limits what values exist, and the scanner limits how you may write them. A later chapter in this tutorial treats this failure in depth. For now, remember that bracketed values still need to be written out in full.
Worked example: a hero section that outgrows the theme
Suppose a designer hands you a hero banner. Walk through each requirement and decide where it belongs.
- Padding of 2rem on small screens, 4rem on large. These are on the spacing scale, so use
p-8 lg:p-16. - Brand color
#bada55for the headline, used across the whole site. This repeats, so add--color-brandto the theme and writetext-brand. - A decorative badge at exactly 117px from the top. One use, one place. Write
top-[117px]. - A mask that needs
mask-type: luminance. No utility exists. Write[mask-type:luminance]. - Style every second child inside the feature list. No built-in variant matches this exact selector. Write
[&>li:nth-child(even)]:bg-gray-50.
The finished markup might look like this:
<section class="relative p-8 lg:p-16">
<h1 class="text-brand text-lg">Welcome</h1>
<span class="absolute top-[117px] right-4">New</span>
<div class="[mask-type:luminance]"></div>
<ul class="[&>li:nth-child(even)]:bg-gray-50">
<li>Fast</li>
<li>Simple</li>
<li>Flexible</li>
</ul>
</section>Only the brand color went into the theme. The other custom needs stayed local, so the theme stays small and every one-off sits next to the element it affects.
Common mistakes at the edge of the theme
- Inventing a class that looks like it should exist. Writing
top-117pxorw-317in v3 produces nothing. If the build output has no rule for a class, check whether the suffix is a real theme key. In Tailwind v4, spacing utilities derive from a base spacing value, so more numeric suffixes work than in v3. Do not rely on that for values that are not multiples of the base step. - Replacing the theme instead of extending it. In v3, defining
theme.colorsinstead oftheme.extend.colorsremoves the default palette. - Reaching for brackets too often. The same bracketed value repeated in twenty places is a theme value that has not been named yet. Brackets are for exceptions.
- Assuming brackets are exempt from scanning. They are not.
w-[${width}px]is never generated, as shown above. - Expecting an error. Tailwind does not warn about unknown class names. You find mistakes by checking the browser or the generated CSS.
A rule of thumb for choosing
Ask these questions in order:
- Does a theme value already express this? Use it.
- Will the value appear in several places, or does it carry meaning (a brand color, a standard header height)? Add it to the theme.
- Is it a one-off, a measurement taken from a single design, or a property or selector with no utility? Use an arbitrary value, property or variant.
The last chapter of this tutorial returns to this decision with more criteria. The central point is that the theme and the bracket syntax work together. The theme holds the vocabulary your project shares, and the brackets handle everything that does not belong in it.
Exercise
A teammate writes <div class="mt-93px"> and the margin does not change. The build finishes without errors. Explain why nothing happened, and give two different ways to fix it, saying when you would pick each.
Show answer
Tailwind looks up the suffix 93px in the spacing scale. It is not a key, so Tailwind generates no rule and does not report an error. Fix 1: use an arbitrary value, mt-[93px], when the value is a one-off. Fix 2: add a named value to the theme (for example a spacing entry such as --spacing-hero-gap: 93px in Tailwind v4's @theme, then write mt-hero-gap) when the value repeats or has a meaning worth naming. In Tailwind v4 you could also use a spacing step if 93px happened to be a multiple of the base spacing, but 93 is not a multiple of 4px.
Exercise
In Tailwind v3, you want to add a brand color. Which is correct, theme: { colors: { brand: "#bada55" } } or theme: { extend: { colors: { brand: "#bada55" } } }, and what goes wrong with the other one?
Show answer
The extend version is correct. Defining colors directly under theme replaces the entire default palette, so classes like bg-blue-500 and text-gray-700 stop being generated. extend adds brand alongside the defaults.
Exercise
Sort each need into theme extension or an arbitrary tool: (a) a brand color used on every page, (b) an element positioned 117px from the top in one banner, (c) a mask-type declaration, (d) styling only odd list items inside one component.
Show answer
(a) Theme extension, because it repeats and carries meaning. (b) Arbitrary value, top-[117px]. (c) Arbitrary property, [mask-type:luminance]. (d) Arbitrary variant, [&>li:nth-child(odd)]:bg-gray-100, or a built-in like odd: on each item if you can edit the items.