Skip to main content
When building with Tailwind’s utility-first approach, you’ll naturally encounter situations where you need to reuse the same set of utilities across multiple elements. Here are the recommended approaches for managing duplication and reusing styles. 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.

Benefits of Component-Based Reuse

  1. Single source of truth - Change the component once, update everywhere
  2. Co-located markup and styles - Easy to understand and maintain
  3. Full power of JavaScript - Conditional logic, props, state
  4. 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
Better solved with components:
  • 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:
Use them like any other Tailwind utility:
From the source, custom utilities support all Tailwind features:

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:
Then override in specific contexts:

Loops and Maps

For generating utility variations, use your CSS preprocessor:

Editor Snippets

Create snippets for frequently-used utility combinations:

Avoiding Premature Abstraction

Don’t create abstractions too early. It’s better to have some duplication initially and extract patterns as they naturally emerge.

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.