Use Components (Recommended)
The best way to reuse styles in Tailwind is to create components using your framework of choice. This keeps your utility classes alongside your markup in a single place.- React
- Vue
- HTML Partials
Benefits of Component-Based Reuse
- Single source of truth - Change the component once, update everywhere
- Co-located markup and styles - Easy to understand and maintain
- Full power of JavaScript - Conditional logic, props, state
- Type safety - With TypeScript, catch errors at compile time
Using @apply
When working with traditional CSS files or when components aren’t an option, use@apply to extract repeated utility patterns:
How @apply Works
From the Tailwind source code,@apply processes utility classes and inlines their CSS:
@apply with Variants
You can use variants in@apply statements:
Important Modifier
Add! to make declarations !important:
When to Use @apply
Good use cases:
- Base styles that are truly reused everywhere
- Third-party component overrides
- Extracting very common patterns in large codebases
- Button variants
- Form fields
- Cards and other UI patterns
- Page layouts
Creating Custom Utilities
For project-specific utilities that aren’t in Tailwind, use@utility:
Multi-Class Variants
For groups of utilities that belong together, use component classes:CSS Variables for Theming
Use CSS custom properties for values that change:Loops and Maps
For generating utility variations, use your CSS preprocessor:Editor Snippets
Create snippets for frequently-used utility combinations:Avoiding Premature Abstraction
Copy-Paste First
When you need the same pattern twice, copy-paste is fine:Extract When Needed
After using the pattern 3-5 times and it’s proven stable, create a component:Best Practices
1. Prefer Components Over @apply
2. Keep Utilities Semantic
3. Don’t @apply Everything
Remember: The goal isn’t to eliminate all utility classes from your HTML. Use abstractions where they add value, but don’t be afraid of utilities in your markup.