Tailwind CSS Responsive Design
Learn how to build fully responsive interfaces with Tailwind's mobile-first utility classes.
Mobile-First Approach
Tailwind uses a mobile-first breakpoint system, meaning unprefixed utilities target mobile devices, and prefixed utilities target larger screen sizes.
<!-- This will be blue on mobile and red on medium screens and up -->
<div class="bg-blue-500 md:bg-red-500">
Responsive Background
</div>
<!-- This will be a single column on mobile, 2 columns on small screens, and 3 columns on medium screens -->
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Responsive Background Example:
Resize your browser window to see the changes. The box will be blue on mobile screens (<768px) and red on medium screens and up (≥768px).
Responsive Grid Example:
This grid will have 1 column on mobile, 2 columns on small screens (≥640px), and 3 columns on medium screens (≥768px).
Default Breakpoints
Tailwind includes these default responsive breakpoints:
sm
: 640px and upmd
: 768px and uplg
: 1024px and upxl
: 1280px and up2xl
: 1536px and up
Responsive Layout Patterns
Common responsive layout patterns using Tailwind's responsive utilities.
Responsive Columns
<!-- Responsive grid that changes columns based on screen size -->
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-6 gap-4">
<div class="bg-blue-100 p-4 rounded">1</div>
<div class="bg-blue-100 p-4 rounded">2</div>
<div class="bg-blue-100 p-4 rounded">3</div>
<div class="bg-blue-100 p-4 rounded">4</div>
<div class="bg-blue-100 p-4 rounded">5</div>
<div class="bg-blue-100 p-4 rounded">6</div>
</div>
Responsive Padding and Margin
<!-- Element with different padding at different screen sizes -->
<div class="p-4 md:p-8 lg:p-12 bg-gray-100 rounded">
This box has 1rem padding on mobile, 2rem on tablets, and 3rem on desktops.
</div>
<!-- Element with different margin at different screen sizes -->
<div class="mt-4 md:mt-8 lg:mt-12 bg-gray-100 rounded p-4">
This box has different top margins depending on the screen size.
</div>
Responsive Direction
<!-- Stack vertically on mobile, horizontally on larger screens -->
<div class="flex flex-col md:flex-row gap-4">
<div class="bg-blue-100 p-4 rounded">First item</div>
<div class="bg-blue-100 p-4 rounded">Second item</div>
<div class="bg-blue-100 p-4 rounded">Third item</div>
</div>
Responsive Typography
Adjust font sizes, text alignment, and other text properties based on screen size.
Responsive Font Sizes
<!-- Heading with responsive font size -->
<h1 class="text-2xl sm:text-3xl md:text-4xl lg:text-5xl font-bold">
Responsive Heading
</h1>
<!-- Paragraph with responsive font size -->
<p class="text-sm md:text-base lg:text-lg">
This paragraph has different font sizes depending on the screen width.
</p>
Responsive Text Alignment
<!-- Text centered on mobile, left-aligned on larger screens -->
<p class="text-center md:text-left">
This text is centered on mobile but left-aligned on medium screens and up.
</p>
<!-- Text that changes alignment at different breakpoints -->
<p class="text-left sm:text-center md:text-right lg:text-justify">
This text changes its alignment at different breakpoints.
</p>
Responsive Visibility
Show or hide elements based on screen size.
Conditionally Showing Elements
<!-- Only visible on mobile -->
<div class="block md:hidden">
Only visible on mobile screens
</div>
<!-- Hidden on mobile, visible on medium screens and up -->
<div class="hidden md:block">
Hidden on mobile, visible on medium screens and up
</div>
<!-- Only visible on large screens -->
<div class="hidden lg:block">
Only visible on large screens
</div>
Responsive Mobile Menu Pattern
<!-- Mobile menu button (visible only on mobile) -->
<button class="block md:hidden">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
<!-- Navigation links (hidden on mobile, visible on larger screens) -->
<nav class="hidden md:block">
<ul class="flex space-x-4">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
Responsive Component Examples
Complete examples of responsive components built with Tailwind CSS.
Responsive Card Grid
<!-- Responsive card grid -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
<!-- Card 1 -->
<div class="bg-white rounded-lg shadow-md overflow-hidden">
<img class="w-full h-48 object-cover" src="https://source.unsplash.com/random/800x600/?nature,1" alt="Card image">
<div class="p-6">
<h3 class="font-bold text-xl mb-2">Card Title 1</h3>
<p class="text-gray-700">This is a responsive card that will adjust its layout based on screen size.</p>
<button class="mt-4 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Learn More
</button>
</div>
</div>
<!-- Card 2 -->
<div class="bg-white rounded-lg shadow-md overflow-hidden">
<img class="w-full h-48 object-cover" src="https://source.unsplash.com/random/800x600/?nature,2" alt="Card image">
<div class="p-6">
<h3 class="font-bold text-xl mb-2">Card Title 2</h3>
<p class="text-gray-700">This is a responsive card that will adjust its layout based on screen size.</p>
<button class="mt-4 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Learn More
</button>
</div>
</div>
<!-- Card 3 -->
<div class="bg-white rounded-lg shadow-md overflow-hidden">
<img class="w-full h-48 object-cover" src="https://source.unsplash.com/random/800x600/?nature,3" alt="Card image">
<div class="p-6">
<h3 class="font-bold text-xl mb-2">Card Title 3</h3>
<p class="text-gray-700">This is a responsive card that will adjust its layout based on screen size.</p>
<button class="mt-4 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Learn More
</button>
</div>
</div>
</div>
Responsive Navbar
<!-- Responsive Navbar -->
<nav class="bg-gray-800 text-white">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex items-center justify-between h-16">
<div class="flex items-center">
<div class="flex-shrink-0">
<span class="text-xl font-bold">Logo</span>
</div>
<!-- Desktop menu (hidden on mobile) -->
<div class="hidden md:block">
<div class="ml-10 flex items-baseline space-x-4">
<a href="#" class="bg-gray-900 text-white px-3 py-2 rounded-md text-sm font-medium">Home</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">About</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Services</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Contact</a>
</div>
</div>
</div>
<!-- Mobile menu button (only visible on mobile) -->
<div class="md:hidden">
<button class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-white hover:bg-gray-700 focus:outline-none">
<svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
</svg>
</button>
</div>
</div>
</div>
<!-- Mobile menu (hidden by default, would be shown/hidden with JavaScript) -->
<div class="hidden md:hidden">
<div class="px-2 pt-2 pb-3 space-y-1 sm:px-3">
<a href="#" class="bg-gray-900 text-white block px-3 py-2 rounded-md text-base font-medium">Home</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium">About</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium">Services</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white block px-3 py-2 rounded-md text-base font-medium">Contact</a>
</div>
</div>
</nav>
Custom Breakpoints
Learn how to define your own custom breakpoints in Tailwind CSS.
Customizing Breakpoints in tailwind.config.js
// tailwind.config.js
module.exports = {
theme: {
screens: {
'xs': '480px',
'sm': '640px',
'md': '768px',
'lg': '1024px',
'xl': '1280px',
'2xl': '1536px',
'tablet': '640px',
'laptop': '1024px',
'desktop': '1280px',
}
}
}
With these custom breakpoints, you can use them like this:
<div class="text-sm xs:text-base tablet:text-lg laptop:text-xl desktop:text-2xl">
Text that changes size at custom breakpoints
</div>
Best Practices
Tips for effective responsive design with Tailwind CSS.
- Start with mobile designs first, then add responsive utilities for larger screens
- Use the grid system for complex layouts instead of manually calculating widths
- Leverage
hidden
andblock
utilities to show/hide content based on screen size - Consider using
container
withmx-auto
for centered, responsive content - Test your designs at various screen sizes during development
- Use DevTools to simulate different device sizes
- Don't overuse breakpoints - aim for designs that flow naturally across screen sizes
For more Tailwind CSS topics, check out our Fundamentals, Customization, and UI Components pages.