Mastering Tailwind CSS: From Basics to Advanced Patterns
3 min read
Mastering Tailwind CSS
Tailwind CSS has revolutionized how we write CSS. Let's dive into advanced patterns and best practices.
Core Concepts
Utility-First Approach
Tailwind promotes a utility-first workflow:
<div class="flex items-center space-x-4 p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow">
<img class="w-12 h-12 rounded-full" src="/avatar.jpg" alt="User avatar" />
<div>
<h3 class="text-lg font-semibold text-gray-900">John Doe</h3>
<p class="text-gray-600">Software Engineer</p>
</div>
</div>
Custom Configuration
Extend Tailwind's default theme:
// tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
colors: {
brand: {
50: '#f0f9ff',
100: '#e0f2fe',
// ... other shades
900: '#0c4a6e',
},
},
fontFamily: {
sans: ['Inter var', ...defaultTheme.fontFamily.sans],
},
},
},
}
Advanced Patterns
Component Classes
Using @apply for component classes:
@layer components {
.btn {
@apply px-4 py-2 rounded-lg font-medium transition-colors;
}
.btn-primary {
@apply bg-blue-500 text-white hover:bg-blue-600;
}
.btn-outline {
@apply border-2 border-gray-200 hover:bg-gray-50;
}
}
Dynamic Classes
Using template literals with Tailwind:
function Alert({ type = 'info' }: { type?: 'info' | 'success' | 'error' }) {
const styles = {
info: 'bg-blue-50 text-blue-800 border-blue-200',
success: 'bg-green-50 text-green-800 border-green-200',
error: 'bg-red-50 text-red-800 border-red-200',
}
return (
<div className={`p-4 rounded-lg border ${styles[type]}`}>
Alert content
</div>
)
}
Responsive Design
Mobile-First Approach
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div class="p-6 bg-white rounded-lg shadow">
<h3 class="text-lg md:text-xl font-bold">Card Title</h3>
<p class="mt-2 text-gray-600">Card content goes here...</p>
</div>
<!-- More cards... -->
</div>
Custom Breakpoints
// tailwind.config.js
module.exports = {
theme: {
screens: {
'tablet': '640px',
'laptop': '1024px',
'desktop': '1280px',
},
},
}
Dark Mode
Using Dark Mode Classes
<div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100">
<h1 class="text-2xl font-bold">
This text adapts to dark mode
</h1>
<p class="text-gray-600 dark:text-gray-300">
This paragraph also adapts to dark mode
</p>
</div>
Custom Dark Mode Colors
// tailwind.config.js
module.exports = {
darkMode: 'class',
theme: {
extend: {
colors: {
dark: {
bg: '#121212',
surface: '#1E1E1E',
primary: '#BB86FC',
},
},
},
},
}
Performance Optimization
Purging Unused Styles
// tailwind.config.js
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
theme: {
// ...
},
}
Just-in-Time Mode
The new JIT mode provides:
- Faster build times
- Smaller CSS bundles
- All variants available by default
- Arbitrary value support
Best Practices
-
Organize Components
- Create reusable component patterns
- Use consistent naming conventions
- Document component usage
-
Maintain Consistency
- Use design tokens
- Create a component library
- Follow team conventions
-
Optimize for Production
- Enable JIT mode
- Configure content paths
- Remove unused styles
Conclusion
Tailwind CSS provides a powerful toolkit for building modern interfaces. By following these patterns and best practices, you can create maintainable, performant, and beautiful applications.