
Responsive Design With Tailwind in TailwindCSS
? Responsive Design with Tailwind CSS
Tailwind CSS makes responsive design simple and consistent using mobile-first breakpoints and responsive utility prefixes.
? How It Works
Tailwind uses prefixes like sm:
, md:
, lg:
, xl:
, and 2xl:
to apply styles at specific breakpoints:
<div class="text-sm md:text-base lg:text-xl"> Responsive Text</div>
This means:
text-sm
applies by default (mobile)md:text-base
applies from768px
and uplg:text-xl
applies from1024px
and up
? Default Breakpoints
Prefix | Min Width | Device Example |
---|---|---|
sm: | 640px | Small tablets |
md: | 768px | Tablets |
lg: | 1024px | Laptops |
xl: | 1280px | Desktops |
2xl: | 1536px | Large monitors |
These can be customized in tailwind.config.js
.
? Responsive Examples
? Responsive Layout
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4"> <div class="bg-blue-100 p-4">Box 1</div> <div class="bg-blue-100 p-4">Box 2</div> <div class="bg-blue-100 p-4">Box 3</div> <div class="bg-blue-100 p-4">Box 4</div></div>
?? This becomes 1 column on mobile, 2 on tablet, 4 on desktop.
? Responsive Spacing
<div class="p-2 sm:p-4 lg:p-8"> Responsive Padding</div>
? Responsive Visibility
<div class="block md:hidden">Show on mobile only</div><div class="hidden md:block">Show on tablet and above</div>
? Responsive Images
<img class="w-full md:w-1/2 lg:w-1/3" src="image.jpg" />
? Best Practices
? Start with mobile-first base styles.
? Add responsive styles using prefixes (
md:
,lg:
) as needed.? Use
container
class withmx-auto
for centered layouts.? Use
flex
orgrid
for responsive structures.
? Example: Responsive Card Layout
<div class="p-4 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6"> <div class="bg-white shadow p-6 rounded">Card 1</div> <div class="bg-white shadow p-6 rounded">Card 2</div> <div class="bg-white shadow p-6 rounded">Card 3</div></div>
Would you like a full responsive landing page template using Tailwind?