Tailwind CSS Tips
Tailwind CSS has revolutionized how we style web applications. Here are five patterns that I use in every project to write cleaner, more maintainable styles.
First, embrace the component abstraction. Instead of repeating utility classes, extract common patterns into React components. This keeps your JSX clean while maintaining the benefits of utility-first CSS.
Second, use the @apply directive sparingly. While it can be useful for base styles, overusing it defeats the purpose of utility classes. Reserve it for truly global patterns like prose styling.
Third, leverage the arbitrary value syntax for one-off values. Writing w-[347px] is better than adding a custom value to your config for something used only once.
Fourth, organize your utilities consistently. I follow the pattern of layout, spacing, sizing, typography, colors, and effects. This makes scanning classes much easier.
Fifth, use CSS variables for dynamic values. Tailwind integrates beautifully with custom properties, allowing you to create dynamic themes and responsive designs.