Introduction
Tailwind CSS has become the go-to CSS framework for modern web development. But with great power comes great responsibility. Let's explore best practices to keep your Tailwind code clean and maintainable.
1. Use Component Patterns
Instead of repeating classes, extract components:
jsx
Copy
1// ❌ Bad: Repeating classes everywhere 2<button className="px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg font-medium transition-colors"> 3 Save 4</button> 5<button className="px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg font-medium transition-colors"> 6 Submit 7</button> 8 9// ✅ Good: Extract a component10function Button({ children, ...props }) {11 return (12 <button 13 className="px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg font-medium transition-colors"14 {...props}15 >16 {children}17 </button>18 );19}
2. Leverage @apply Sparingly
Use @apply only for highly reused patterns:
css
Copy
1/ globals.css / 2@layer components { 3 .btn-primary { 4 @apply px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg font-medium transition-colors; 5 } 6 7 .card { 8 @apply bg-white rounded-xl shadow-lg p-6 border border-gray-100; 9 }10}
3. Custom Theme Configuration
Extend Tailwind with your design tokens:
javascript
Copy
1// tailwind.config.js 2module.exports = { 3 theme: { 4 extend: { 5 colors: { 6 brand: { 7 50: '#f0fdf4', 8 500: '#22c55e', 9 900: '#14532d',10 },11 },12 fontFamily: {13 sans: ['Inter', 'sans-serif'],14 },15 animation: {16 'fade-in': 'fadeIn 0.3s ease-in-out',17 },18 keyframes: {19 fadeIn: {20 '0%': { opacity: '0' },21 '100%': { opacity: '1' },22 },23 },24 },25 },26};
4. Responsive Design Patterns
Use mobile-first approach:
html
Copy
1<!-- Mobile first: single column, then expand --> 2<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> 3 <Card /> 4 <Card /> 5 <Card /> 6</div>
5. Dark Mode
Implement dark mode with CSS variables:
css
Copy
1@layer base { 2 :root { 3 --background: 255 255 255; 4 --foreground: 0 0 0; 5 } 6 7 .dark { 8 --background: 10 10 10; 9 --foreground: 255 255 255;10 }11}
javascript
Copy
1// tailwind.config.js 2module.exports = { 3 darkMode: 'class', 4 theme: { 5 extend: { 6 colors: { 7 background: 'rgb(var(--background))', 8 foreground: 'rgb(var(--foreground))', 9 },10 },11 },12};
6. Performance Tips
- Use PurgeCSS (built into Tailwind)
- Avoid dynamic class names
- Use the JIT engine (default in v3+)
jsx
// ✅ Good: Use complete class names
const colorClasses = {
red: 'text-red-500',
blue: 'text-blue-500',
};
<div className={colorClasses[color]}>">
Copy
1// ❌ Bad: Dynamic classes won't be purged 2<div className={text-${color}-500}> 3 4// ✅ Good: Use complete class names 5const colorClasses = { 6 red: 'text-red-500', 7 blue: 'text-blue-500', 8}; 9<div className={colorClasses[color]}>
Conclusion
Following these best practices will help you write cleaner, more maintainable Tailwind CSS code. Remember: Tailwind is a tool – how you use it determines the quality of your codebase.