Tailwind CSS directives: an introduction with code examples

Tailwind CSS is a popular utility-first CSS framework that provides a simple and flexible way to build fast-loading and highly-customizable designs. 

One of the key features of Tailwind CSS is its built-in directives, which allow you to conditionally apply styles based on the values of specific attributes. In this article, we will take a closer look at the directives available in Tailwind CSS and explore some code examples to show how they can be used in practice.

@apply

The @apply directive allows you to apply styles from a utility class to a CSS class. This makes it possible to create reusable styles that can be easily reused throughout a project.

<style>
.bg-blue-500 {
  @apply bg-blue-500;
}
</style>

<div class="bg-blue-500">Hello World!</div>

@tailwind

The @tailwind directive is a way to customize and extend the default styles provided by Tailwind CSS. It allows you to define your own styles and apply them to your HTML elements using utility classes. You can use the @tailwind directive to customize the default styles for your specific design needs, or to add new styles that are not provided by the default configuration.

Here's an example of how to use the @tailwind directive to define a custom utility class:

<style>
@tailwind base;

@tailwind components;

@tailwind utilities {
  .text-blue-500 {
    color: #007aff;
  }
}
</style>

<p class="text-blue-500">Hello World!</p>

In this example, the @tailwind directive is used to extend the base and component styles provided by Tailwind CSS, and to define a custom utility class called text-blue-500. The text-blue-500 utility class can be applied to any HTML element to change its text color to blue.

@layer

The @layer directive in Tailwind CSS is used to group and isolate a set of styles within a custom utility class. It creates a new style layer that can be referenced within the stylesheet, allowing you to create highly specific and targeted styles.

Here's an example of how to use the @layer directive:

<style>
@tailwind base;

@tailwind components;

@layer hover {
  .hover\:bg-red-500:hover {
    background-color: #ff5630;
  }
}

@tailwind utilities;
</style>

<button class="hover:bg-red-500">Hover me</button>

In this example, the @layer directive is used to define a layer called hover, which contains a single utility class that changes the background color of an element to red when hovered over. The hover layer is then referenced in the @tailwind utilities directive, making the utility class available to use in the HTML. By using the @layer directive, you can ensure that your styles are organized, reusable, and easily maintainable.

Conclusion

Directives in Tailwind CSS provide a simple and flexible way to conditionally apply styles to your designs. Whether you are working with reusable styles, responsive designs, or any other complex styling requirements, Tailwind CSS has you covered. With a little bit of knowledge and some practice, you can start using these directives to create dynamic and highly-customizable designs in no time.

Additional resources
  • Frontend web development courses

    Beginner-friendly courses focusing on HTML, CSS, and JavaScript.

    View Courses
  • Frontend web development projects

    Beginner-friendly projects focusing on HTML, CSS, and JavaScript.

    View Projects
  • Free website templates

    Collection of free, high-quality website templates for your next project.

    View Templates