Tailwind CSS Skill progressive_disclosure: entry_point: - summary - when_to_use - quick_start sections: core_concepts: - utility_first_approach - responsive_design - configuration advanced: - dark_mode - custom_utilities - plugins - performance_optimization integration: - framework_integration - component_patterns reference: - common_utilities - breakpoints - color_system tokens: entry: 75 full: 4500 Summary
Tailwind CSS is a utility-first CSS framework that provides low-level utility classes to build custom designs without writing CSS. It offers responsive design, dark mode, customization through configuration, and integrates seamlessly with modern frameworks.
When to Use
Best for:
Rapid prototyping with consistent design systems Component-based frameworks (React, Vue, Svelte) Projects requiring responsive and dark mode support Teams wanting to avoid CSS file maintenance Design systems with standardized spacing/colors
Consider alternatives when:
Team unfamiliar with utility-first approach (learning curve) Project requires extensive custom CSS animations Legacy browser support needed (IE11) Minimal CSS footprint required without build process Quick Start Installation
npm
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p
yarn
yarn add -D tailwindcss postcss autoprefixer npx tailwindcss init -p
pnpm
pnpm add -D tailwindcss postcss autoprefixer npx tailwindcss init -p
Configuration
tailwind.config.js:
/ @type {import('tailwindcss').Config} */ module.exports = { content: [ "./src//*.{js,jsx,ts,tsx}", "./public/index.html", ], theme: { extend: {}, }, plugins: [], }
Basic CSS Setup
styles/globals.css:
@tailwind base; @tailwind components; @tailwind utilities;
First Component // Simple button with Tailwind utilities function Button({ children, variant = 'primary' }) { const baseClasses = "px-4 py-2 rounded-lg font-medium transition-colors"; const variants = { primary: "bg-blue-600 text-white hover:bg-blue-700", secondary: "bg-gray-200 text-gray-900 hover:bg-gray-300", danger: "bg-red-600 text-white hover:bg-red-700" };
return ( ); }
Core Concepts Utility-First Approach
Tailwind provides single-purpose utility classes that map directly to CSS properties.
Layout Utilities
Flexbox:
// Centered flex container
// Responsive flex direction
// Flex wrapping and alignment
Grid:
// Basic grid
// Responsive grid
// Auto-fit grid
Spacing System
Padding and Margin:
// Uniform spacing
// Directional spacing
// Negative margins
// Responsive spacing
Space Between:
// Gap between children
// Responsive gap
Typography // Font sizes and weights
Heading
Paragraph
Caption
// Text alignment and decoration
Centered underlined text
Right-aligned strikethrough
// Responsive typography
Responsive heading
// Text overflow handling
This text will be truncated with ellipsis...
This text will be clamped to 3 lines with ellipsis...
Colors // Background colors
// Text colors
Red text
Adaptive text
// Border colors
// Opacity modifiers
Responsive Design
Tailwind uses mobile-first breakpoint system.
Breakpoints // Default breakpoints (tailwind.config.js) { theme: { screens: { 'sm': '640px', // Small devices 'md': '768px', // Medium devices 'lg': '1024px', // Large devices 'xl': '1280px', // Extra large '2xl': '1536px', // 2X extra large } } }
Responsive Patterns // Hide/show at breakpoints
// Responsive layout
// Responsive grid
// Container with responsive padding
Responsive container
Configuration Theme Extension
tailwind.config.js:
/ @type {import('tailwindcss').Config} */ module.exports = { content: ['./src//*.{js,jsx,ts,tsx}'], theme: { extend: { colors: { brand: { 50: '#f0f9ff', 100: '#e0f2fe', 500: '#0ea5e9', 900: '#0c4a6e', }, accent: '#ff6b6b', }, spacing: { '18': '4.5rem', '88': '22rem', '128': '32rem', }, fontFamily: { sans: ['Inter', 'system-ui', 'sans-serif'], display: ['Poppins', 'sans-serif'], mono: ['Fira Code', 'monospace'], }, fontSize: { '2xs': '0.625rem', '3xl': '2rem', }, borderRadius: { '4xl': '2rem', }, boxShadow: { 'inner-lg': 'inset 0 2px 4px 0 rgb(0 0 0 / 0.1)', }, animation: { 'slide-in': 'slideIn 0.3s ease-out', 'fade-in': 'fadeIn 0.2s ease-in', }, keyframes: { slideIn: { '0%': { transform: 'translateX(-100%)' }, '100%': { transform: 'translateX(0)' }, }, fadeIn: { '0%': { opacity: '0' }, '100%': { opacity: '1' }, }, }, }, }, plugins: [], }
Custom Breakpoints module.exports = { theme: { screens: { 'xs': '475px', 'sm': '640px', 'md': '768px', 'lg': '1024px', 'xl': '1280px', '2xl': '1536px', '3xl': '1920px', // Custom breakpoints 'tablet': '640px', 'laptop': '1024px', 'desktop': '1280px', // Max-width breakpoints 'max-md': {'max': '767px'}, }, }, }
Advanced Features Dark Mode Class Strategy (Recommended)
tailwind.config.js:
module.exports = { darkMode: 'class', // or 'media' for OS preference // ... }
Implementation:
// Toggle component function DarkModeToggle() { const [isDark, setIsDark] = useState(false);
useEffect(() => { if (isDark) { document.documentElement.classList.add('dark'); } else { document.documentElement.classList.remove('dark'); } }, [isDark]);
return ( ); }
// Dark mode styles function Card() { return (
Card Title
Card content adapts to dark mode
System Preference Strategy // tailwind.config.js module.exports = { darkMode: 'media', // Uses OS preference }
// Automatically adapts to system preference
Custom Utilities Adding Custom Utilities
tailwind.config.js:
const plugin = require('tailwindcss/plugin');
module.exports = { plugins: [ plugin(function({ addUtilities, addComponents, theme }) { // Custom utilities addUtilities({ '.scrollbar-hide': { '-ms-overflow-style': 'none', 'scrollbar-width': 'none', '&::-webkit-scrollbar': { display: 'none', }, }, '.text-balance': { 'text-wrap': 'balance', }, });
// Custom components
addComponents({
'.btn': {
padding: theme('spacing.4'),
borderRadius: theme('borderRadius.lg'),
fontWeight: theme('fontWeight.medium'),
'&:hover': {
opacity: 0.9,
},
},
'.card': {
backgroundColor: theme('colors.white'),
borderRadius: theme('borderRadius.lg'),
padding: theme('spacing.6'),
boxShadow: theme('boxShadow.lg'),
},
});
}),
], }
Custom Variants const plugin = require('tailwindcss/plugin');
module.exports = { plugins: [ plugin(function({ addVariant }) { // Custom variant for third child addVariant('third', '&:nth-child(3)');
// Custom variant for not-last child
addVariant('not-last', '&:not(:last-child)');
// Custom variant for group-hover with specific element
addVariant('group-hover-show', '.group:hover &');
}),
], }
// Usage
Plugins Official Plugins npm install -D @tailwindcss/forms @tailwindcss/typography @tailwindcss/aspect-ratio
tailwind.config.js:
module.exports = { plugins: [ require('@tailwindcss/forms'), require('@tailwindcss/typography'), require('@tailwindcss/aspect-ratio'), ], }
Forms Plugin // Automatic form styling
Typography Plugin // Beautiful prose styling
Article Title
Automatic typography styles for markdown content...
- Styled lists
- Proper spacing
Aspect Ratio Plugin // Maintain aspect ratios
Performance Optimization Content Configuration // Optimize purge paths module.exports = { content: [ './src//*.{js,jsx,ts,tsx}', './public/index.html', // Include component libraries './node_modules/@my-ui//*.js', ], // Safelist dynamic classes safelist: [ 'bg-red-500', 'bg-green-500', { pattern: /bg-(red|green|blue)-(400|500|600)/, variants: ['hover', 'focus'], }, ], }
JIT Mode (Default in v3+)
Just-In-Time compilation generates styles on-demand:
// Arbitrary values work instantly
// No rebuild needed for new utilities
Build Optimization // postcss.config.js module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, // Production minification ...(process.env.NODE_ENV === 'production' ? { cssnano: {} } : {}), }, }
Framework Integration React / Next.js
Installation:
npx create-next-app@latest my-app --tailwind
or add to existing project
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p
next.config.js:
/* @type {import('next').NextConfig} / module.exports = { // Tailwind works out of box with Next.js }
_app.tsx:
import '@/styles/globals.css' import type { AppProps } from 'next/app'
export default function App({ Component, pageProps }: AppProps) {
return
Component Example:
// components/Button.tsx import { ButtonHTMLAttributes, forwardRef } from 'react' import { cva, type VariantProps } from 'class-variance-authority'
const buttonVariants = cva( "inline-flex items-center justify-center rounded-lg font-medium transition-colors", { variants: { variant: { default: "bg-blue-600 text-white hover:bg-blue-700", outline: "border border-gray-300 hover:bg-gray-100", ghost: "hover:bg-gray-100", }, size: { sm: "px-3 py-1.5 text-sm", md: "px-4 py-2", lg: "px-6 py-3 text-lg", }, }, defaultVariants: { variant: "default", size: "md", }, } )
interface ButtonProps
extends ButtonHTMLAttributes
const Button = forwardRef
export default Button
SvelteKit
Installation:
npx sv create my-app
Select Tailwind CSS option
or add to existing
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p
svelte.config.js:
import adapter from '@sveltejs/adapter-auto'; import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
export default { preprocess: vitePreprocess(), kit: { adapter: adapter() } };
Component Example:
<button class="rounded-lg font-medium transition-colors {variants[variant]} {sizes[size]}" on:click
Vue 3
Installation:
npm create vue@latest my-app
Select Tailwind CSS
or add to existing
npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p
main.ts:
import { createApp } from 'vue' import App from './App.vue' import './assets/main.css'
createApp(App).mount('#app')
Component Example:
Component Patterns Layout Components Container function Container({ children, size = 'default' }) { const sizes = { sm: 'max-w-3xl', default: 'max-w-7xl', full: 'max-w-full' };
return (
Grid Layout function GridLayout({ children, cols = { default: 1, md: 2, lg: 3 } }) { return (
Stack (Vertical Spacing) function Stack({ children, spacing = 4 }) { return (
UI Components Card function Card({ title, description, image, footer }) { return (
{title}
{description}
Modal function Modal({ isOpen, onClose, title, children }) { if (!isOpen) return null;
return (
{/* Modal */}
<div className="flex min-h-full items-center justify-center p-4">
<div className="
relative bg-white dark:bg-gray-800
rounded-lg shadow-xl
max-w-md w-full
p-6
animate-fade-in
">
<h2 className="text-2xl font-bold mb-4">
{title}
</h2>
<button
onClick={onClose}
className="absolute top-4 right-4 text-gray-400 hover:text-gray-600"
>
✕
</button>
{children}
</div>
</div>
</div>
); }
Form Input function Input({ label, error, ...props }) { return (
{error}
)}Badge function Badge({ children, variant = 'default', size = 'md' }) { const variants = { default: 'bg-gray-100 text-gray-800', primary: 'bg-blue-100 text-blue-800', success: 'bg-green-100 text-green-800', warning: 'bg-yellow-100 text-yellow-800', danger: 'bg-red-100 text-red-800', };
const sizes = { sm: 'text-xs px-2 py-0.5', md: 'text-sm px-2.5 py-1', lg: 'text-base px-3 py-1.5', };
return ( inline-flex items-center rounded-full font-medium ${variants[variant]} ${sizes[size]}}> {children} ); }
Reference Common Utility Classes Display & Positioning display: block, inline-block, inline, flex, inline-flex, grid, inline-grid, hidden position: static, fixed, absolute, relative, sticky top/right/bottom/left: 0, auto, 1-96 (in 0.25rem increments) z-index: z-0, z-10, z-20, z-30, z-40, z-50, z-auto
Flexbox & Grid flex-direction: flex-row, flex-col, flex-row-reverse, flex-col-reverse justify-content: justify-start, justify-center, justify-end, justify-between, justify-around align-items: items-start, items-center, items-end, items-baseline, items-stretch gap: gap-0 to gap-96 (in 0.25rem increments) grid-cols: grid-cols-1 to grid-cols-12, grid-cols-none
Sizing width: w-0 to w-96, w-auto, w-full, w-screen, w-1/2, w-1/3, w-2/3, etc. height: h-0 to h-96, h-auto, h-full, h-screen min/max-width: min-w-0, min-w-full, max-w-xs to max-w-7xl min/max-height: min-h-0, min-h-full, max-h-screen
Spacing (0.25rem = 4px increments) padding: p-0 to p-96, px-, py-, pt-, pr-, pb-, pl- margin: m-0 to m-96, mx-, my-, mt-, mr-, mb-, ml-, -m- space-between: space-x-, space-y-*
Typography font-size: text-xs, text-sm, text-base, text-lg, text-xl to text-9xl font-weight: font-thin to font-black (100-900) text-align: text-left, text-center, text-right, text-justify line-height: leading-none to leading-loose letter-spacing: tracking-tighter to tracking-widest
Colors (50-900 scale) text-{color}-{shade}: text-gray-500, text-blue-600, text-red-700 bg-{color}-{shade}: bg-white, bg-gray-100, bg-blue-500 border-{color}-{shade}: border-gray-300, border-blue-500 Colors: slate, gray, zinc, neutral, stone, red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose
Borders border-width: border, border-0, border-2, border-4, border-8 border-style: border-solid, border-dashed, border-dotted, border-double, border-none border-radius: rounded-none, rounded-sm, rounded, rounded-lg, rounded-full border-color: border-gray-300, etc.
Effects shadow: shadow-sm, shadow, shadow-md, shadow-lg, shadow-xl, shadow-2xl, shadow-none opacity: opacity-0 to opacity-100 (in 5% increments) blur: blur-none, blur-sm, blur, blur-lg, blur-xl
Transitions & Animations transition: transition-none, transition-all, transition, transition-colors, transition-opacity duration: duration-75 to duration-1000 (ms) timing: ease-linear, ease-in, ease-out, ease-in-out animation: animate-none, animate-spin, animate-ping, animate-pulse, animate-bounce
Responsive Breakpoints sm: 640px @media (min-width: 640px) md: 768px @media (min-width: 768px) lg: 1024px @media (min-width: 1024px) xl: 1280px @media (min-width: 1280px) 2xl: 1536px @media (min-width: 1536px)
State Variants hover: &:hover focus: &:focus active: &:active disabled: &:disabled visited: &:visited (links) checked: &:checked (inputs) group-hover: .group:hover & peer-focus: .peer:focus ~ & first: &:first-child last: &:last-child odd: &:nth-child(odd) even: &:nth-child(even)
Dark Mode dark: .dark & dark:hover: .dark &:hover dark:focus: .dark &:focus
Best Practices Class Organization // Group classes logically
Extracting Components // DON'T repeat complex class strings
// DO extract to component function Button({ children }) { return ( ); }
Using CSS Variables
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
primary: 'rgb(var(--color-primary) /
/ globals.css / :root { --color-primary: 59 130 246; / RGB values for blue-500 / --color-secondary: 139 92 246; / RGB values for purple-500 / }
.dark { --color-primary: 96 165 250; / Lighter blue for dark mode / --color-secondary: 167 139 250; / Lighter purple for dark mode / }
// Usage with opacity
Avoiding Class Conflicts // DON'T use conflicting utilities