The key is to use
@apply
judiciously. It's best suited for small, utility-like abstractions, not for replacing the benefits of JavaScript components or the clarity of utility classes in HTML for larger structures.
This approach keeps all styling information directly in the HTML, making it easy to see and modify.
Styled Heading
Some descriptive text here.
Another Styled Heading
Slightly different descriptive text.
Clarity:
All styling (
text-blue-700
,
bg-green-500
, etc.) is explicit in the HTML. This is the core strength of Tailwind.
Imagine you have a *very* common, small set of text styles you apply repeatedly, like a "caption" style.
Caption Text Example 1
Caption Text Example 2
@apply
for "Caption Text":
The repeated utilities are:
text-xs font-medium text-gray-500 tracking-wide uppercase
.
/* In CSS (conceptual): */
.caption-text {
@apply text-xs font-medium text-gray-500 tracking-wide uppercase;
}
HTML would then be:
<p class="caption-text">Caption Text</p>
@apply
leads away from Tailwind's utility-first benefits. Prefer direct utility application or true component-based architecture.