Sub topic 11
Defining Custom Utilities
This chapter assumes you know Tailwind utility classes, the @apply directive, and the @layer components pattern from earlier chapters. It also assumes you know what a variant such as hover: or md: does.
Why a custom utility
A component class like .btn-primary bundles many decisions into one name. Sometimes you want something smaller: a single reusable building block that behaves like a built-in utility. You can put it in markup next to other utilities, and you can prefix it with variants.
Tailwind CSS v4 has a directive for this called @utility. It registers a class name as a real utility. Tailwind then knows how to sort it, how to wrap it in variants, and when to leave it out of the output.
Registering a utility in v4
Write @utility in your main stylesheet, after the import of Tailwind.
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}You can now use btn-base in markup like any other utility.
<button class="btn-base bg-blue-600 text-white hover:bg-blue-700 md:px-6">
Save
</button>Here btn-base holds the shared shape. The colors stay in the markup, so each button can still choose its own. Because btn-base is a registered utility, md:btn-base and hover:btn-base also work.
Utilities versus component classes
A plain .btn-primary rule inside @layer components is ordinary CSS. Tailwind does not treat it as a utility, so variants do not apply to it automatically. The table shows the difference.
| Feature | `@layer components` class | `@utility` |
|---|---|---|
| Works with `hover:`, `md:`, and other variants | No, unless you write each rule yourself | Yes |
| Removed from output when unused | Depends on version and setup | Yes, only used utilities are emitted |
| Typical content | A whole component, such as a button | A small reusable piece |
| Sorting relative to other utilities | Placed in the components layer | Sorted with utilities |
The last row matters for overrides. Utilities come after components in the cascade. A utility class in your markup can therefore override a component class, but a component class cannot override a utility.
Custom utilities are sorted among the utilities, so a custom utility and a built-in one are treated alike.
Tailwind v3 equivalent
In Tailwind v3 there is no @utility. You put the rule inside @layer utilities instead.
@layer utilities {
.btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}
}Tailwind v3 also lets variants work on classes inside @layer utilities. Check which version your project uses before you copy a snippet, because @utility is not valid in v3.
Common errors
- Using
@utilityin v3. The build fails or ignores the rule. Use@layer utilitiesthere. - Writing a name with a dot. In v4 you write
@utility btn-base, not@utility .btn-base. The name has no leading dot. - Putting the whole component in one utility. If the rule bundles colors, shadows, and spacing, it stops being reusable. Keep utilities small and keep component decisions in a component.
- Expecting a utility to beat a later override. If two utilities set the same property, source order and specificity decide the winner. Resolve this with
tailwind-mergein a component, as shown earlier in this tutorial.
Checking your result
After you add a utility, confirm it works with a variant. Add hover: or md: in front of its name in markup and check that the rule changes in the browser. If nothing happens, the usual causes are a v3 project using @utility, or a stylesheet that was not imported into the build.
Exercise
In a Tailwind v4 project you define .card-pad inside @layer components and then write md:card-pad in your markup. The padding never changes at the md breakpoint. What is the cause, and how do you fix it?
Show answer
A class inside @layer components is plain CSS, so Tailwind does not generate variant versions of it. Register it as a utility instead, with @utility card-pad { ... } (no leading dot). Then md:card-pad works.
Exercise
You need a rule that sets rounded-md px-4 py-2 font-semibold and want it to be usable with hover: in a Tailwind v3 project. Which layer do you place it in?
Show answer
Place it in @layer utilities. Tailwind v3 has no @utility directive, and rules in that layer can be used with variants.