Tailwind CSS Fundamentals
Learn the core concepts and utility-first approach of Tailwind CSS for rapid UI development.
What is Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that allows you to build custom designs without writing CSS. Unlike component-based frameworks like Bootstrap, Tailwind provides low-level utility classes that let you build completely custom designs.
Traditional CSS vs. Tailwind CSS
/* Traditional CSS */
.btn {
display: inline-block;
padding: 0.5rem 1rem;
font-weight: 600;
text-align: center;
border-radius: 0.25rem;
background-color: #3b82f6;
color: white;
}
.btn:hover {
background-color: #2563eb;
}
<!-- Traditional HTML with CSS classes -->
<button class="btn">Click Me</button>
<!-- Tailwind utility classes -->
<button class="inline-block px-4 py-2 font-semibold text-center rounded bg-blue-500 text-white hover:bg-blue-600">
Click Me
</button>
Core Concepts
Tailwind's utility-first approach is based on several core concepts that make it different from traditional CSS frameworks.
1. Utility-First
Instead of pre-designed components, Tailwind provides utility classes that you combine to create your own designs.
<div class="p-6 max-w-sm mx-auto bg-white rounded-xl shadow-md flex items-center space-x-4">
<div class="flex-shrink-0">
<img class="h-12 w-12" src="/img/logo.svg" alt="Logo">
</div>
<div>
<div class="text-xl font-medium text-black">ChitChat</div>
<p class="text-gray-500">You have a new message!</p>
</div>
</div>
2. Responsive Design
Tailwind includes responsive variants that let you build responsive interfaces easily.
<div class="text-center sm:text-left md:text-right lg:text-justify">
This text changes alignment at different breakpoints
</div>
3. Hover, Focus, and Other States
Tailwind provides state variants for styling elements in different states.
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:ring-2 focus:ring-blue-400">
Hover and Focus States
</button>
4. Component Extraction
Reuse patterns by extracting components using directives or JavaScript frameworks.
// Using @apply directive in CSS
@layer components {
.btn-primary {
@apply py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75;
}
}
Setting Up Tailwind CSS
Getting started with Tailwind CSS in your project.
Installation
// Install Tailwind CSS, PostCSS, and autoprefixer
npm install -D tailwindcss postcss autoprefixer
// Initialize Tailwind CSS
npx tailwindcss init -p
Configuration
// tailwind.config.js
module.exports = {
content: [
"./src/**/*.{html,js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Add Tailwind to your CSS
/* src/styles.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
Build Process
// Add to your package.json scripts
"scripts": {
"build:css": "tailwindcss -i ./src/styles.css -o ./dist/styles.css",
"watch:css": "tailwindcss -i ./src/styles.css -o ./dist/styles.css --watch"
}
<!-- Add to your HTML head -->
<script src="https://cdn.tailwindcss.com"></script>
Utility Classes Reference
Essential Tailwind utility classes for common styling needs.
Layout
container
- Set max-width based on screen sizeflex
,inline-flex
- Display flexgrid
,inline-grid
- Display gridhidden
,block
,inline
,inline-block
- Display propertiesoverflow-auto
,overflow-hidden
,overflow-scroll
- Overflow behaviorrelative
,absolute
,fixed
,sticky
- Position properties
Flexbox & Grid
flex-row
,flex-col
- Flex directionflex-wrap
,flex-nowrap
- Flex wrappingitems-start
,items-center
,items-end
- Align itemsjustify-start
,justify-center
,justify-end
,justify-between
- Justify contentgrid-cols-1
throughgrid-cols-12
- Grid columnsgap-0
throughgap-10
- Gap between grid/flex items
Spacing
p-0
throughp-10
- Padding on all sidespx-0
throughpx-10
- Horizontal paddingpy-0
throughpy-10
- Vertical paddingpt-0
,pr-0
,pb-0
,pl-0
- Individual side paddingm-0
throughm-10
- Margin on all sidesmx-auto
- Horizontal centering
Typography
text-xs
throughtext-9xl
- Font sizefont-thin
throughfont-black
- Font weighttext-left
,text-center
,text-right
- Text alignmenttext-gray-500
,text-red-500
, etc. - Text colorleading-none
throughleading-loose
- Line heighttracking-tight
throughtracking-widest
- Letter spacing
Backgrounds & Borders
bg-white
,bg-black
,bg-gray-100
, etc. - Background colorborder
,border-0
throughborder-8
- Border widthborder-gray-300
, etc. - Border colorrounded
,rounded-sm
throughrounded-full
- Border radiusshadow
,shadow-sm
throughshadow-2xl
- Box shadow
Responsive Design
Building responsive layouts with Tailwind's mobile-first approach.
Breakpoint Prefixes
sm:
- 640px and abovemd:
- 768px and abovelg:
- 1024px and abovexl:
- 1280px and above2xl:
- 1536px and above
Mobile-First Approach
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<!-- Cards will be 1 column on mobile, 2 columns on tablets, 3 columns on desktops -->
<div class="bg-white p-6 rounded-lg shadow-md">Card 1</div>
<div class="bg-white p-6 rounded-lg shadow-md">Card 2</div>
<div class="bg-white p-6 rounded-lg shadow-md">Card 3</div>
</div>
Responsive Navigation Example
<nav class="bg-gray-800 p-4">
<div class="container mx-auto flex justify-between items-center">
<div class="text-white font-bold">Logo</div>
<!-- Mobile menu button -->
<button class="block md:hidden text-white">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
<!-- Desktop menu -->
<div class="hidden md:flex space-x-4">
<a href="#" class="text-white hover:text-gray-300">Home</a>
<a href="#" class="text-white hover:text-gray-300">About</a>
<a href="#" class="text-white hover:text-gray-300">Services</a>
<a href="#" class="text-white hover:text-gray-300">Contact</a>
</div>
</div>
</nav>
State Variants
Style elements based on their state using Tailwind's variant modifiers.
Common State Variants
hover:
- Mouse hover statefocus:
- Element has focusactive:
- Element is being activateddisabled:
- Element is disabledvisited:
- Link has been visitedfirst:
,last:
,odd:
,even:
- Child selectorsgroup-hover:
- When parent with class 'group' is hovereddark:
- Dark mode styles
Interactive Button Example
<button class="
bg-blue-500
hover:bg-blue-700
active:bg-blue-800
focus:outline-none
focus:ring-2
focus:ring-blue-400
focus:ring-opacity-75
disabled:opacity-50
disabled:cursor-not-allowed
text-white
font-bold
py-2
px-4
rounded
">
Interactive Button
</button>
Group Hover Example
<div class="group p-6 max-w-sm mx-auto bg-white rounded-xl shadow-md hover:shadow-lg transition-shadow">
<div class="flex items-center space-x-4">
<div>
<div class="text-xl font-medium text-black">Card Title</div>
<p class="text-gray-500 group-hover:text-blue-500 transition-colors">
This text changes color when the card is hovered
</p>
</div>
</div>
</div>
Dark Mode Support
// tailwind.config.js
module.exports = {
darkMode: 'class', // or 'media' for OS preference
// ...
}
// HTML usage
<div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-white">
This content adapts to dark mode
</div>
Component Patterns
Strategies for building reusable components with Tailwind CSS.
1. Extracting Components with @apply
/* In your CSS file */
@layer components {
.btn {
@apply py-2 px-4 font-semibold rounded-lg shadow-md focus:outline-none focus:ring-2 focus:ring-opacity-75;
}
.btn-primary {
@apply bg-blue-500 text-white hover:bg-blue-700 focus:ring-blue-400;
}
.btn-secondary {
@apply bg-gray-500 text-white hover:bg-gray-700 focus:ring-gray-400;
}
}
/* HTML usage */
<button class="btn btn-primary">Primary Button</button>
<button class="btn btn-secondary">Secondary Button</button>
2. JavaScript Component Abstraction
// React component example
function Button({ variant = 'primary', children, ...props }) {
const baseClasses = 'py-2 px-4 font-semibold rounded-lg shadow-md focus:outline-none focus:ring-2 focus:ring-opacity-75';
const variantClasses = {
primary: 'bg-blue-500 text-white hover:bg-blue-700 focus:ring-blue-400',
secondary: 'bg-gray-500 text-white hover:bg-gray-700 focus:ring-gray-400',
success: 'bg-green-500 text-white hover:bg-green-700 focus:ring-green-400',
};
const classes = `${baseClasses} ${variantClasses[variant]}`;
return (
<button className={classes} {...props}>
{children}
</button>
);
}
// Usage
<Button variant="primary">Primary Button</Button>
<Button variant="secondary">Secondary Button</Button>
3. Template Partials (Server-Side)
<!-- _button.html partial -->
<button class="py-2 px-4 font-semibold rounded-lg shadow-md focus:outline-none focus:ring-2 focus:ring-opacity-75
{% if variant == 'primary' %}
bg-blue-500 text-white hover:bg-blue-700 focus:ring-blue-400
{% elif variant == 'secondary' %}
bg-gray-500 text-white hover:bg-gray-700 focus:ring-gray-400
{% endif %}">
{{ label }}
</button>
<!-- Usage with server-side templating -->
{% include '_button.html' with variant='primary' label='Primary Button' %}
{% include '_button.html' with variant='secondary' label='Secondary Button' %}
Best Practices
Organization
- Follow a consistent class order (layout → typography → visual)
- Extract components for repeated patterns
- Use comments to separate sections in complex components
- Consider using multi-line formatting for readability
- Create a design system with custom theme values
Performance
- Enable PurgeCSS in production to remove unused styles
- Use JIT (Just-In-Time) mode for faster development
- Consider code-splitting for large applications
- Avoid excessive class extraction with @apply
- Use responsive variants judiciously
Maintainability
- Document custom components and utilities
- Create a style guide for your team
- Use consistent naming conventions for custom utilities
- Consider using Tailwind plugins for repeated patterns
- Set up linting rules for Tailwind classes