Using @apply to Extract Reusable Styles

Tutorial 2 of 5

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:

  • @apply works best with utility classes and does not support pseudo-classes or variants.
  • @apply should 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 @apply directive 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

  1. Exercise 1: Create a .hero class 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; }

  1. Exercise 2: Create a responsive .card class that changes padding and margins based on screen size.

Solution:
css .card { @apply p-4 md:p-8 mt-4 md:mt-8; }

  1. Exercise 3: Define a .form-input class 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!