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

What is the purpose of @apply Directive in Tailwind CSS ?

Last Updated : 05 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Syntax

Create 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 in your HTML

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

Here, the @apply Directive can be used to apply a set of Tailwind utility classes to a custom class.

Key Features

  • Modular Styling: Encapsulate styles and create reusable components by defining custom classes with @apply.
  • Tailwind Compatibility: The @apply directive seamlessly integrates with Tailwind CSS, allowing for the combination of utility classes.
  • Responsive Styles: Combine @apply with responsive variants (sm:, md:, lg:, xl:) for adaptability.

Conclusion

By using the @apply directive, Tailwind CSS users can maintain the benefits of utility-first styling while achieving modularity and code reusability in a more structured and efficient manner.


Previous Article
Next Article

Similar Reads

How to use @apply directive in Tailwind-CSS ?
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.
3 min read
Purpose of the @Directive decorator.
In Angular, Directives are defined as classes that can add new behavior to the elements in the template or modify existing behavior. The `@Directive` decorator in Angular is used to create a directive, which is a class that allows us to attach behavior to elements in the DOM. Directives are a way to extend the functionality of HTML elements and att
5 min read
What is the purpose of using the Gap Classes in Tailwind CSS ?
Tailwind CSS Gap Classes such as gap-[size], are used to define the gap or spacing between grid or flex container child elements. These classes simplify the management of spacing, creating consistent layouts by controlling the space between elements horizontally (with gap-x-[size]) and vertically (with gap-y-[size]). Syntax:<div class="grid grid
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 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
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
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
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
Explain the purpose of clearing floats in CSS
In this article, we will learn to clear float and the purpose of clearing floats in CSS. Before diving into this topic, we will know the float property in CSS. The float property in CSS is used to change the normal flow of an element. The float property defines where should be an element place container's left or right side. Purpose of clearing flo
2 min read
What is the purpose of the 'line-height' property in CSS ?
The line-height property in CSS is used to control the height of a line of text within an element. It defines the amount of space above and below inline elements, such as text, to determine the overall height of a line. Syntaxp { line-height: 1.5;}The primary purposes of the line-height property include: Vertical Alignment: Ensures consistent verti
1 min read
What is the purpose of the transform Property in CSS ?
The transform Property in CSS is used to apply various graphical transformations to HTML elements. It allows developers to modify the appearance and layout of elements by altering their position, rotation, scale, and skew. The primary purposes of the transform property include: Positional AdjustmentsMove elements in the 2D or 3D space, shifting the
1 min read
Explain the purpose of using the CSS box-sizing Property
In CSS, targeting only the first child of an element is useful for applying styles exclusively to the first child element within a container. The :first-child pseudo-class is used to select and style the first child element. Syntax/* Selecting and styling the first <p> element within a container */ .container p:first-child { font-weight: bold
1 min read
What is the purpose of using the CSS box-decoration-break Property ?
The box-decoration-break Property in CSS is used to control the rendering behavior of box decorations, such as borders and backgrounds when a box is broken across multiple lines. It allows you to determine whether these decorations should behave as continuous or broken across line breaks. Note: The box-decoration-break Property accepts values like
1 min read
What is the purpose of the Transition Property in CSS ?
The Transition property in CSS is used to smoothly animate changes in property values over a specified duration. It enhances the user experience by creating fluid and visually appealing transitions between different states of an element. The transition property is applied to the element being animated, specifying the properties to transition, the d
1 min read
What is the purpose of the outline Property in CSS ?
The outline property in CSS is used to set the style, color, and width of an outline around an element. It is often used to highlight elements without affecting their layout, providing a visible focus or emphasis. Syntax:/* Applying a red dotted outline to focused elements */ :focus { outline: 2px dotted red; } FeaturesFocus Indication: Commonly us
1 min read
What is the purpose of the scroll-snap-type Property in CSS ?
The scroll-snap-type property in CSS is used to control the snapping behavior of scroll positions within a scroll container. It defines how the scrolling alignment should occur when users scroll through a container, creating a smooth and controlled scrolling experience. Note: The scroll-snap-type property can take values like none, mandatory, and p
1 min read
What is the purpose of CSS-in-JS libraries in React?
CSS-in-JS libraries in React help you to write and manage styles for your components directly within JavaScript files. This approach offers several advantages over traditional methods of styling in web development, where CSS is typically written in separate files and then linked to HTML.Main purposes & benefits of using CSS-in-JS libraries in R
3 min read
Which CSS framework is better Tailwind CSS or Bootstrap ?
In this article, we will see which CSS framework is better tailwind CSS or bootstrap. Both are widely used CSS frameworks for styling the front end. We will understand each one of them in detail. Both frameworks have their own pros and cons so it depends upon the type of application you are going to build and accordingly you can choose the best fra
5 min read
How to Add a @tailwind CSS Rule for CSS Checker ?
Tailwind CSS is basically 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. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. In this article, we will ex
4 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 apply multiple transform property to an element using CSS ?
There is a variety of procedures to apply multiple transform property in an element. In this article we will discuss the easiest and popular ones. In the 1st method we will be combining the transform property itself. In the 2nd method, we will use the nested classes to apply other transforms. Method 1: In this method, we will combine the transform
3 min read
How to apply box-shadow to display elements like single element to user in CSS ?
In CSS, the box-shadow property allows you to add a shadow effect to an element. However, when it comes to applying box shadow to a combination of elements such as a <span> and <input>, it can be a bit tricky to get the desired effect. In this article, we will discuss how to apply box-shadow to a <span> <input> combination s
2 min read
How to apply CSS page-break to print a table with lots of rows?
When printing a table with lots of rows the problem can arise in keeping the data together when the page ends. Since the data presented continuously makes more sense. Here we will track down ways by which one can print contents of a table with lots of rows when the situation of page-break is encountered. The most logical property that can be used f
3 min read
How to apply css property to a child element using JQuery?
The task is to apply CSS property to a child element using jQuery. To do that, first we select the child element with the help of children() method in jQuery and then apply CSS property to it with the help of css() method in jQuery. Syntax: // Note : children method selects the direct child to parent $(selector).children("selector").css("property-n
2 min read
How to apply Amazing Hover Effect over Button using HTML and CSS ?
You have visited many websites and you have seen many hover buttons present in those websites. So, such Hover Button animation effect can be easily generated by using HTML and CSS. By using HTML we will design the basic structure of the button and then by using the properties of CSS, we can create the hover animation effect. A sample video is provi
3 min read
How to apply CSS Padding Shorthand Property ?
Padding is the space between the element and its border. The shorthand property of padding sets the padding area in all four sides of the element. The padding value can be in px, em, rem or %. If we want to apply padding on a specific side we can use the following properties: padding-left: Sets padding on the left side of the element.padding-right:
2 min read
How to apply inline CSS ?
In this article we will learn how to apply inline CSS, Inline CSS contains the CSS property in the body section attached to the element is known as inline CSS. This kind of style is specified within an HTML tag using the style attribute. It is used to apply a unique style to a single HTML element. Syntax: <tag style = " "></tag> Example
1 min read
How to apply no shadow in an element using CSS ?
In this article, we are going to learn How to apply no shadow in an element using CSS, To remove the box shadow from an element in CSS, you can use the box-shadow property with a value of none, like this: box-shadow: none, This will effectively remove any existing shadow or prevent a shadow from being applied to the element. In this article, we wil
1 min read
What is @apply in CSS ?
In this article, we will see the @apply rule property in CSS. This rule is used to implement a bunch of CSS properties which is already been declared in a variable like a method in JAVA. In simple words, we will make a set of CSS properties and store them into a variable for later use and we can use these properties as much time as we want. Syntax:
2 min read
three90RightbarBannerImg