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…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers 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:

  • @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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help