Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS Reusing Styles

Tailwind CSS Reusing Styles

Reusing styles in Tailwind CSS is a common need, especially as a project grows, you may find yourself repeating the same style combinations in different places.

In Tailwind CSS, styles are implemented through low-level utility classes, which is ideal for avoiding the problem of premature abstraction, giving you flexible control over styles and being very efficient in many scenarios.

However, as a project grows, the repeated use of the same utility class combinations in code becomes inevitable, which can lead to redundancy and maintenance difficulties. At this point, how to manage duplicate styles and create reusable abstractions becomes especially important.

Why Are There Duplicate Styles?

The core philosophy of Tailwind CSS is Utility-First. By combining a series of low-level utility classes, you can control element styles very precisely. Although this approach is powerful at the beginning, as projects become more complex, especially when dealing with common UI elements (such as buttons, cards, avatars, etc.), the same utility classes are often reused in multiple places.

Consider the following template, where each avatar image uses the same utility classes:

Example

 

User 1

 

User 2

 

User 3

 

User 4

 

User 5

Try it »

Here, each avatar image uses the same utility class combination: w-16 h-16 rounded-full border-2 border-white.

Although this approach is concise, as the number of avatars increases, the code becomes increasingly verbose, and if you need to adjust these styles (such as changing the border color or corner radius), you must manually modify each class, making maintenance difficult.

Solutions for Duplicate Styles

Tailwind Through features such as @apply, @layer, and variants, you can easily create reusable styles, reduce code duplication, and improve development efficiency and maintainability.

1. Using the @apply Directive to Create Custom Classes

Tailwind provides the @apply directive, which lets you combine a set of commonly used utility classes into a custom class in a CSS file, thereby reducing duplicate code in HTML. In this way, you can abstract repeated utility classes into a reusable class.



@tailwind base;

@tailwind components;

@tailwind utilities;



/* In your CSS file */

.avatar {

  @apply w-16 h-16 rounded-full border-2 border-white;

}

Then, in HTML, you only need to use this custom avatar class instead of repeatedly writing the same utility classes:

Example

 

User 1

 

User 2

 

User 3

 

User 4

 

User 5

In this way, you avoid repeatedly writing the same utility classes in HTML, while also making future style maintenance and modification easier.

Compiling Tailwind CSS uses Tailwind CLI or PostCSS to compile:



npx tailwindcss -i ./styles.css -o ./output.css --watch

The output.css file generated after compilation replaces @apply with specific CSS styles.

** 2. Using @layer and @apply to Define More Complex Styles**

If you have a more complex set of utility class combinations across multiple elements, you can use @layer to define these styles and combine them with @apply.



@tailwind base;

@tailwind components;

@tailwind utilities;



/* Define a custom hierarchy and apply a set of utility classes */

@layer components {

  .avatar {

    @apply w-16 h-16 rounded-full border-2 border-white shadow-md;

  }

  .button {

    @apply px-4 py-2 bg-blue-500 text-white rounded-lg;

  }

}

Then, in HTML, you can still use these predefined styles through the avatar and button classes.

Example

 

User 1

 

This approach allows you to define reusable classes in CSS files and keeps HTML clean.

** 3. Using Tailwind CSS's variants Feature**

In Tailwind CSS, variants allow you to extend existing utility classes through different states (such as responsive, hover, focus, etc.). If certain utility class combinations in your project need to vary based on different states or device sizes, you can use variants to create reusable styles with responsive and interactive states.

For example, create a responsive avatar style:

@layer components {

  .avatar {

    @apply w-16 h-16 rounded-full border-2 border-white;

  }



  /* Responsive change: use larger avatar on larger screens */

  @media (min-width: 768px) {

    .avatar {

      @apply w-24 h-24;

    }

  }

}

This approach ensures avatars maintain a consistent appearance across different devices and can automatically adjust their size based on screen size.