Web Development
1 min read
Tailwind CSS Tips for Cleaner Code
Practical tips for organizing your Tailwind classes and building maintainable component styles.
Tailwind CSS is incredibly powerful, but without organization, your HTML can become an unreadable mess. Here's how I keep things clean.
1. Consistent Class Ordering
I follow a consistent order for classes:
- Layout (flex, grid, position)
- Sizing (w-, h-, max-w-)
- Spacing (p-, m-, gap-)
- Typography (text-, font-)
- Colors (bg-, text-, border-)
- Effects (shadow-, opacity-)
- Transitions (transition-, duration-)
2. Extract Components
If you're copying the same set of classes more than twice, it's time to extract a component.
3. Use @apply Sparingly
The @apply directive lets you compose utility classes in CSS. Use it for base styles that rarely change, but don't overuse it.
4. Leverage the Config
Extend your tailwind.config.js to add project-specific values for colors, fonts, and spacing.
5. Use Prettier Plugin
The Tailwind Prettier plugin automatically sorts your classes. Install it and never think about class order again.
Share this article
HW
Brooklyn Web Studio
Modern & fast websites