@apply helps you reuse groups of Tailwind CSS classes by putting them into one custom class. This keeps your code neat and easy to change.
@apply for extracting patterns in Tailwind
.custom-class {
@apply utility-classes;
}Use @apply inside a CSS or Tailwind CSS file.
You list the Tailwind utility classes you want to combine after @apply.
btn class with blue background, white bold text, padding, and rounded corners..btn {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}card class with shadow, padding, white background, and rounded corners..card {
@apply shadow-lg p-6 bg-white rounded-lg;
}alert style with red background, border, text, padding, and rounded edges..alert {
@apply bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded;
}This example shows a button using a custom btn-primary class. The class uses @apply to combine Tailwind utilities for background color, text color, font weight, padding, rounded corners, and a hover effect.
The button is centered on the page with a light gray background.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>@apply Example</title> <script src="https://cdn.tailwindcss.com"></script> <style> .btn-primary { @apply bg-blue-600 text-white font-semibold py-2 px-4 rounded hover:bg-blue-700; } </style> </head> <body class="flex items-center justify-center min-h-screen bg-gray-100"> <button class="btn-primary" aria-label="Primary action button">Click Me</button> </body> </html>
You must use @apply inside a CSS file processed by Tailwind's build tool.
Not all Tailwind utilities can be used with @apply (for example, arbitrary value utilities like w-[100px] or certain complex utilities).
Using @apply helps keep your HTML cleaner and your styles consistent.
@apply lets you combine Tailwind classes into one custom class.
It helps reuse styles and keep your code tidy.
You write @apply inside CSS to extract patterns from utility classes.
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
