Using Tailwind’s @apply to Build Reusable Component Classes in JIT Mode
Learn how to compose Tailwind utilities into single CSS classes with @apply, keep your HTML tidy, and ensure the JIT compiler processes them correctly.
18 Aug 2025, 04:11 UTC

Desired Outcome
Compose multiple Tailwind utilities into a single reusable CSS class so that your HTML stays concise and component styles are easy to maintain.
Prerequisites
- Node.js 16+ – required for Tailwind CLI and PostCSS.
- Tailwind CSS 3.x – the latest major release supports the JIT engine and the
@applydirective. - JIT mode enabled – set
mode: 'jit'intailwind.config.jsor use the default JIT in Tailwind 3. - PostCSS pipeline – the Tailwind plugin must run **before** any other processor (e.g., autoprefixer). A typical
postcss.config.jslooks like:module.exports = { plugins: [ require('tailwindcss'), require('autoprefixer'), ], }; - CSS entry file – create a file (e.g.,
src/styles.css) that will be processed by Tailwind.
Focused Procedure
- Create the component class
Open
src/styles.cssand add a rule that uses@apply:
This line tells Tailwind to replace.btn { @apply bg-blue-500 text-white px-4 py-2 rounded; }.btnwith the compiled CSS for those utilities. - Add variant rules (optional)
To support hover or focus states, prefix the selector with the variant before
@apply:
Tailwind will generate the correct CSS for the.btn:hover { @apply bg-blue-700; }hovervariant. - Group under
@layer(recommended for libraries)Wrap component definitions inside
@layer componentsto keep the output organized:
This makes the generated CSS easier to read and maintain.@layer components { .btn { @apply bg-blue-500 text-white px-4 py-2 rounded; } .btn:hover { @apply bg-blue-700; } } - Import the CSS in your project
In your main JavaScript/TypeScript file (e.g.,
src/main.js), import the stylesheet:
The build tool will run the PostCSS pipeline automatically.import './styles.css'; - Run the Tailwind build
Execute the CLI in watch mode to see live updates:
Replace the paths with your project structure.npx tailwindcss -i src/styles.css -o dist/output.css --watch
Expected Checks
- Compiled CSS – Open
dist/output.cssand verify that.btncontains the expected declarations (e.g.,background-color: #3b82f6;,color: #ffffff;, etc.). - Hover behavior – Open a page that uses
<button class="btn">and inspect the element in devtools. Hovering should apply the darker blue background. - Purging unused utilities – If you remove
.btnfrom the source and rebuild, the corresponding utilities should disappear from the output CSS.
Recovery Options
- Build errors about @apply – Ensure that the Tailwind plugin is the first in the PostCSS pipeline. If you see errors like "Unknown directive @apply", check that the file is being processed by Tailwind.
- Missing utilities – Verify that the utilities referenced in
@applyexist intailwind.config.js. Custom colors or spacing values must be defined in thetheme.extendsection. - Variants not applying – If a hover rule is ignored, confirm that the variant is enabled in the config (e.g.,
variants: { backgroundColor: ['hover'] }) and that the selector syntax is correct. - Unexpected CSS size – Overusing
@applyfor tiny, unique styles can bloat the output. Keep an eye on the purge settings and consider using@layer utilitiesfor truly global styles.
Practical Tip
When building a component library, place all component rules inside @layer components. This keeps the generated CSS tidy and makes it clear which classes are meant for public use versus internal styling. It also prevents accidental duplication of utilities that could otherwise be purged.
Conclusion
Using @apply in a JIT‑enabled Tailwind project lets you write clean, maintainable component classes without sacrificing the benefits of utility‑first styling. Follow the steps above, keep an eye on the build output, and you’ll have a robust, reusable style foundation for your application.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.