Tailwind CSS / Components and Reusable Styles
Using @apply to Extract Reusable Styles
In this tutorial, we'll explore how to use the @apply directive in Tailwind CSS to extract common utility patterns into reusable classes. This is a great way to keep your styleshe…
Section overview
5 resourcesCovers creating reusable components and extracting styles in Tailwind CSS.
Using @apply to Extract Reusable Styles
1. Introduction
In this tutorial, our goal is to learn how to use the @apply directive in Tailwind CSS for extracting common utility patterns into reusable classes. This is a powerful technique that can help maintain clean, manageable, and DRY ("Don't Repeat Yourself") stylesheets.
By the end of this tutorial, you will be able to:
- Understand the @apply directive in Tailwind CSS
- Extract common utility patterns into reusable classes
- Maintain cleaner and more manageable stylesheets
Prerequisites: A basic understanding of HTML, CSS, and Tailwind CSS is required to follow along with this tutorial.
2. Step-by-Step Guide
The @apply directive in Tailwind CSS allows you to extract classes into reusable components. This way, you can apply multiple utility classes to an element at once using the custom class name.
Example:
.btn {
@apply py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700;
}
In the above example, we've created a custom class called .btn that applies several utility classes at once.
Best Practices and Tips:
@applyworks best with utility classes and does not support pseudo-classes or variants.@applyshould be used sparingly, as Tailwind CSS encourages utility-first CSS. It can be beneficial for extracting complex styles that are used in multiple places.
3. Code Examples
Let's take a look at some practical examples.
Example 1: Basic @apply usage
.card {
@apply p-6 mt-6 text-left border w-96 rounded-xl hover:text-blue-600 focus:text-blue-600;
}
In this example, we've created a .card class that applies several utility classes.
Example 2: @apply with responsive design
.container {
@apply mx-auto px-4 sm:px-6 lg:px-8;
}
Here, we're using @apply with responsive classes. The sm: and lg: prefixes apply styles at different screen sizes.
Example 3: @apply with theme customization
.custom-button {
@apply bg-theme-color text-white;
}
In this example, we're using @apply to apply a custom theme color defined in the Tailwind CSS configuration file.
4. Summary
In this tutorial, we've learned:
- The concept of the
@applydirective in Tailwind CSS - How to extract common utility patterns into reusable classes
- How to maintain cleaner and more manageable stylesheets
To further develop your skills, you could explore more about customizing your Tailwind CSS configuration.
5. Practice Exercises
- Exercise 1: Create a
.heroclass that applies a set of utility classes for a full-width, center-aligned text over a background image.
Solution:
css
.hero {
@apply w-full text-center bg-cover bg-center bg-no-repeat;
}
- Exercise 2: Create a responsive
.cardclass that changes padding and margins based on screen size.
Solution:
css
.card {
@apply p-4 md:p-8 mt-4 md:mt-8;
}
- Exercise 3: Define a
.form-inputclass that applies a set of utility classes including focus styles.
Solution:
css
.form-input {
@apply px-4 py-2 border border-gray-300 focus:outline-none focus:ring-2 focus:ring-blue-200 rounded-md;
}
Remember, the more you practice, the more you learn. Happy coding!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article