📖 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 file
Use @apply inside .btn to add Tailwind utility classes for padding, background color, text color, rounded corners, and hover effect
Add three buttons in the HTML that use the btn class
Ensure 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
1
Create the HTML structure with three buttons
Write the basic HTML skeleton with a <main> section containing three <button> elements. Each button should have the text Button 1, Button 2, and Button 3 respectively. Do not add any classes yet.
Tailwind
Hint
Use three <button> tags inside the <main> element with the exact texts.
2
Create a CSS class .btn with Tailwind utility classes using @apply
In the styles.css file, create a CSS class called .btn. Inside it, use the @apply directive to add these Tailwind classes exactly: px-4, py-2, bg-blue-600, text-white, rounded, and hover:bg-blue-700.
Tailwind
Hint
Write a CSS class .btn and inside it use @apply with the exact Tailwind classes separated by spaces.
3
Add the btn class to all three buttons in the HTML
Modify the three <button> elements in the HTML to include the class attribute with the value btn. For example, <button class="btn">Button 1</button>.
Tailwind
Hint
Add class="btn" inside each <button> tag.
4
Add accessibility and responsive spacing to the buttons
Add an aria-label attribute to each button with the same text as the button's visible text (e.g., aria-label="Button 1"). Also, add a Tailwind class space-x-4 to the <main> element to add horizontal spacing between the buttons.
Tailwind
Hint
Add class="space-x-4" to the <main> tag and add aria-label attributes to each button with matching text.
Practice
(1/5)
1. What is the main purpose of using @apply in Tailwind CSS?
easy
A. To create animations in Tailwind CSS
B. To add JavaScript functionality to Tailwind classes
C. To import external CSS files into Tailwind
D. To combine multiple utility classes into one reusable custom class
Solution
Step 1: Understand what @apply does
@apply lets you write multiple Tailwind utility classes inside a CSS rule to create a single custom class.
Step 2: Identify the main use
This helps reuse styles and keep code tidy by combining utilities into one class.
Final Answer:
To combine multiple utility classes into one reusable custom class -> Option D
Quick Check:
@apply combines utilities = C [OK]
Hint: Remember: @apply bundles utilities into one class [OK]
Common Mistakes:
Thinking @apply adds JavaScript
Confusing @apply with animation creation
Assuming @apply imports CSS files
2. Which of the following is the correct syntax to use @apply inside a CSS file?
easy
A. .btn { @apply text-white bg-blue-500 px-4 py-2; }
B. .btn { apply: text-white bg-blue-500 px-4 py-2; }
C. .btn { @use text-white bg-blue-500 px-4 py-2; }
D. .btn { apply(@text-white, @bg-blue-500); }
Solution
Step 1: Recall the correct @apply syntax
The correct way is to write @apply followed by utility classes inside a CSS rule block.
Step 2: Check each option
Only .btn { @apply text-white bg-blue-500 px-4 py-2; } uses @apply correctly with utility classes separated by spaces.
A. The syntax is invalid because @apply must be last
B. Multiple @apply statements should be combined into one
C. The color property conflicts with @apply and causes error
D. You cannot use @apply with padding classes
Solution
Step 1: Check usage of multiple @apply
Tailwind allows multiple @apply but best practice is to combine them for clarity and to avoid duplication.
Step 2: Analyze other options
@apply works with padding, color property is valid CSS and does not cause error, and @apply can be anywhere inside the rule.
Final Answer:
Multiple @apply statements should be combined into one -> Option B
Quick Check:
Combine @apply classes for cleaner CSS = D [OK]
Hint: Combine all utilities in one @apply for neat CSS [OK]
Common Mistakes:
Thinking @apply can't be used multiple times
Assuming color property conflicts with @apply
Believing @apply must be last in CSS block
5. You want to create a reusable button style in Tailwind using @apply that has white text, blue background, padding, rounded corners, and a hover effect that darkens the background. Which CSS snippet correctly achieves this?