Reusable patterns help you write less code and keep your website looking consistent. They save time and make changes easier.
Why reusable patterns matter in Tailwind
Use Tailwind utility classes grouped in reusable components or custom classes.
Example:
<button class="btn-primary">Click me</button>
In CSS or Tailwind config:
.btn-primary {
@apply bg-blue-500 text-white py-2 px-4 rounded;
}@apply directive lets you combine utility classes into one reusable class.<button class="bg-green-500 text-white py-2 px-4 rounded">Save</button>@apply to create a reusable button style..btn-save {
@apply bg-green-500 text-white py-2 px-4 rounded;
}
<button class="btn-save">Save</button><button class="btn-primary">Submit</button> /* btn-primary defined once with @apply */
This example shows two buttons using the same reusable combination of Tailwind utility classes. Both buttons look consistent and share hover and focus styles. For more advanced reusability, define custom classes using Tailwind's @apply directive in a PostCSS-enabled setup.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Reusable Patterns Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-2xl mb-4">Buttons with Reusable Pattern</h1> <button class="bg-blue-600 text-white font-semibold py-2 px-6 rounded hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400">Save</button> <button class="bg-blue-600 text-white font-semibold py-2 px-6 rounded hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 ml-4">Cancel</button> </body> </html>
Reusable patterns reduce mistakes by keeping styles consistent.
Using @apply requires Tailwind CSS setup that supports it (like PostCSS).
Reusable classes help when you want to change a style quickly across many elements.
Reusable patterns save time and keep your site consistent.
Tailwind's @apply helps create reusable style classes.
Use reusable classes for buttons, colors, spacing, and more.
Practice
Solution
Step 1: Understand the purpose of reusable patterns
Reusable patterns help avoid repeating the same styles and keep the design uniform.Step 2: Connect with Tailwind's approach
Tailwind encourages using utility classes and reusable styles to speed up development and maintain consistency.Final Answer:
They save time and keep the design consistent across the site. -> Option CQuick Check:
Reusable patterns = save time + consistency [OK]
- Thinking reusable patterns slow down the site
- Believing reusable means writing more CSS manually
- Confusing reusable with avoiding Tailwind utilities
Solution
Step 1: Identify Tailwind's reusable style feature
The@applydirective lets you combine utility classes into one reusable class.Step 2: Differentiate from other CSS rules
@importloads CSS files,@mediahandles responsiveness, and@font-faceloads fonts, none create reusable classes.Final Answer:
The@applydirective -> Option AQuick Check:
@apply= reusable styles [OK]
@apply combines utilities into reusable classes [OK]- Confusing
@applywith@import - Thinking
@mediacreates reusable classes - Mixing up CSS font rules with Tailwind features
<button class="btn-primary p-4">Click Me</button>
/* CSS */
.btn-primary {
@apply bg-blue-600 text-white rounded-lg;
}Solution
Step 1: Understand the
The@applyusage.btn-primaryclass appliesbg-blue-600,text-white, androunded-lgstyles.Step 2: Combine with padding from HTML
The button also hasp-4class adding padding around the content.Final Answer:
A button with blue background, white text, rounded corners, and padding -> Option AQuick Check:
bg-blue + text-white + rounded + p-4 = styled button [OK]
@apply adds Tailwind utilities to custom classes [OK]- Ignoring the padding class
p-4 - Thinking
@applychanges text color to blue - Assuming no styles apply without inline utilities
@apply:.btn {
@apply bg-green-500 text-white p-3 rounded;
}
.btn {
@apply bg-red-500;
}Solution
Step 1: Analyze multiple
Applying@applyon same class@applytwice on.btnmeans the second overrides the first styles.Step 2: Understand style overriding in CSS
The second@apply bg-red-500replaces the green background, causing inconsistency.Final Answer:
The second@applyoverrides the first, causing inconsistent styles. -> Option BQuick Check:
Multiple@applyon same class = override [OK]
- Thinking multiple @apply are not allowed at all
- Believing @apply cannot use color classes
- Confusing CSS syntax rules with HTML placement
Solution
Step 1: Understand reusable pattern goals
Reusable patterns should combine common styles and allow easy customization like hover effects.Step 2: Evaluate options for combining base and hover styles
Using@applyfor base styles and adding hover utilities in HTML keeps code clean and flexible.Step 3: Reject less efficient approaches
Writing all inline repeats code, default classes may lack needed styles, and splitting padding and hover separately is less maintainable.Final Answer:
Create a custom class using@applywith base styles and add hover utilities in HTML. -> Option DQuick Check:
Reusable base + hover in HTML = best practice [OK]
- Writing all styles inline causing repetition
- Relying only on default classes without customization
- Separating padding and hover styles unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
