Sub topic 12
Changing spacing, size, and type at breakpoints
This chapter assumes you know the spacing scale, the padding, margin, width, and font-size utilities, and how to add classes to HTML elements. It also assumes a project with Tailwind CSS installed.
The mobile-first model
Tailwind's responsive variants are prefixes such as md: and lg:. A prefix limits a utility to a minimum screen width and every width above it.
A class with no prefix applies at all widths. A prefixed class overrides it from its breakpoint upward. This is called mobile-first: you write the small-screen style first, then add overrides as the screen grows.
The default breakpoints are:
| Prefix | Minimum width | Media query |
|---|---|---|
| `sm:` | 640px | `@media (min-width: 640px)` |
| `md:` | 768px | `@media (min-width: 768px)` |
| `lg:` | 1024px | `@media (min-width: 1024px)` |
| `xl:` | 1280px | `@media (min-width: 1280px)` |
| `2xl:` | 1536px | `@media (min-width: 1536px)` |
Scaling spacing and type together
The same pattern works for padding, margin, gap, size, and type utilities.
<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>Here is how the browser reads this markup:
- Below 768px, the section has
p-4(1rem) and the text istext-lg. - From 768px up,
md:p-8(2rem) andmd:text-xltake over. - From 1024px up,
lg:p-12(3rem) replaces the padding. The text staystext-xl, because nolg:text class exists and themd:rule still applies.
Sizing and layout at breakpoints
Width, gap, and similar utilities take prefixes in the same way. A common case is a full-width element that becomes a fraction of its parent on larger screens:
<div class="flex flex-col gap-2 md:flex-row md:gap-6">
<div class="w-full md:w-1/3">Sidebar</div>
<div class="w-full md:w-2/3">Main</div>
</div>On small screens the two blocks stack, with a small gap. From md up they sit in a row, the gap grows, and the widths split one third to two thirds.
Common errors
- Using
sm:to mean "on mobile".sm:starts at 640px. To style phones, write the unprefixed class. Use a prefix only for larger screens. - Overriding in the wrong direction. Writing
md:p-8 p-4gives the same result asp-4 md:p-8. Class order in the attribute does not decide which rule wins. The generated CSS order does, and Tailwind emits the breakpoint rules after the base rules. Still, write base classes first so the list is easier to read. - Forgetting that overrides carry upward. A
md:class remains active atlg:andxl:. Add a larger prefix only when the value changes again. - Building class names dynamically. A string like `
md:p-${n}` is not detected by Tailwind's scanner, so the CSS is never generated. Write the full class names in your source.
Range and custom breakpoints
To apply a utility only inside one range, stack a minimum variant with a maximum one. In Tailwind v3.2 and later, max-md: applies below 768px, so md:max-lg:p-6 applies from 768px to 1023px.
You can change breakpoints in your theme. The theme chapter covers where that configuration lives.
Exercise
A card uses class="p-4 text-base lg:p-10 lg:text-lg". What padding and font size does it have on a 900px-wide screen, and why?
Show answer
It has p-4 (1rem) padding and text-base. A 900px screen is wider than md (768px) but narrower than lg (1024px), so the lg: overrides are not active. Only the unprefixed classes apply.
Exercise
Write the classes for a heading that is text-2xl by default, text-4xl from 768px up, and has mb-4 by default but mb-8 from 1024px up.
Show answer
class="mb-4 text-2xl md:text-4xl lg:mb-8"