Tailwind CSS Best Practices

Tailwind's utility-first approach can be very handy, but getting the most out of it requires adopting a few important habits. Incorporating the following best practices will help you write cleaner, more maintainable code and avoid the most common pitfalls.

1. Don't Abstract Too Early

One of the most common mistakes Tailwind newcomers make is reaching for @apply or component classes too early, essentially recreating the problems that Tailwind is designed to solve. The recommended workflow is:

  1. Start with utilities directly in your HTML. Don't worry about repeated class strings at first.
  2. Wait until a pattern is clearly repeated across multiple templates or components before extracting it.
  3. Extract into reusable components using your framework's component system (React, Vue, Blade, etc.), not @apply.

Abstraction via @apply is best reserved for situations where a component system is not available (for example, styling Markdown-generated HTML or a third-party widget). When you do use it, keep the class names semantic:

Avoid using @apply just to shorten repeated class lists in your own templates.

2. Use Your Design Tokens Consistently

Tailwind's power comes from its design constraints. The moment you start mixing arbitrary values (w-[247px], text-[13.5px]) throughout your codebase, you lose the consistency benefits. Follow these guidelines:

3. Build Mobile-First

Tailwind's responsive prefixes (sm:, md:, lg:, xl:, 2xl:) apply styles at a minimum width. This means the unprefixed utilities always apply to the smallest screen first, and overrides cascade upward.

Building mobile-first means:

4. Never Forget Accessibility

Utility-first CSS makes it easy to focus purely on visual output and forget about accessibility. Here are the most important things to keep in mind:

5. Optimise for Performance

Tailwind generates your CSS at build time by scanning your source files for class names. The generated stylesheet only includes the classes you actually use, so the output is minimal by default. However, there are a few things to keep in mind:

6. Organise Your Class Lists

Long class strings on HTML elements can become difficult to read. A few conventions help:

In the final page, we'll wrap up the tutorial with a summary and point you towards further resources.