Sub topic 1
Responsive Design with Tailwind CSS Breakpoints
Prerequisites
Assume you know basic HTML and CSS, including flexbox, grid, and media queries. You should also recognize Tailwind utilities such as flex, p-4, and text-lg.
Setting up the viewport
Responsive design lets a page adapt to different screen sizes. Before Tailwind's breakpoints can work correctly on mobile browsers, add the viewport meta tag to the document head:
<meta name="viewport" content="width=device-width, initial-scale=1">This tells the browser to use the device's actual CSS width instead of pretending that a phone has a much wider desktop viewport. Without it, breakpoint behavior can appear incorrect on phones.
Thinking mobile-first
Tailwind uses a mobile-first model. An unprefixed utility applies at every width unless another utility overrides it. A prefixed utility applies at its breakpoint and wider.
For example, this grid starts with one column, changes to two columns at sm, and changes to three at lg:
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
<div class="rounded border p-4">One</div>
<div class="rounded border p-4">Two</div>
<div class="rounded border p-4">Three</div>
</div>This approach keeps the smallest layout as the base. You then add changes for larger screens. Therefore, sm: means “at least small,” not “small screens only.”
The default breakpoints
Tailwind's default minimum-width breakpoints are:
| Prefix | Minimum width |
|---|---|
| `sm` | 640px |
| `md` | 768px |
| `lg` | 1024px |
| `xl` | 1280px |
| `2xl` | 1536px |
A class such as md:flex-row overrides flex-col from 768px upward. Use the smallest number of breakpoints that expresses the layout clearly. Do not choose a prefix only because its name sounds like the target device.
Exercise
What does sm:grid-cols-2 mean, and what should you use for one column on smaller screens?
Show answer
It means two columns at 640px and wider. Use an unprefixed grid-cols-1 for smaller screens.