Sub topic 11
Defining custom utilities
This chapter assumes you know how @apply and @layer components work, and that you use Tailwind CSS v4 (v3 differences are noted). It covers how to register your own utility classes so they behave like built-in ones. That means they work with hover:, md:, dark: and other variants, they are generated only when used, and other CSS can pull them in with @apply.
Why a component class is not enough
A class under @layer components is plain CSS. In the previous chapters you wrote .btn-primary that way. It works, but it has limits:
- You cannot write
md:btn-primaryorhover:btn-primaryand expect Tailwind to wrap the rule in a media query or a:hoverselector. - In v4, the rule is emitted whether or not any markup uses it.
- Each state, such as hover, needs its own hand-written selector, as in
.btn-primary:hover.
A custom utility fixes these. Tailwind treats it like px-4 or flex: it scans your source files for the class name and generates the CSS only when it finds it. It also lets any variant wrap the class.
Registering a static utility
A static utility is a fixed name that maps to fixed declarations. Use @utility followed by the name and a block.
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}The body can contain ordinary declarations, @apply lines, or both. The class btn-base now works anywhere a built-in utility does:
<button class="btn-base bg-blue-600 text-white hover:bg-blue-700 md:px-6">
Save
</button>Three things happen here:
- Tailwind finds
btn-basein the markup and emits its CSS. md:px-6is a normal built-in utility. It overrides thepx-4thatbtn-basecontains for viewports at the medium breakpoint and up.- You could also write
md:btn-baseorhover:btn-base, and Tailwind would wrap the whole rule in the matching variant.
The last point is what separates @utility from a component class. The design question is what you put in the utility. A small utility such as btn-base, which holds only the shape and typography, leaves color and state to the markup. A utility that bundles everything is hard to override.
Custom CSS properties and nested selectors
A utility body can hold raw CSS that Tailwind has no built-in class for. It can also use & for nested selectors, exactly as in native CSS nesting.
@utility scrollbar-hidden {
scrollbar-width: none;
&::-webkit-scrollbar {
display: none;
}
}Variants apply to the whole block, so md:scrollbar-hidden hides scrollbars only at the medium breakpoint and up. The pseudo-element rule is wrapped as well.
Functional utilities with a wildcard
Static utilities map one name to one result. Built-in classes such as px-4 or bg-blue-600 accept a value after a dash. You can build the same shape by ending the name in -* and reading the value with --value().
@import "tailwindcss";
@theme {
--tab-size-github: 8;
}
@utility tab-* {
tab-size: --value(--tab-size-*);
}The --value() function resolves whatever follows tab- in the class name. The argument tells it where to look:
| Argument form | What it accepts | Example class | Result |
|---|---|---|---|
| `--value(--tab-size-*)` | A key from a theme namespace | `tab-github` | `tab-size: 8` |
| `--value(integer)` | A bare integer | `tab-4` | `tab-size: 4` |
| `--value([integer])` | An arbitrary value in brackets | `tab-[6]` | `tab-size: 6` |
| `--value(--spacing-*)` | A spacing theme key | `inset-gutter` style names | Looked up in the theme |
You can pass several arguments, and Tailwind tries them in order until one matches:
@utility tab-* {
tab-size: --value(--tab-size-*, integer, [integer]);
}With this version, tab-github, tab-4, and tab-[6] all work. If none of the forms matches, Tailwind generates nothing for that class. It does not guess.
Using the utility from other CSS
Custom utilities can be used inside @apply, in the same way as built-in ones. That makes them useful as building blocks:
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}
@layer components {
.btn-primary {
@apply btn-base bg-blue-600 text-white hover:bg-blue-700;
}
}Here .btn-primary is a component class assembled from one custom utility and several built-ins. This two-level pattern keeps shared shape in a utility and leaves decisions about color to the component class. It also gives you the option of using either btn-base directly in markup or .btn-primary where a ready-made name is clearer.
How Tailwind processes a custom utility
The sequence below shows what happens from the moment you write @utility to the moment a class reaches the browser.
The scan step is the same one used for built-in classes. It reads source files as plain text, so the full class name has to appear literally. A class assembled from string pieces, such as "btn-" + size, is not detected. The same rule you learned for variant props applies here.
Worked example: a focus ring utility
Many projects repeat the same accessible focus style on buttons, links, and inputs. A custom utility removes that repetition and still supports variants.
@import "tailwindcss";
@utility focus-ring {
@apply outline-2 outline-offset-2 outline-blue-600;
}<a href="/docs" class="rounded px-2 focus-visible:focus-ring">Docs</a>
<input class="rounded border px-3 py-2 focus:focus-ring" />The focus-visible: prefix wraps the utility in :focus-visible, so the outline shows only on keyboard focus. If the same style lived in a .focus-ring:focus-visible component rule, you would have to write each state's selector by hand and could not switch states from the markup.
Common errors
- Using a utility name that Tailwind does not detect. The name must appear whole in a scanned file. Dynamic names like `
btn-${size}` produce no CSS. - Defining the utility inside
@layer utilitiesin v4 and expecting variants. Use@utility. - Naming a utility after a built-in class. A custom
@utility flexmerges with or competes against the built-in rules, and it is hard to reason about. Pick a distinct name or a project prefix. - Forgetting that a wildcard utility needs a matching value.
tab-*with--value(integer)will not matchtab-wide. Check which argument forms you listed. - Using
@utilityin a separate stylesheet without a reference. A Vue or Svelte style block cannot see your utilities until you add@reference "../../app.css";at its top. The chapter on scoped styles covers this. - Bundling too much. A utility that sets color, spacing, and typography is a component in disguise. Overriding one part means fighting the rest. Keep it to one concern.
When to choose a custom utility
| Situation | Better choice |
|---|---|
| Repeated markup with structure (icon, label, wrapper) | Framework component |
| One concern repeated across many elements, such as a focus ring | `@utility` |
| A class that must work with `hover:`, `md:`, or `dark:` | `@utility` |
| A value-driven class like `tab-4` | Functional `@utility` with `--value()` |
| A fixed named element style with no variant needs | `@layer components` with `@apply` |
A rule of thumb: if you would want to prefix the class with a variant, make it a utility. If it names a whole thing, such as a card or a button, make it a component, either as framework code or as a component class.
Exercise
You wrote .focus-ring { @apply outline-2 outline-blue-600; } inside @layer components in a Tailwind v4 project. The markup uses focus-visible:focus-ring, but the outline never appears. What is wrong, and how do you fix it?
Show answer
A rule in @layer components is plain CSS and does not get variant support in v4. Tailwind does not know how to wrap it in :focus-visible, so focus-visible:focus-ring generates nothing. Register it as a custom utility instead: @utility focus-ring { @apply outline-2 outline-blue-600; }. Now the focus-visible: prefix works, and the CSS is emitted only when the class is used.
Exercise
Write a functional utility named columns-gap-* that sets column-gap from a spacing value in the theme (the --spacing-* namespace), and also accepts any integer, treated as a theme multiplier is not needed, so accept it as a raw bracket value like columns-gap-[2rem]. Which --value() arguments do you list?
Show answer
Use @utility columns-gap-* { column-gap: --value(--spacing-*, [length]); }. The first argument resolves theme keys from the spacing namespace. The second argument accepts an arbitrary length in brackets, so columns-gap-[2rem] works. Classes that match neither form generate no CSS.