Extracting Patterns with Tailwind CSS @apply
📖 Scenario: You are building a simple webpage with multiple buttons that share the same style. Instead of repeating the same Tailwind CSS classes on each button, you want to extract the common styles into a reusable CSS class using the @apply directive.
🎯 Goal: Create a CSS class called .btn that uses @apply to include common Tailwind utility classes. Then use this class on multiple buttons in your HTML to keep your code clean and consistent.
📋 What You'll Learn
Create a CSS class named
.btn in a styles.css fileUse
@apply inside .btn to add Tailwind utility classes for padding, background color, text color, rounded corners, and hover effectAdd three buttons in the HTML that use the
btn classEnsure the buttons have consistent styling and a hover effect
💡 Why This Matters
🌍 Real World
Extracting common styles into reusable classes helps keep your CSS clean and maintainable, especially in projects with many similar elements.
💼 Career
Knowing how to use Tailwind's @apply directive is valuable for frontend developers to write efficient, scalable styles and collaborate on large codebases.
Progress0 / 4 steps