DEV Community

Cover image for Tailwind tip: Managing a long list of utility classes in a single line of code
Kaleem Elahi
Kaleem Elahi

Posted on

Tailwind tip: Managing a long list of utility classes in a single line of code

Managing a long list of utility classes in a single line can become unwieldy.

Here is Tailwind's built-in feature @apply that can help make your code more readable and manageable:

Use @apply in Custom CSS

With Tailwind's @apply directive, you can create reusable CSS classes by grouping multiple Tailwind utilities. This helps reduce the number of classes in your HTML and keeps your components more organized.

Example:

/* styles.css */
.btn-primary {
  @apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}
Enter fullscreen mode Exit fullscreen mode

You can then use this new .btn-primary class in your HTML instead of repeating the Tailwind utilities:

<button class="btn-primary">Primary Button</button>
Enter fullscreen mode Exit fullscreen mode

If it helped you, you can support:
https://buymeacoffee.com/kaleemelahi


Follow for more:
Linkedin
GIthub

Top comments (0)