Repeating the same 15 utility classes everywhere is unsustainable. This lesson covers extracting components, using @apply for patterns, and building a reusable component library.
If you have 20 buttons that all need px-4 py-2 bg-blue-600 text-white rounded-lg font-medium hover:bg-blue-700 transition-colors, you have a problem. Three solutions, in order of preference:
/* Only do this for genuinely reused patterns */
@layer components {
.btn {
@apply px-4 py-2 rounded-lg font-medium transition-colors focus:outline-none focus:ring-2;
}
.btn-primary {
@apply bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500;
}
.btn-secondary {
@apply bg-gray-100 text-gray-900 hover:bg-gray-200 focus:ring-gray-400;
}
.card {
@apply bg-white rounded-xl shadow-sm border border-gray-100 p-6;
}
}
Success!
Your changes have been saved.
group utility enables parent-child hover states. Add group to the parent, then group-hover:text-blue-600 to the child. Hover the parent, the child changes. Perfect for cards with hover effects.group utility on the card so hovering the card highlights the title.@apply works for genuinely reused patterns — use sparingly, max once per pattern.group on parent + group-hover:* on child = parent-triggered child hover effects.@layer base (elements), @layer components (classes), @layer utilities (one-off utilities).