The "@apply" Gotcha: Overuse and Its Downsides

While @apply can be useful for abstracting common utility combinations, it's important not to overuse it. This example illustrates the potential pitfalls.

Scenario: A Styled Card Component

Consider this card, styled directly with Tailwind utilities:

Placeholder Logo
Complex Card

Styled with many utility classes!

Utilities used: p-6 max-w-md mx-auto bg-white rounded-xl shadow-lg flex items-center space-x-4 border border-gray-200 ...

The Temptation and The "Gotcha"

Seeing all those utility classes, one might be tempted to create a single custom class like .custom-card using @apply .

/* In CSS (conceptual): */
.custom-card {
  @apply p-6 max-w-md mx-auto bg-white rounded-xl shadow-lg flex items-center space-x-4 border border-gray-200;
  /* ... potentially more classes ... */
}

This is where the "Gotcha" comes in:

Guidance: Use @apply sparingly, for very small, highly-reused utility patterns (e.g., a specific text style focus ring, or a very simple button base), rather than for entire component abstractions.