What if you could change dozens of button styles by editing just one line of code?
Why @apply for extracting patterns in Tailwind? - Purpose & Use Cases
Imagine you are styling many buttons on your website. You write the same group of Tailwind classes over and over, like bg-blue-500 text-white px-4 py-2 rounded for each button.
Typing the same classes repeatedly is tiring and easy to mess up. If you want to change the style later, you must update every button manually, which takes a lot of time and can cause mistakes.
The @apply directive lets you group common Tailwind classes into one custom class. Then you use that class everywhere, so changing styles means editing just one place.
<button class="bg-blue-500 text-white px-4 py-2 rounded">Click me</button> <button class="bg-blue-500 text-white px-4 py-2 rounded">Submit</button>
.btn-primary {
@apply bg-blue-500 text-white px-4 py-2 rounded;
}
<button class="btn-primary">Click me</button>
<button class="btn-primary">Submit</button>You can create reusable style patterns that keep your code clean and make design updates fast and error-free.
When building a website with many buttons, cards, or alerts sharing the same style, @apply helps you keep your CSS organized and consistent.
Writing repeated Tailwind classes is slow and error-prone.
@apply groups common classes into one reusable style.
Updating styles becomes easy and consistent across your site.
Practice
@apply in Tailwind CSS?Solution
Step 1: Understand what
@applydoes@applylets 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 DQuick Check:
@apply combines utilities = C [OK]
- Thinking @apply adds JavaScript
- Confusing @apply with animation creation
- Assuming @apply imports CSS files
@apply inside a CSS file?Solution
Step 1: Recall the correct
The correct way is to write@applysyntax@applyfollowed 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@applycorrectly with utility classes separated by spaces.Final Answer:
.btn { @apply text-white bg-blue-500 px-4 py-2; } -> Option AQuick Check:
Correct @apply syntax = A [OK]
- Using 'apply:' instead of '@apply'
- Using '@use' or other incorrect keywords
- Trying to call @apply like a function
@apply:
.card {
@apply p-6 bg-white rounded-lg shadow-md;
}
What visual styles will the .card class have?Solution
Step 1: Understand each utility class
p-6means padding 1.5rem,bg-whitesets white background,rounded-lgadds large rounded corners,shadow-mdadds medium shadow.Step 2: Match these styles to options
Padding of 1.5rem, white background, rounded corners, and medium shadow correctly describes these styles. Others have wrong padding sizes or colors.Final Answer:
Padding of 1.5rem, white background, rounded corners, and medium shadow -> Option CQuick Check:
p-6 + bg-white + rounded-lg + shadow-md = A [OK]
- Confusing padding units (px vs rem)
- Mixing up background colors
- Misunderstanding shadow sizes
@apply?
.btn {
@apply text-white bg-blue-500 px-4 py-2;
@apply rounded-lg;
color: red;
}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 BQuick Check:
Combine @apply classes for cleaner CSS = D [OK]
- Thinking @apply can't be used multiple times
- Assuming color property conflicts with @apply
- Believing @apply must be last in CSS block
@apply that has white text, blue background, padding, rounded corners, and a hover effect that darkens the background. Which CSS snippet correctly achieves this?Solution
Step 1: Understand hover styling with @apply
To apply hover styles with @apply, you must nest inside a CSS hover selector like&:hoverin your CSS.Step 2: Check each option's hover syntax
.btn { @apply text-white bg-blue-500 px-4 py-2 rounded; &:hover { @apply bg-blue-700; } } correctly uses&:hover { @apply bg-blue-700; }. .btn { @apply text-white bg-blue-500 px-4 py-2 rounded hover:bg-blue-700; } tries to usehover:bg-blue-700inside @apply which is invalid. Options C and D have incorrect hover selector syntax.Final Answer:
.btn { @apply text-white bg-blue-500 px-4 py-2 rounded; &:hover { @apply bg-blue-700; } } -> Option AQuick Check:
Use &:hover with @apply for hover styles = B [OK]
- Trying to use hover: prefix inside @apply
- Using :hover without & in nested CSS
- Incorrect hover selector syntax causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
