Sub topic 12
Changing spacing, size, and type at breakpoints
This chapter assumes you know the spacing scale, the padding, margin, and gap utilities, the width and height utilities, and the font size and line height utilities from earlier chapters. It also assumes a working Tailwind project. Here you will make any of those utilities change with the viewport width.
Mobile first: unprefixed means "everywhere"
A breakpoint is a viewport width at which the layout changes. Tailwind expresses breakpoints as variants, which are prefixes you put in front of a utility. md:p-8 means "apply p-8 when the viewport is at least the md width."
Tailwind is mobile first. A utility with no prefix applies at every width. A prefixed utility applies at that width and above. Prefixes are min-width media queries, so they never turn off at larger sizes.
The default breakpoints are:
| Prefix | Minimum width | Generated media query |
|---|---|---|
| `sm:` | 640px (40rem) | `@media (min-width: 40rem)` |
| `md:` | 768px (48rem) | `@media (min-width: 48rem)` |
| `lg:` | 1024px (64rem) | `@media (min-width: 64rem)` |
| `xl:` | 1280px (80rem) | `@media (min-width: 80rem)` |
| `2xl:` | 1536px (96rem) | `@media (min-width: 96rem)` |
There is no xs: or mobile: prefix. The base styles are the mobile styles.
<section class="p-4 md:p-8 lg:p-12">
<p class="text-lg md:text-xl">Padding and text grow at larger breakpoints.</p>
</section>The padding is 1rem below 768px, 2rem from 768px, and 3rem from 1024px. At 900px the md: rule is the last one that matches, so the padding is 2rem.
The most common mistake
Many people write sm: when they mean "on small screens." sm: means "from 640px and up," not "only on phones."
<!-- Wrong: the padding is 2rem on every screen of 640px and wider -->
<div class="sm:p-8 p-4">...</div>
<!-- Right way to read it: base first, then overrides as the screen grows -->
<div class="p-4 sm:p-8">...</div>Write the smallest layout with no prefix, then add prefixes only where the design must change. Writing classes in ascending order of breakpoint makes the list easier to read, but the order in the class attribute does not affect the result. Tailwind emits its CSS so that larger breakpoints come later and win.
Responsive spacing
Any padding, margin, or gap utility can take a prefix. A typical pattern is tight spacing on small screens and looser spacing on large ones.
<div class="mx-auto mt-6 px-4 py-6 md:mt-10 md:px-8 md:py-10 lg:px-12">
<ul class="flex flex-col gap-2 md:gap-4">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>In this example:
px-4becomesmd:px-8and thenlg:px-12. Each prefix overrides only the property it targets.md:py-10changes vertical padding without touching the horizontal padding.gap-2becomesmd:gap-4. The same prefix works onspace-y-*utilities.
You only need to repeat a utility at a breakpoint when its value changes. If py-6 is correct at every width, do not write md:py-6.
Responsive sizing
Width and height utilities accept prefixes too. A common use is a full-width element on phones that takes a fraction of the parent on wider screens.
<div class="flex flex-col gap-4 md:flex-row md:gap-8">
<aside class="w-full md:w-1/3">Sidebar</aside>
<main class="w-full md:w-2/3">Main content</main>
</div>Below 768px the container is a column and each child is full width. From 768px the container becomes a row, the gap doubles, and the children take one third and two thirds of the width.
flex-col and md:flex-row are layout utilities, not sizing utilities, but they usually change at the same breakpoint as the widths. When w-1/3 and w-2/3 sit in a flex-col container, they would constrain widths without any benefit. That is why the base w-full matters.
Min and max utilities also take prefixes:
<img class="size-16 md:size-24 lg:size-32" src="avatar.png" alt="Avatar">
<div class="max-w-prose md:max-w-none">Capped on small screens, uncapped from md up</div>size-* requires Tailwind v3.4 or later.
Responsive type
Typography utilities respond in the same way. Font size is the most common change, but weight, line height, and letter spacing can change too.
<h1 class="text-2xl font-bold leading-tight tracking-tight md:text-4xl lg:text-5xl">
Responsive heading
</h1>
<p class="text-base/6 text-gray-600 md:text-lg/8">
Body copy with a larger size and a looser line height on wider screens.
</p>A font size utility such as text-4xl also sets a default line height. When you change the size at a breakpoint, check whether the line height still fits. Two options keep them aligned:
- Use the slash syntax, such as
md:text-lg/8, which sets size and line height in one class. This needs Tailwind v3.3 or later. - Add a separate
leading-*utility. An explicitleading-tightin the base list stays in effect whenmd:text-4xlchanges only the size, so the heading keeps its tight line height. That is usually what you want for headings, but check that it also looks right at the largest size.
If a prefixed size utility sets a line height and you do not want it to, add a prefixed leading utility with it, such as md:text-4xl md:leading-snug.
Targeting ranges and maximums
Sometimes a change should apply only below a breakpoint, or only between two of them. Tailwind provides max-* variants for this. They use the same breakpoint names and generate max-width queries.
<div class="max-md:p-2 md:max-lg:p-6 lg:p-10">
Small padding below md, medium padding from md up to (not including) lg, large from lg.
</div>Here max-md: applies below 768px, and md:max-lg: applies from 768px up to just below 1024px. These variants are available in Tailwind v3.2 and later.
Use them sparingly. A design written mobile first with only min-width prefixes is easier to follow, because every class list reads as a sequence of overrides in one direction. Reach for max-* when a rule really should switch off at a larger size, such as hiding a menu button.
Variants stack
A responsive prefix can combine with other variants such as hover: and focus:. Chain them with colons. The responsive prefix normally comes first.
<a href="#" class="px-3 py-1 text-sm hover:bg-gray-100 md:px-5 md:py-2 md:text-base md:hover:bg-gray-200">
Link
</a>md:hover:bg-gray-200 applies the hover background only from 768px up.
Custom breakpoints and detection
If the five defaults do not fit, you can change them in your theme. In Tailwind v3, add a screens key under theme.extend. In Tailwind v4, define a --breakpoint-* variable in @theme.
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
screens: {
'3xl': '1920px',
},
},
},
}/* app.css (Tailwind v4) */
@import "tailwindcss";
@theme {
--breakpoint-3xl: 120rem;
}Both examples make a 3xl: prefix available.
Tailwind finds classes by scanning your source files for complete class names. A responsive class built from string pieces is not found and produces no CSS.
<!-- Not detected: the full class name never appears in the source -->
<div class="md:p-${size}">...</div>
<!-- Detected: complete class names are written out -->
<div class="md:p-4">...</div>If a responsive class has no effect, check that the full name, including its prefix, appears in a scanned file.
Debugging checklist
When a responsive class does not do what you expect:
- Confirm the viewport is at least as wide as the breakpoint. Browser developer tools show the current width.
- Check that you did not use
sm:to mean "mobile." It means 640px and up. - Check that the class name is written out in full in a scanned file.
- Look for a conflicting utility with a larger breakpoint.
lg:p-12overridesmd:p-8at 1024px and above. - Confirm that your Tailwind version supports the utility.
size-*needs v3.4 andtext-base/7needs v3.3.
Exercise
A card has the classes p-4 sm:p-6 lg:p-10 text-base md:text-lg. What padding and font size does it use at a viewport width of 700px, and at 1100px?
Show answer
At 700px the sm: rule matches (640px and up) but lg: does not, so padding is p-6 (1.5rem). md: (768px) does not match, so the font size stays text-base. At 1100px the lg: and md: rules both match, so padding is p-10 (2.5rem) and the font size is text-lg. Each prefixed class overrides only the property it sets, and the largest matching breakpoint wins.
Exercise
Rewrite this list so that it is mobile first and the heading is 1.5rem on small screens, 2.25rem from 768px up, and has a tight line height throughout: <h1 class="md:text-4xl text-2xl">.
Show answer
<h1 class="text-2xl leading-tight md:text-4xl">. The unprefixed text-2xl is the small-screen size (1.5rem), md:text-4xl takes over from 768px, and leading-tight is unprefixed, so it applies at every width. text-4xl sets 2.5rem rather than 2.25rem, so use md:text-[2.25rem] or a custom theme value if the exact 2.25rem is required.