The Wayback Machine - https://web.archive.org/web/20241122202924/https://www.geeksforgeeks.org/how-to-use-apply-directive-in-tailwind-css/
Open In App

How to use @apply directive in Tailwind-CSS ?

Last Updated : 31 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Tailwind CSS is a popular utility-first CSS framework. It is a set of pre-defined CSS classes that can be used to quickly and easily style your HTML elements without having to write custom CSS code. The classes are organized into a set of utility classes that can be used to control the layout, spacing, typography, and other aspects of your design.

The @apply directive in Tailwind CSS allows you to apply multiple utility classes to a single CSS class. This can help to reduce the amount of CSS code and make your styles more readable. Here’s an example of how to use the @apply directive in Tailwind CSS:

Steps to use @apply directive in tailwind:

Step 1: Create a new CSS class: In your CSS file, create a new class in which you want to apply the utility classes. For example:

.my-button {
}

Step 2: Apply the utility classes: Inside the .my-button class, use the @apply directive to apply the utility classes that you want to use. For example:

.my-button {
   @apply py-2 px-4 rounded-lg bg-blue-500 text-white;
}

This will apply the py-2, px-4, rounded-lg, bg-blue-500, and text-white utility classes to the .my-button class.

Step 3: Use the class in your HTML: Once you’ve created your new class and applied the utility classes to it, you can use it in your HTML like this:

<button class="my-button">Click me</button>

It’s important to note that when you use the @apply directive, the CSS properties are copied and pasted directly into the new class, so any changes made to the original utility classes will not be reflected in the new class.

Now you have learned how to use the @apply directive, it’s time to test out the skill you have learned with an example.

Example: In this example, we are using @apply to apply utility classes to a specific class and I have changed the following lines of code with gfg_center

flex justify-center items-center min-h-screen

Also, I have changed the following lines of code with gfg_background

bg-green-700 p-4 text-3xl text-white rounded-xl shadow-2xl

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Welcome to GFG</title>
    <link rel="stylesheet" href="style.css">  
</head>
<body>
    <div class="gfg_center">
        <div class="gfg_background">
            <p>Hello Geeks</p>
        </div>
    </div>
</body>
</html>


CSS file will contain tailwind directives along with apply directives

CSS




@tailwind base;
@tailwind components;
@tailwind utilities;
  
.gfg_center{
    @apply flex justify-center items-center min-h-screen
}
  
.gfg_background{
    @apply bg-green-700 p-4 text-3xl text-white rounded-xl shadow-2xl
}


Output:

 

Conclusion: The @apply directive in Tailwind CSS allows you to apply multiple utility classes to a single CSS class, which can help to reduce the amount of CSS code and make your styles more readable. To use the @apply directive, you need to create a new CSS class, apply the utility classes to it using the @apply directive and then use it in your HTML. This feature can be useful in cases where you want to create custom styles that are a combination of multiple utility classes and make it easier to maintain and update your code.



Similar Reads

What is the purpose of @apply Directive in Tailwind CSS ?
The @apply directive in Tailwind CSS allows you to create and apply custom utility classes by defining styles in your own CSS or Sass files. This is useful when you want to reuse a set of Tailwind utility classes. SyntaxCreate a custom class in your CSS or Sass file.custom-button { @apply bg-blue-500 text-white p-2 rounded-md;}Use the custom class
1 min read
How to use Tailwind @apply in CSS Modules in Next.js ?
In Next.js, you can enhance CSS Modules by integrating Tailwind's @apply directive. This approach combines Tailwind's utility classes with modular CSS, ensuring scoped styling and efficient management across your application. We can integrate Tailwind's @apply directive in CSS Modules of Next.js by importing Tailwind's styles and applying utility c
3 min read
What is the use of the tailwind.config.js file in Tailwind CSS ?
The tailwind.config.js file in Tailwind CSS serves as a central configuration hub for customizing the framework. It allows you to define colors, fonts, breakpoints, and more, tailoring Tailwind to fit your project's specific design and functionality requirements. Syntax:/** @type {import('tailwindcss').Config} */module.exports = { content: [], them
1 min read
How to apply background image with linear gradient using Tailwind CSS ?
In this article, we will see how to apply background images with a linear gradient using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. To apply background images with linear gradients we use the background image utility of Tailwind CSS. It is the alternati
2 min read
How to apply Dynamic Styles using Tailwind CSS?
Tailwind CSS helps you style your website easily by using simple classes. Sometimes, you might need to change these styles based on different conditions, like when a button is clicked. Below are the approaches to apply dynamic styles using Tailwind CSS: Table of Content Using Inline StylesUsing CSS Custom PropertiesUsing Inline StylesThe first way
3 min read
How to Apply Inset to Box Shadow in Tailwind CSS?
In web development, shadows play a significant role in attracting user attention. By applying shadows to elements on our web pages, we can enhance their visual appearance and attract the user's focus. In Tailwind CSS, we can apply an insert box shadow by using the "shadow-inner" utility class. ApproachFirst, create a basic HTML structure and includ
2 min read
How to use CSS Animations with Tailwind CSS ?
Tailwind CSS classes are used to style elements and apply animations effortlessly. Utilize Tailwind's animation utility classes to add dynamic visual effects. Combine Tailwind CSS with custom CSS animations for versatile and engaging web designs. Table of Content Tailwind CSS Animation Utility ClassesCSS Animations with Tailwind CSS and JavaScriptT
3 min read
How to use model directive with two way computed property in vue.js ?
Vue.js is an open-source Model–View–View Model front-end JavaScript framework for building user interfaces and single-page applications. Data binding is one of the most important features in web applications. Vue.js provides two-way data binding that helps DOM to update automatically when the data Model is changed. Vue.js uses a v-model directive f
5 min read
How to use trackBy() Function with ngFor Directive in Angular ?
In this article, we will see How to use the 'trackBy' function with 'ngFor' Directive in Angular, along with understanding their basic implementation through illustrations. The NgFor is used as a structural directive that renders each element for the given collection each element can be displayed on the page. The trackBy Function facilitates findin
3 min read
What are the Benefits of Using the use strict Directive in JavaScript ?
The "use strict" directive enables strict mode in JavaScript, which provides several benefits: Strict mode eliminates some JavaScript silent errors by changing them to throw errors.Strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical co
1 min read
How to Use & Create custom directive in Angular?
Directives in Angular are nothing but the classes that allow us to add and modify the behavior of elements. Using directives in angular we can modify the DOM (Document Object Module) styles, handle user functionality, and much more. Table of Content Custom DirectivesSteps to create the DirectiveBenefits of custom directives Custom Directives:A cust
2 min read
Storybook-tailwind. How should I add tailwind to storybook
In this article, we will guide you through the process of adding Tailwind CSS to Storybook to enhance your UI development. Storybook is an open-source UI that helps developers create reusable, organized UI components with proper documentation independently. Tailwind CSS is basically a utility-first CSS framework for building rapid custom UI. Prereq
2 min read
How to use $scope.$apply() in AngularJS ?
In this article, we will be discussing the $apply() function & how to use it in angularjs. In AngularJS, $apply() function is used to evaluate expressions outside of the AngularJS context (browser DOM Events, XHR). Moreover, $apply has $digest under its hood, which is ultimately called whenever $apply() is called to update the data bindings. We
3 min read
How to Use Shaders to Apply Color in WebGL?
Shaders in WebGL are small programs that run on the GPU to control the rendering pipeline. They allow you to manipulate the appearance and position of shapes on the screen. In this example, we use vertex and fragment shaders to apply a uniform color to a shape, making it possible to change the shape's color interactively. The color is set using a u
8 min read
How to use template literals in Tailwind CSS to change classes dynamically ?
Tailwind CSS is a popular utility-first CSS framework used by many web developers to build modern and responsive user interfaces. It provides a wide range of pre-defined classes that can be used to style elements, but what if we want to dynamically change classes based on some condition or user input? This is where template literals come in handy.
6 min read
How to use both Tailwind CSS and Bootstrap 4 at the same time ?
In this article, we will know how to use Bootstrap with Tailwind CSS, at a moment, in the same code. We can use both the CSS frameworks together but it is not recommended. Because few classes will contradict with each other like "container", "clearfix", etc. As we know that Bootstrap is a known CSS framework. Although the Tailwind CSS framework can
2 min read
Can we use Padding Negative in Tailwind CSS ?
It is to be noted that Tailwind CSS doesn't support negative padding values, or in other words, there is no concept that exists for utilizing the padding value as negative. Note: Padding cannot be negative, so instead of using this, you can use negative margin. Instead of using this (negative padding), the negative margin can be used by specifying
2 min read
How to use hover, focus and active variants in Tailwind CSS ?
In this article, we will see how to use hover, focus, and active variants in Tailwind CSS. Tailwind CSS uses the Hover, Focus, and Active variants to style an element when the user mouses move over it, focuses it, or actively clicks/tapped it. These variants allow you to create interactive and dynamic user interfaces without writing custom CSS. The
4 min read
How to use Tailwind CSS with esbuild ?
Tailwind CSS is a utility-first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. An esbuild is a bundler for the web project that brings the build tool for performance enhancement, along with facilitating to development of an easy-to-use modern
3 min read
How to use motion-safe and motion-reduce in Tailwind CSS ?
Tailwind CSS is a CSS framework that helps in building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. In Tailwind CSS, motion-safe and motion-reduce are utility classes used for creating animations. motion-safe ensures animations play regardless of user preferences, whi
2 min read
How to use Ant Design with Tailwind CSS in a React Project ?
The integration of Ant Design and Tailwind CSS in a React project presents challenges due to their differing styling conventions. Ant Design offers a feature-rich component library with its own class names, while Tailwind CSS provides a utility-first framework for custom designs. Combining both libraries requires a thoughtful approach to applying t
2 min read
How to use Tailwind CSS with Django ?
Tailwind CSS has gained immense popularity among developers for its utility-first approach to styling web applications. Django on the other hand is a robust and flexible web framework written in Python. Combining these two powerful tools can enhance your Django projects. In this article, we will explore how to integrate the capabilities of Tailwind
4 min read
How to use line-clamp Class for Text Truncation in Tailwind CSS ?
The line-clamp-[number] Class in Tailwind CSS is used for text truncation by limiting the number of displayed lines. This is particularly useful for creating ellipsis in multi-line text when the content exceeds a specified number of lines. Syntax<div class="line-clamp-3"> <!-- Text content --></div><div class="truncate">
1 min read
How to use the skew and skew-y Classes in Tailwind CSS ?
In Tailwind CSS, the skew-x-[amount] and skew-y-[amount] classes are used to apply skew transformations to elements. These classes allow you to skew an element along the x-axis or y-axis, creating visually interesting effects. Syntax<div class="skew-x-12"> Skewed along the x-axis</div><div class="skew-y-[-8]"> Skewed along the y-a
1 min read
How to use Diagonal Fractions in Tailwind CSS ?
Tailwind CSS diagonal-fractions utility allows you to display numbers separated by a slash as common diagonal fractions. This activates those sets of figures where the numerator and denominator appear small and are separated by a slash. Syntax:<element class="diagonal-fractions"> Content </element>Diagonal-fractions UtilityThe diagonal-
2 min read
How to use Flexbox Utilities in Tailwind CSS ?
To use Flexbox utilities in Tailwind CSS, apply classes that control the layout and alignment of flex container and flex items. Common classes include flex, flex-row, flex-col, justify-between, items-center, and more. Syntax<div class="flex flex-row justify-between items-center"> <div class="flex-shrink-0">Logo</div> <div class
2 min read
What is use PurgeCSS in a Tailwind CSS Project ?
PurgeCSS in a Tailwind CSS project is a tool used to eliminate unused CSS styles from your final production build. It analyzes your codebase and removes any styles that are not applied in your HTML or JavaScript, resulting in a smaller and more optimized stylesheet for better performance. Steps to Configure the PurgeCSSStep 1: Install the PostCSS p
1 min read
How to use Ring and Ring-offset Classes in Tailwind CSS ?
In Tailwind CSS, the ring and ring-offset classes are used to add outline-like effects around elements, allowing you to create various visual styles such as focus rings, border highlights, and more. To use the ring and ring-offset classes in Tailwind CSS, apply them to HTML elements to add visual enhancements like border rings and offsets. Use the
2 min read
How to use ::before and ::after elements in Tailwind CSS ?
In Tailwind CSS, we can use the ' :: before ' and ' :: after ' pseudo-elements by adding utility classes directly in your HTML. Simply apply the "before:" or "after:" prefix followed by the desired utility classes to style elements before or after their content. Using Utility ClassesTailwind CSS offers utility classes for seamless modification of p
1 min read
How to use Radial Gradient in Tailwind CSS ?
A radial gradient is a graphical effect where colors transition in a circular or elliptical pattern. By default, the first color starts at the center and fades out to the edge of the element. Tailwind CSS makes it easy to implement radial gradients with its utility classes. We can use the below approaches to achieve a Radial gradient in Tailwind CS
3 min read
three90RightbarBannerImg