Back

Dec 28, 2022

Dec 28, 2022

Tailwind @Apply: Replacing Complex Classes with Tailwind CSS

Let's explore the Tailwind CSS utility @apply — a directive that allows developers to combine a collection of utilities into a reusable component class.

decorative cover image showing CSS and JavaScript textbooks
decorative cover image showing CSS and JavaScript textbooks
decorative cover image showing CSS and JavaScript textbooks

What is Tailwind CSS?

Tailwind CSS is an open-source utility-based CSS framework that provides a set of customizable utilities for constructing web designs and layouts.

The key difference between Tailwind CSS framework and other CSS frameworks like Bootstrap and Material UI is that it does not contain a collection of preset classes for components such as buttons and tables. Instead, it includes extensive utility classes for grids, margins, forms, placement, and so forth.

Tailwind CSS automatically removes any extraneous CSS, resulting in the shortest CSS bundle possible when building for production.

How to Install Tailwind CSS into Your Project

The Tailwind CLI tool is the quickest and easiest way to embed Tailwind CSS into your project from scratch. Check that you have Node.js installed before you begin.

Step 1: Install Tailwind CSS into your project with the following command:

npm install -D tailwindcss

Save this code

Step 2: Then, create a tailwind.config.js file with this command:

npx tailwindcss init

Save this code

Step 3: In your tailwind.config.js file, add the paths to all of your template files like this:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

Save this code

Step 4: Add the @tailwind directives to your main CSS file to represent each of Tailwind's layers.

@tailwind base;
@tailwind components;
@tailwind utilities;

Save this code

Step 5: Add your compiled CSS file to the of your markup file and begin styling your content with Tailwind's utility classes.

Save this code

To ensure you are setting up you Tailwind properly, you can watch this video to Install tailwind css from scratch using the CLI

Getting Started with Tailwind CSS @apply

Tailwind CSS’s @apply directive is a relatively new feature in the Tailwind CSS ecosystem that allows developers to “apply” existing utility classes in line with their custom CSS. It is convenient when you need to write custom CSS (for example, to override styles in a third-party library) but still want to work with your design tokens and use the same syntax as you would in HTML.

@tailwind base;
@tailwind components;
@tailwind utilities;
.class {
 background-color: blue;
 border-radius: 50%;
}
/* re-using the styling for the first class here */
.class-2 {
 @apply class;
}
/* applying Tailwind CSS utility classes */
.another__class {
 @apply border border=gray-300 rounded;
}

Save this code

How to Use the Tailwind @apply Directive in Your Next Project

@apply allows you to reuse preset utility classes to style several components in your application. You can also combine its functionalities with other front-end frameworks, such as Bootstrap.

Consider the code below:

 Button Success Button Danger Button

Save this code

In the code above, we created three buttons by using the @apply directive to import preset utility classes.

Here’s the result:

CSS image showing three buttons, one blue, one green, one red

You may discover that the buttons share various utility classes. These utility classes can be used for the base styling of any variant of our buttons. Consider an application with 25 buttons. We must then write the common utility classes (used for base styling) 25 times. This can lead to maintenance issues, because updating the common utility classes requires you to do so in 25 different places throughout the app, which quickly becomes complex and redundant.

In such cases, you can use the @apply directive to convert standard utility classes into custom CSS classes. We can avoid duplicating the traditional utility classes this way.

We've extracted our utility classes into the btn class below:

Save this code

We now have the appropriate level of abstraction for our utility classes after grouping the utility classes into the btn class.

Note: To avoid specificity issues, you can use the @layer components directive to notify Tailwind which layer your custom component styles reside in. @layer allows you to control declaration order by automatically shifting your styles to the equivalent @tailwind directive, and it also allows you to use features like modifiers and tree-shaking in your own custom CSS. You can access this guide to get a better understanding of the @layer directive.

Using @apply with Tailwind CSS Components

Component frameworks like Svelte and Vue allow per-component styling within a

Save this code

The error appears because frameworks like Vue and Svelte process each

Written by

Written by

Anda Mary

Anda Mary

SHARE

SHARE

our newsletter

Sign up for The Pieces Post

Check out our monthly newsletter for curated tips & tricks, product updates, industry insights and more.

our newsletter

Sign up for The Pieces Post

Check out our monthly newsletter for curated tips & tricks, product updates, industry insights and more.

our newsletter

Sign up for The Pieces Post

Check out our monthly newsletter for curated tips & tricks, product updates, industry insights and more.