You open a site on your phone and the text is tiny. You have to pinch the screen to read it, then drag left and right to see the price.
A page that makes you pinch and drag isn't responsive yet. Responsive means one page that adapts to every screen width, from a phone to a monitor. Mobile-first is the way to write it: you write the CSS for the smallest screen first, with no media query (a block of CSS that applies only in certain conditions). Then new rules are added with @media (min-width: ...) each time the screen gets wide enough. Picture packing for a holiday. If you start with a small backpack, you're forced to pick what matters first: a change of clothes, a charger, medicine. If you then get a big suitcase, you just add things. If you start with a full suitcase and then have to move to a backpack, you unpack in a hurry, and the important things can get left behind. The small backpack is the phone screen, the big suitcase is a wide screen, and the important things are the content the page must always have.
Why start with the phone
The figure "70%+ of web traffic in Mrs. Sri's country is mobile" is quoted a lot. The data is from StatCounter, a service that records visits to many sites. The figure is the share of page views (pages opened) that it records, not of people: 70.18% in 2016 and 74.35% in 2017. Since then it has gone up and down: 66.56% in 2022, 63.06% in 2023, 64.78% in 2024, 61.51% in 2025, and only 45.29% for January to September 2026. The lesson: say the year when you quote a number. Better still, look at your own site's analytics (its visit data).
Two reasons don't depend on that number:
- A small screen forces choices. On a 320px screen there's no room for everything. In the 40 guest chats, the most frequent questions were free dates, the total, the cancellation rule and how to pay. Those four must show first. The rest waits for a wider screen.
- 320px is an accessibility requirement. WCAG 1.4.10 (Reflow, level AA) asks that content can be used at a width of 320 CSS px without scrolling in two directions at once (up-down and left-right). That width equals a 1280px screen zoomed to 400%. So a page that works at 320px also works for people who magnify their screen.
The viewport meta first
Before any CSS, a responsive page needs one <meta> tag (information for the browser) inside the <head>:
<meta name="viewport" content="width=device-width, initial-scale=1">
Without this line, a phone browser treats its screen as 980px wide. It lays the page out that wide, then shrinks it to fit. We tried it on an emulated (imitated) 375px phone, in Chromium and WebKit. Without the meta, the page was 980px wide, the three columns for wide screens appeared, and everything was shrunk until the text was hard to read. With the meta, the width is 375px, and media queries work as they should.
The base first, then min-width
Next, write the base CSS for phones, and add rules for wide screens. This code sets up the page (.page) and Mrs. Sri's list of room cards (.rooms):
/* Base: applies from 320px, no media query */
.page { max-width: 1200px; margin: 0 auto; padding: 16px; }
.rooms { display: grid; gap: 16px; } /* one column */
/* 768px and up: three columns, roomier spacing */
@media (min-width: 768px) {
.page { padding: 24px; }
.rooms { grid-template-columns: repeat(3, 1fr); gap: 24px; }
}
- Rules outside a media query apply on every screen. Those rules are the phone version.
@media (min-width: 768px)means "a screen of 768px or more". Mobile-first always uses min-width: each block adds something for wider screens.max-width: 1200pxandmargin: 0 autofrom the grid lesson stop the page from growing wider than 1200px.
Here is the result at five screen widths, measured in Chromium, WebKit and Firefox. The last two columns use methods explained further down: where the card's photo sits (container queries) and the heading size (clamp()).
| Screen | Columns | Card width | Card | Heading (clamp()) |
|---|---|---|---|---|
| 320px | 1 | 288px | photo on top | 24px |
| 375px | 1 | 343px | photo on top | 25.38px |
| 667px (phone sideways) | 1 | 635px | photo on the left | 32.67px |
| 768px | 3 | 224px | photo on top | 35.2px |
| 1280px | 3 | 384px | photo on top | 40px |
The 768px breakpoint (the screen width where the layout changes) isn't picked for a particular tablet brand. It's picked because that's the width where three cards start to fit. The content is 720px wide (768 minus 24px of padding on the left and right), minus two 24px gaps, divided by three: (720 - 2 × 24) / 3 = 224px. Put breakpoints at the width where your layout starts to break or to look empty.
The opposite is desktop-first: design three columns first, then take things away with max-width media queries (which apply on screens up to that width). What often happens when moving to the phone: the layout breaks, content overflows (goes outside its box), and whatever doesn't fit gets hidden. In the end the page is redesigned starting from nothing. We measured, at 320px, a page that only has the desktop CSS (repeat(3, 1fr), three equal columns, with no media query). The browser doesn't stack the columns on its own. Each card becomes 74.67px, the price "$35 / night · 2 guests" breaks over 2 lines, and the card's content spills 33px out of the card.
Container queries: a card that fits its space
Say Mrs. Sri's room card is made side by side (photo on the left) with @media (min-width: 600px). On a phone turned sideways, the screen is 667px, the card is 635px wide and looks good. On a 768px tablet, three cards sit in a row and each is only 224px. What happens to the card at 768px? Guess first.
A media query asks the screen. At 768px the screen is wide, so @media (min-width: 600px) puts the card side by side, even though the card is only 224px. The 192px photo is still placed, and the text gets only the leftover 89.5px. As a result the Terrace Room's price breaks over 2 lines, the content spills 88px out of the card, and the whole page can be scrolled sideways by 63px (the same in all three browsers). So the answer to the guess: the card breaks.
A container query asks the container (the element that wraps the card). In this code the container is .room, one grid cell that holds one card:
.room { container-type: inline-size; } /* each grid cell becomes a container */
/* A card of 30rem (480px) and up: photo on the left */
@container (min-width: 30rem) {
.room-card { grid-template-columns: 12rem 1fr; }
.photo { grid-row: 1 / span 3; height: auto; min-height: 120px; }
}
container-type: inline-sizemakes.rooma container whose width can be queried. The card (.room-card) sits inside.room.@container (min-width: 30rem)applies when the container is 480px or wider, whatever the screen width. On a 667px screen the card is 635px, so it's side by side. On a 768px screen the card is 224px, so it's stacked again (photo on top). On a 1280px screen the card is 384px, so it stays stacked.- Supported since Chrome 105, Safari 16 and Firefox 110 (caniuse data, a site that records browser support).
clamp(): type that grows
The heading size can also follow the screen width with no media query:
h1 { font-size: clamp(1.5rem, 1rem + 2.5vw, 2.5rem); }
clamp(min, value, max) uses the middle value, but never below min or above max. Here the min is 1.5rem (24px) and the max is 2.5rem (40px). 1vw is 1% of the screen width, so 2.5vw = 0.025 × the screen width. At 768px: 1rem + 2.5vw = 16 + 0.025 × 768 = 35.2px. The 24px minimum is reached exactly at 320px (16 + 8), and the 40px maximum from 960px. This one line replaces several media queries for font size, and the size grows smoothly with no jumps.
Compare a fixed 40px heading: "Rooms at Mist Homestay" breaks into 2 lines at 320px and 375px, and takes 96px of screen height before the guest sees any room. With clamp() the heading is 1 line.
Keep rem inside the middle value. 1rem grows when the user raises the browser's default font size. vw only follows the screen width. A heading that is only 5vw doesn't grow at all for someone who needs bigger text.
⚠️ Why does the layout break on a phone?
- Forgetting the viewport meta. The page is laid out at 980px then shrunk, and the phone media queries never run.
- Writing desktop first, then patching with max-width. Columns don't stack by themselves: at 320px three cards become 74.67px and their content spills out.
- Media queries for components. A card that's narrow on a wide screen (224px at 768px) is forced side by side too. For components, ask for the container's width with a container query.
- An
@containerrule aimed at the container itself. The rules inside@containerapply to the container's contents, not to the container itself. That's whycontainer-typesits on.room, and the side-by-side styles on.room-card. - Hiding content on phones. Guests on phones need the same information: the cancellation rule and the total mustn't disappear just because the screen is small.
TL;DR: Mobile-first: write the CSS for the smallest screen first, with no media query (a block of CSS that applies only in certain conditions). Then add rules with @media (min-width: ...), always min-width (a screen of that width or more). Start with <meta name="viewport" content="width=device-width, initial-scale=1">: without it, a phone lays the page out 980px wide and then shrinks it. The "70%+ of traffic is mobile" figure is StatCounter's data from 2016 to 2017. 2025 recorded 61.51%, so say the year and check your own site's analytics (its visit data). Test at 320px (WCAG 1.4.10 Reflow), 375, 768 and 1280px: Mrs. Sri's page is 1 column on phones (cards of 288px and 343px), and 3 columns with 224px cards at 768px and 384px cards at 1280px. Desktop-first (starting from three columns, then taking things away) often ends with a broken layout, overflowing content or hidden content, until it has to be redesigned. Container queries (container-type: inline-size and @container (min-width: 30rem)) make a card follow its container's width (the element that wraps it), not the screen's. clamp(1.5rem, 1rem + 2.5vw, 2.5rem) grows the heading from 24px to 40px (35.2px at 768px). Keep rem inside so the heading follows the user's font size setting.
⚡ The viewport meta first
Write:
<meta name="viewport" content="width=device-width, initial-scale=1">
Meaning: This line goes in the page's <head>. Without it, a phone browser treats its screen as 980px wide, lays the page out that wide, then shrinks it. With it, the page is as wide as the phone's screen, and media queries (blocks of CSS that apply only in certain conditions) work as they should.
⚡ The base first, then min-width
Write:
/* Base: applies from 320px, no media query */
.page { max-width: 1200px; margin: 0 auto; padding: 16px; }
.rooms { display: grid; gap: 16px; } /* one column */
/* 768px and up: three columns, roomier spacing */
@media (min-width: 768px) {
.page { padding: 24px; }
.rooms { grid-template-columns: repeat(3, 1fr); gap: 24px; }
}
Meaning: Mobile-first means writing the CSS for the smallest screen first. Rules outside a media query are the phone version: 16px of padding and the room list .rooms in one column. @media (min-width: 768px) applies on a screen of 768px or more, so that block only adds things for wider screens: three columns and 24px spacing. Mobile-first always uses min-width.
⚡ Starting from the desktop
Guess first: you design desktop-first, starting from a 3-column layout for wide screens, then move to the phone. What often happens?
The layout breaks, the content overflows (goes outside its box), some of it gets hidden, and it ends up redesigned starting from nothing. At 320px the three columns don't stack on their own: each card becomes 74.67px and its content spills 33px out.
Meaning: Mobile-first prevents that problem. For cards inside a grid there's one more step: a card should ask its container, not the screen. container-type: inline-size on .room (the card's container) plus @container (min-width: 30rem) puts the card side by side (photo on the left) only when its container is 480px or wider, whatever the screen width.