Css

What is apply in CSS

20 September 2026 · 9 min read

What is apply in CSS

Cascading Style Sheets (CSS) are the backbone of web design, dictating the visual presentation of HTML elements. As projects grow in complexity, managing CSS efficiently becomes crucial. One powerful tool that helps achieve this is the @apply directive. The @apply directive in CSS allows developers to reuse predefined sets of CSS declarations, promoting consistency and reducing redundancy. It’s essentially a way to inject existing styles into new CSS rules, making stylesheets more modular and maintainable. Understanding how @apply works and when to use it can significantly improve your CSS workflow and the overall quality of your web projects.

Understanding the Basics of @apply

The @apply directive functions by taking a set of CSS properties and values defined in a CSS class or a custom property (variable) and inserting them directly into another CSS rule. This approach avoids the need to rewrite the same styles multiple times, which can lead to errors and increased maintenance effort. Think of it as a CSS “mix-in” or a shortcut for applying a group of styles at once. The core benefit is that it helps keep your CSS DRY (Don’t Repeat Yourself). This directive is particularly useful when working with CSS frameworks like Tailwind CSS, where utility classes are heavily used and can be easily composed using @apply.

To illustrate, consider a scenario where you have a button style defined in a CSS class named .button-primary. You can use @apply to include these styles in another button class, say .submit-button. The .submit-button will inherit all the styles from .button-primary, and you can add any additional specific styles as needed. This not only saves time but also ensures visual consistency across your application. By centralizing style definitions, you can easily make global changes and ensure they are reflected everywhere the @apply directive is used.

However, it’s important to note that the @apply directive is not a standard CSS feature and is primarily used within CSS-in-JS libraries and preprocessors like Tailwind CSS and Styled Components. When using @apply, ensure that your build process correctly handles these directives. Without the proper tooling, the browser will not understand the @apply syntax, and the styles will not be applied correctly. For further reading on CSS preprocessors, check out this article on CSS-Tricks CSS Preprocessors.

Benefits of Using @apply in CSS

Using @apply offers several advantages that contribute to more efficient and maintainable CSS. One of the primary benefits is reduced code duplication. By reusing styles defined in other classes, you avoid repeating the same CSS properties and values throughout your stylesheet. This not only makes your code cleaner and easier to read but also reduces the chances of introducing inconsistencies when making updates. Imagine having to change a color across multiple button styles; with @apply, you only need to change it in one place.

Another significant benefit is improved maintainability. When styles are centralized and reused, making changes becomes much simpler. If you need to update a common style, you only need to modify the original class or custom property, and the changes will automatically propagate to all the places where @apply is used. This reduces the risk of forgetting to update a style in one place, leading to visual discrepancies. It also makes it easier for other developers to understand and modify the codebase.

Furthermore, @apply can contribute to better performance. While the impact might not be significant for small projects, in large applications with complex stylesheets, reducing code duplication can lead to smaller CSS file sizes. Smaller files mean faster download times, which can improve the overall loading speed of your website. Additionally, some CSS-in-JS libraries optimize the generated CSS code when using @apply, further enhancing performance. According to Google’s PageSpeed Insights, optimizing CSS delivery can significantly improve page load times Optimize CSS Delivery.

  • Reduced code duplication
  • Improved maintainability
  • Better performance (potentially)

How to Implement @apply in Your Projects

Implementing @apply is relatively straightforward, especially when using CSS frameworks like Tailwind CSS. However, the exact steps might vary depending on your specific setup and the tools you are using. Here’s a general guide on how to get started:

  1. Choose a CSS-in-JS library or preprocessor: If you are not already using one, select a library that supports @apply. Tailwind CSS is a popular choice, but Styled Components and other similar tools also provide this functionality.
  2. Define your base styles: Create CSS classes or custom properties that encapsulate the styles you want to reuse. These will serve as the source for the @apply directive.
  3. Use @apply in your CSS rules: In the CSS rules where you want to apply the styles, use the @apply directive followed by the name of the CSS class or custom property.
  4. Configure your build process: Ensure that your build process is set up to handle the @apply directive. This might involve installing specific plugins or configuring your bundler to process the CSS correctly.
  5. Test your styles: After implementing @apply, thoroughly test your website to ensure that the styles are applied correctly and that there are no visual issues.

For example, in Tailwind CSS, you might have a utility class like .font-bold that sets the font-weight to bold. You can then use @apply font-bold in another class to apply this style. Similarly, you can use @apply with custom CSS properties (variables) to reuse sets of values. Remember to consult the documentation of your chosen CSS-in-JS library or preprocessor for specific instructions and best practices. For example, Tailwind CSS provides extensive documentation on how to use the @apply directive effectively Tailwind CSS @apply Documentation.

Consider a real-world example where you are building a design system for a large application. You might define a set of base button styles in a class called .button-base. This class could include styles for padding, font size, and background color. Then, you can create specific button styles like .button-primary and .button-secondary and use @apply .button-base to inherit the base styles. This ensures that all your buttons have a consistent look and feel, while still allowing you to customize the specific styles for each button type.

Best Practices and Potential Pitfalls

While @apply can be a powerful tool, it’s essential to use it judiciously and follow best practices to avoid potential pitfalls. One common mistake is overusing @apply, which can lead to overly complex and difficult-to-understand stylesheets. It’s best to reserve @apply for cases where you are truly reusing a set of styles across multiple components or elements. Avoid using it for simple, one-off styles that are only needed in one place. According to a study by Smashing Magazine, well-structured CSS is crucial for long-term project maintainability Maintainable Scalable React UI Architecture.

Another potential issue is specificity conflicts. When you use @apply, the styles from the applied class are inserted directly into the current rule. This means that the specificity of those styles can be affected by the context in which they are applied. If you are not careful, this can lead to unexpected style overrides and make it difficult to debug your CSS. To avoid this, make sure that the specificity of the applied styles is appropriate for the context in which they are being used. You can also use CSS specificity tools to analyze and manage your CSS specificity.

Furthermore, be mindful of the order in which you define your styles. The order of CSS rules can affect the final result, especially when using @apply. Make sure that the styles you are applying are defined before the rules that use them. This ensures that the styles are applied correctly and that there are no unexpected overrides. Also, remember that @apply is not standard CSS and requires a preprocessor or CSS-in-JS library to work. Make sure your development environment is properly configured to handle it. Use descriptive anchor text for all links to improve SEO and user experience.

  • Avoid overusing @apply.
  • Be mindful of specificity conflicts.
  • Ensure proper style definition order.
Infographic here
FAQ About `@apply` in CSS -------------------------
What is the main purpose of `@apply`?
The main purpose of `@apply` is to reduce code duplication and improve maintainability by reusing predefined sets of CSS declarations.
Is `@apply` standard CSS?
No, `@apply` is not standard CSS. It is primarily used within CSS-in-JS libraries and preprocessors like Tailwind CSS and Styled Components.
How does `@apply` affect CSS specificity?
`@apply` can affect CSS specificity because the styles from the applied class are inserted directly into the current rule, potentially leading to unexpected style overrides. You should manage specificity carefully.
Can I use `@apply` with custom CSS properties (variables)?
Yes, you can use `@apply` with custom CSS properties to reuse sets of values, making your stylesheets more flexible and maintainable.
What are some potential pitfalls of using `@apply`?
Potential pitfalls include overusing `@apply`, leading to complex stylesheets, and specificity conflicts that can make debugging difficult.
In summary, `@apply` is a powerful CSS directive that can significantly improve your workflow by promoting code reuse and maintainability. By understanding its benefits, implementation details, and potential pitfalls, you can effectively leverage `@apply` to create cleaner, more efficient, and more scalable CSS code. Remember to choose the right tools, follow best practices, and always test your styles thoroughly to ensure that everything works as expected. Embrace the power of `@apply` to streamline your CSS development and elevate your web projects.

Question & Answer :
I’m curious what the CSS directive @apply does. I have googled @apply but I couldn’t find anything that could explain its meaning properly for me.

What is the usage of such a directive?

The simple way of explaining it would be; introducing variables into CSS (which is a feature of preprocessors, such as Sass), and mixins which are function like behaviors (also in preprocessors).

Imagine that --header-theme is a function (mixin):

:root { --header-theme: { color: red; font-family: cursive; font-weight: 600; }; } h1 { @apply --header-theme; } h2 { @apply --header-theme; } 

This way, you can use it in many different places without having to rewrite it again (DRY).

Now the variable part could be explained with this example:

:root { --brand-color: red; /* Default value */ --header-theme: { color: var(--brand-color); font-family: cursive; font-weight: 600; }; } h1 { @apply --header-theme; } h2 { --brand-color: green; @apply --header-theme; } 

The mixin will have a variable sent to it and change the color.

This is not the limits of the feature, and you can use it for far more. You can read more about mixin and variables in Sass for other ways of using it, and after I suggest you read this blog post.

Now after I got you excited, it is time for the bad news. It is not implemented in browsers yet (Chrome), but it is still worth knowing that it is coming and maybe if you want to prepare yourself start with Sass.