Responsive Design — Tailwind CSS

A breakpoint prefix (a breakpoint is the screen width where the style changes) means "from this width and up": sm: 640px, md: 768px (48rem), lg: 1024px, xl…

In CSS, a layout that changes on wider screens needs a media query (a CSS rule that applies only at certain screen widths). You write @media, repeat the selector, then write the rule again, often far away from the original rule. In Tailwind, all of that fits in one class.

Responsive design means one page that adapts to the width of the screen: one column on a phone, two columns on a tablet and a laptop. In Tailwind you do it with a breakpoint prefix in front of a class. A breakpoint is the screen width where the style changes. For example, md:grid-cols-2 means "from 768px wide and up, use grid-cols-2". It works like a ride at a theme park with a sign saying "riders from 120 cm". A child who is 120 cm tall may ride, and taller children may ride too. The md: prefix works like that sign, but it measures screen width, and the limit is 768px: a screen 768px wide or wider uses grid-cols-2.

One column on a phone, two columns from tablet up

This is the same layout as in the Lab and the exercise:

<div id="layout" class="grid grid-cols-1 md:grid-cols-2 gap-4">
  <div class="bg-blue-100 p-4">Column 1</div>
  <div class="bg-green-100 p-4">Column 2</div>
</div>

Tailwind turns those classes into CSS like this:

.grid { display: grid; }
.grid-cols-1 { grid-template-columns: repeat(1, minmax(0, 1fr)); }
.gap-4 { gap: 1rem; }

@media (width >= 48rem) {
  .md\:grid-cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

Change the screen width in the Lab and you get this:

Screen width grid-cols-1 md:grid-cols-2 Result
360px (phone) applies off, under 768px 1 column, Column 2 below Column 1
764px applies off 1 column
768px (tablet) overridden on 2 columns
1280px (laptop) overridden on 2 columns

Five breakpoints, all of them from

Prefix From Roughly
(no prefix) 0px every screen, phones included
sm: 40rem = 640px a phone held sideways
md: 48rem = 768px tablet
lg: 64rem = 1024px laptop
xl: 80rem = 1280px desktop
2xl: 96rem = 1536px wide screen

Every prefix means "from this width and up", with no upper limit. So on a 1280px laptop, md:grid-cols-2 is still on, and the layout stays two columns.

Want three columns on a laptop? Add one more prefix: grid-cols-1 md:grid-cols-2 lg:grid-cols-3. At 1280px, md: and lg: are both on, and lg: wins, because Tailwind puts the lg: rules after the md: rules (from the smallest breakpoint to the largest).

Mobile-first: write the phone style with no prefix

A class with no prefix applies on every screen, so it's the style for the smallest screen. The pattern: first write the phone layout with no prefix, then add prefixes for wider screens. This is called mobile-first (starting from the phone).

The reverse often confuses people: grid grid-cols-2 md:grid-cols-1. On a phone you get two cramped columns, and on a laptop one long column. The order of classes in the attribute doesn't matter either: md:grid-cols-2 grid-cols-1 gives exactly the same result as grid-cols-1 md:grid-cols-2. What counts is the order of the CSS that Tailwind generates.

📌 max-* prefixes and your own breakpoints

@import "tailwindcss";

@theme {
  --breakpoint-3xl: 120rem;
}

Now the 3xl: prefix is available too, from 120rem = 1920px.

⚠️ Why doesn't the layout change with the screen?

TL;DR: A breakpoint prefix (a breakpoint is the screen width where the style changes) means "from this width and up": sm: 640px, md: 768px (48rem), lg: 1024px, xl: 1280px, 2xl: 1536px. Tailwind turns it into the media query @media (width >= ...), a CSS rule that applies only from that width. A class with no prefix applies on every screen. So write the phone style first with no prefix, then override it with prefixed classes for wider screens: that's mobile-first. grid grid-cols-1 md:grid-cols-2 gap-4 = one column on a phone, two columns from 768px, and still two columns on a laptop.

⚡ One column first, two columns later

Write:

<div id="layout" class="grid grid-cols-1 md:grid-cols-2 gap-4">
  <div class="bg-blue-100 p-4">Column 1</div>
  <div class="bg-green-100 p-4">Column 2</div>
</div>

Meaning: md: is a breakpoint prefix (a breakpoint is the screen width where the style changes). A class with no prefix applies on every screen. md:grid-cols-2 is only on from 768px wide and up, and then it overrides grid-cols-1.

The exercise looks for the text grid-cols-1, md:grid-cols-2 and gap-4 exactly like that, with no space after the colon.

⚡ md: isn't only for tablets

Guess first: on a 1280px laptop, is that #layout still two columns?

Yes. md: means from 768px and up, with no upper limit.

Meaning: sm: 640px, md: 768px, lg: 1024px, xl: 1280px, 2xl: 1536px. They all mean "from this width and up".

⚡ Phone styles have no prefix

Guess first: how many columns does grid-cols-2 md:grid-cols-1 give on a phone?

Two narrow columns, and on a laptop one long column instead. That is the reverse of what you usually want.

Meaning: Mobile-first (starting from the phone): write the phone layout with no prefix, then add prefixes for wider screens. The order of classes in the class attribute doesn't change the result.

What you will learn