Tailwind CSS: Build Beautiful UIs Faster
Tailwind CSS is a utility-first CSS framework that lets you build custom designs without leaving your HTML. Instead of writing custom CSS, you compose designs using pre-built utility classes.
Why Tailwind?
Traditional CSS: ```html
Hello
```
With Tailwind: ```html
Hello
Benefits:
- No context switching between HTML and CSS
- No naming things (no more “card-wrapper-container”)
- Responsive design built-in
- Tiny production builds (unused CSS purged)
- Consistent design system
Getting Started
Install Tailwind:
```bash npm install -D tailwindcss postcss autoprefixer npx tailwindcss init ```
Configure your template paths:
```javascript // tailwind.config.js export default { content: [’./src/**/*.{html,js,jsx,ts,tsx}’], theme: { extend: {}, }, plugins: [], } ```
Add Tailwind directives:
```css /* styles.css */ @tailwind base; @tailwind components; @tailwind utilities; ```
Core Concepts
Spacing
Consistent spacing using a 4px scale:
```html
Colors
Comprehensive color palette:
```html
Red text
Dark gray text
Typography
```html
Extra small
Small
Base (16px)
Large
Extra large
2XL
Light
Normal
Medium
Bold
Italic
Underlined
Strikethrough
Left
Center
Right
\`\`\`Layout
Flexbox
```html
Grid
```html
Responsive Design
Mobile-first breakpoints:
```html
Building Components
Button
```html ```
Card
```html
Card Title
Card description goes here.
Navigation
```html
\`\`\`Form
```html
\`\`\`Advanced Features
Dark Mode
```html
Animations
```html
Custom Utilities
```javascript // tailwind.config.js export default { theme: { extend: { colors: { brand: { light: ‘#3fbaeb’, DEFAULT: ‘#0fa9e6’, dark: ‘#0c87b8’, }, }, spacing: { ‘128’: ‘32rem’, }, }, }, } ```
Extracting Components
For repeated patterns, create components:
React: ```jsx function Button({ children, variant = ‘primary’ }) { const baseClasses = ‘px-4 py-2 rounded font-medium transition’; const variants = { primary: ‘bg-blue-500 text-white hover:bg-blue-600’, secondary: ‘bg-gray-200 text-gray-800 hover:bg-gray-300’, };
return ( <button className={`${baseClasses} ${variants[variant]}`}> {children} ); } ```
Best Practices
- Use @apply sparingly - Prefer utility classes in HTML
- Create components for repeated patterns
- Use the config file for custom values
- Enable purging in production for small file sizes
- Use responsive prefixes for mobile-first design
- Leverage hover/focus states for interactivity
Common Patterns
Centering
```html
Aspect Ratios
```html
Truncate Text
```html
This text will truncate with ellipsis...
This text truncates after 3 lines...
\`\`\`Resources
- Official Documentation
- Tailwind UI - Premium components
- Headless UI - Unstyled components
- Tailwind Play - Online playground
Tailwind CSS makes styling fast and consistent. Start using it in your next project!