Guide
Using @apply in Tailwind CSS to Create Reusable Utility Classes
Learn how to use Tailwind CSS's @apply to bundle utility classes into custom selectors, keeping HTML clean while preserving the design system.
Published by Tasadduq Burney
22 May 2026, 19:52 UTC
2 min95.5K views0

Why use @apply
When the same set of Tailwind utilities appears repeatedly in your HTML, you can move those utilities into a custom CSS selector with @apply. This keeps the markup cleaner while still relying on Tailwind’s design system.
How it works – a worked example
Add a rule to your stylesheet (e.g., src/styles.css or the file referenced in your Tailwind config):
/* src/styles.css */
.btn {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}
After rebuilding Tailwind, the generated CSS will contain a .btn rule with the properties from the listed utilities. You can then use the class in HTML:
<button class="btn">Save</button>
Limits and what to watch for
- Only generated utilities:
@applyworks with class names that Tailwind has actually produced (e.g.,bg-blue-500). Arbitrary values likebg-[#1a2b3c]or unsupported pseudo‑classes must be written with square‑bracket notation or added as plain CSS. - Build step required: If you forget to run the Tailwind build after adding or changing an
@applyrule, the browser will see no matching CSS and the styles will not appear. - Dynamic strings:
@applydoes not resolve class names that are constructed at runtime (e.g., from template literals). Those utilities must be present in your template files so Tailwind can include them. - Media queries: Placing
@applyinside an@screenblock does not work; the rule is ignored silently.
Practical verification
- Run your build command (e.g.,
npm run buildortailwindcss -i src/styles.css -o dist/output.css --watch). - Open the generated CSS file and confirm that the
.btnrule contains the expected properties (background-color:#3b82f6;etc.). - Inspect the rendered button in the browser’s developer tools; the computed styles should match the utilities you placed inside
@apply.
Common mistakes
- Adding the
@applyrule but not rebuilding, leading to missing styles. - Expecting
@applyto pull in utilities that are only referenced via JavaScript‑generated class strings; Tailwind’s purge will remove them. - Trying to use
@applywith values that Tailwind does not generate, resulting in no effect and no error message.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.