Squashed on phones? Responsive layout and media queries, explained plainly
One page should stand up on a phone, a tablet, and a computer. This guide follows MDN's Responsive web design, Using media queries, and the viewport meta reference: fluid HTML, the viewport tag, fluid grids and max-width, images that never burst their container, media query syntax, breakpoints set by content, mobile first, responsive type, testing capability instead of width, and a pre-launch checklist.

Let me set the boundary first. This site already has two guides on layout: CSS Grid covered two-dimensional layout, Flexbox covered one-dimensional layout; and there is a mobile experience checklist that lists what to check on phones. This guide does not repeat them. It covers one thing only: how the same page stands up on a phone, a tablet, and a computer. It is about how to write it, not what to check: how to write the viewport tag, how to write widths, where to put breakpoints, and how the font size changes with the screen. The mobile experience checklist tells you what to check; this guide tells you how to change the code once a check fails. Grid and Flexbox are layout tools; this guide is about when to switch layouts. The running example is the same fictional practice project as the earlier guides: the Greenstone Cafe order page, a small web tool for a street-corner cafe. Earlier guides covered how its order page looks and how it does the math; this one covers how it takes orders on a phone and on a computer: a single column with big buttons on a phone, two columns of menu plus a sidebar on a computer. Everything in this article that uses the cafe as an example — the operations, the code, and the numbers — is made up to explain the method. It is not a real record. All syntax and behavior of responsive layout and media queries comes from three official MDN documents: "Responsive web design", "Using media queries", and "<meta name="viewport">". The sources are listed in this article's meta information and evidence record. This guide does not cover native app publishing, push notifications, or container queries in full; it only mentions container queries by name where relevant. After reading it, you should be able to do four things: write the viewport meta tag, use fluid layout instead of fixed widths, write media queries and breakpoints, and make font sizes follow the screen.
Step 1: HTML is fluid by nature — do not rush to fix widths
MDN's "Responsive web design" opens with a fact many people never notice: HTML is fundamentally responsive, or fluid. If you write only HTML and no CSS, the browser automatically reflows the text to fit the viewport: narrow the window and the text wraps, widen it and the text spreads out. The trouble starts in the next two moves: a full-width line of text on a wide monitor is too long to read comfortably, so people use CSS to narrow the content into columns — but narrow columns on a phone get squashed until only two or three words fit on a line, which is even harder to read. A fixed-width page does not work either: it produces scrollbars on narrow devices and too much empty space on wide screens. MDN's conclusion is that responsive web design is not a separate technology, but an approach: a set of best practices that let a layout respond to whatever device is viewing the content. The term responsive design was coined by Ethan Marcotte in 2010, and at the time it meant three things: fluid grids, fluid images, and media queries. A fluid grid means sections take space proportionally instead of fixed pixels; a fluid image means an image never exceeds its container; a media query means swapping styles by condition. The cafe order page shows it directly: the menu is two columns on a computer and one column on a phone — not two pages, but one page changing layout at different widths. The judgment in this step is: first ask "what happens to this block if I do not set a width?" — whatever can flow should flow, and only the places flow cannot fix get the treatment in the next steps. Remember one baseline: a horizontal scrollbar is the first red line of responsive layout. At no width should a user have to drag sideways to read the main text.
Step 2: Write the viewport meta tag first, or the phone will think it is 980 pixels wide
The first line of responsive layout code is not CSS but one line of HTML in the head: <meta name="viewport" content="width=device-width" />. MDN's "Responsive web design" says it plainly: you should always include the viewport meta tag in the head of your documents. Why is it mandatory? Because mobile browsers lie about their viewport width. MDN's viewport reference page tells the history: when smartphones first arrived, most sites were not mobile optimized, so mobile browsers set the viewport width to 980 pixels by default, rendered the page at that width, and showed the result shrunk down to fit the phone screen. Users could zoom and pan around, but it looked bad. Worse, the lie breaks your media queries: you wrote a narrow layout that kicks in at 480 pixels or less, but the phone reports a viewport width of 980 pixels, so the narrow layout never triggers. Writing width=device-width tells the browser to drop the 980-pixel default and use the device's real width as the viewport width. MDN's viewport reference page also lists the other keys that can go in content: width can be a whole number of pixels between 1 and 10000, or the special value device-width, which also establishes the value of the vw unit; height can be device-height, which establishes the vh unit; initial-scale is the initial zoom ratio, a number between 0.0 and 10.0; maximum-scale and minimum-scale set the zoom limits; user-scalable controls whether the user can zoom, defaulting to yes; viewport-fit controls how the page displays on notched screens, with auto, contain, or cover; and interactive-widget controls how the viewport changes when a virtual keyboard appears, with resizes-visual, resizes-content, or overlays-content. Two warnings matter most. First, initial-scale=1 is often omitted, and MDN states plainly that omitting it is usually unnecessary: the browser decides the initial zoom automatically, and you only need to add it when overflowing content causes unwanted shrinking. Second, never write user-scalable=no or maximum-scale=1 to lock zoom: MDN states that iOS 10 and above ignores those settings by default, and locking zoom stops users with poor eyesight from enlarging the text — trading accessibility for convenience. The rule for the cafe order page is one line: the first thing in any new page is the viewport meta tag in the head, written before any CSS.
Step 3: Fluid layout — percentages and max-width instead of fixed widths
With the viewport set correctly, the layout itself comes next. The second thread in MDN's "Responsive web design" is the flexible grid: responsive sites are built on flexible grids, and you do not need pixel-perfect layouts for every possible device size. The method is to let content spread naturally as it widens and fold naturally as it narrows, adding a breakpoint only where the content starts to look bad. The two most common tools are percentages and max-width. In its combined example, MDN writes .wrapper { max-width: 960px; margin: 2em auto; }: the container is at most 960 pixels wide, stops growing beyond that, and centers itself with auto margins; below 960 pixels, it follows the viewport and never produces a horizontal scrollbar. The cafe menu container can be written the same way: on a phone it nearly fills the screen, on a computer it caps at 960 pixels and centers, with whitespace on both sides so lines never grow too long to read. Percentages work inside the container: in a two-column layout, one column takes 1 part and the other 2 parts, or one takes 30% and the other 70%, and the columns follow the container without rewriting every width. MDN adds a sentence that is easy to miss: media queries can help with responsive design, but they are not a requirement — flexible grids, relative units, and minimum and maximum values work without media queries at all. Font size using calc(1.5rem + 4vw), for example, grows with the viewport smoothly, and step 8 covers it in detail. The bad example is a container fixed at 1200 pixels wide: on a phone screen 390 pixels wide, the container overflows immediately, a horizontal scrollbar appears, and the menu cannot be tapped. The check for this step is simple: drag the browser window from wide to narrow and watch for any point where a horizontal scrollbar suddenly appears. Wherever it appears, a width is fixed — change it to a percentage or a max-width.
Step 4: Do not let images burst their container — max-width: 100% is the baseline
Text wraps by itself. Images do not. An image is as wide as it is, and when its container is narrower, it overflows. MDN's "Responsive web design" gives three lines of code as the baseline: img, picture, and video all get max-width: 100%. That means a media element is never wider than its container: as the container narrows, the image scales down proportionally, and as the container widens, the image never grows larger than its own intrinsic size and turns blurry. This is the fluid image idea from Ethan Marcotte's original definition: max-width set to 100% lets an image scale down to fit its content without growing larger and becoming pixelated when the container widens. The cafe dish photo is a ready example: the menu card is 400 pixels wide on a computer and the photo follows the card; on a phone the card is only 350 pixels wide and the photo shrinks to 350 pixels automatically instead of bursting the card. But MDN also flags the matching problem: serving one large image and scaling it down with CSS wastes bandwidth — a phone user downloads a desktop-sized image just to see a small one — and a landscape image that looks good on a widescreen monitor can be too small to see on a phone, where a portrait image would fit better. Those problems are solved with the picture element and the srcset and sizes attributes on img, which MDN states are advanced features beyond the scope of that tutorial, pointing to a dedicated responsive images guide. One more detail that is easy to miss: the source elements inside video and audio can use the media attribute to select files by condition, which MDN notes in the same article. The rule for this step is one sentence: write max-width: 100% for every image and video on the site first, and only then talk about other optimizations. Any page missing that line will sooner or later have one image breaking its layout on a phone.
Step 5: How to write a media query — types, features, and logical operators
A media query is the switch that changes styles in responsive layout, and MDN's "Using media queries" opens by describing its structure: a media query is composed of an optional media type and any number of media feature expressions, which may optionally be combined using logical operators, and media queries are case-insensitive. There are three media types: all, print, and screen. The type is optional and defaults to all; it is only required when using the only logical operator. Many media types were originally defined, but MDN states clearly that most were deprecated, leaving just screen, print, and all — use media features for anything more specific. A media feature is a long list: width, height, orientation, aspect-ratio, resolution, color, hover, any-hover, pointer, any-pointer, prefers-color-scheme, prefers-reduced-motion, forced-colors, display-mode, scripting, device-posture, dynamic-range, and more, and each media feature expression must be surrounded by parentheses. The most common form tests width. MDN's "Responsive web design" example is @media screen and (width >= 80rem): screen media with a viewport at least 80rem wide, and the .container rule inside only applies when both are true. Width has two spellings, old and new, and MDN's "Using media queries" states they are equivalent: @media (max-width: 1250px), @media (width <= 1250px), and @media (1250px >= width) are the same query; @media (min-width: 30em) and (max-width: 50em) is the same as @media (30em <= width <= 50em). Note that the range comparisons are inclusive — to exclude the comparison value, use the < and > operators, as in @media (30em < width < 50em). There are three logical operators: and joins conditions, not negates, and only was used for older browsers and has no effect in modern browsers. A comma means "or": @media (height >= 680px), screen and (orientation: portrait) applies when the height is at least 680 pixels or the screen is in portrait mode — either one is enough. MDN adds two details that are easy to trip over. First, a feature can be written without a value: @media (color) applies to any device with a color screen, as long as the feature's value is not 0 or none. Second, when a feature does not apply to the device the browser is running on, expressions involving that feature are always false — testing (hover: hover) on a device with no hover capability is always false. The cafe order page should be written like this: one default set of styles for narrow screens, one more set inside @media (width >= 600px) for wide screens, and a separate set inside @media print for printing. Do not squeeze all three into a single rule.
Step 6: A breakpoint is not a phone model — it is where the content breaks
Where a media query goes in is called a breakpoint, and MDN defines it as the point at which a media query is introduced and the layout changes. Many beginners copy breakpoints from phone models: the iPhone is 390 pixels, the iPad is 768, the laptop is 1440 — write three media queries and call it done. That is wrong, and wrong in a specific way: new models arrive and the size table goes stale; foldable screens, rotating between portrait and landscape, and split-screen windows never match a model table. MDN's "Responsive web design" recommends the opposite: do not set breakpoints by device, set them by content. Start with a flexible grid, let the layout follow the width on its own, and add a breakpoint where the content starts to look bad: two columns squeezed until only two words fit on a line get a breakpoint; a heading breaking across lines gets one; a gap between cards wide enough to fit another card gets one. The same article adds that best practices encourage defining breakpoints with relative units rather than the absolute sizes of an individual device. Writing @media (width >= 40rem) is better than @media (width >= 640px), because rem follows the root font size — when a user enlarges the system font size, the breakpoint moves with it. The cafe order page found its breakpoints exactly this way: the two-column menu gets cramped below 600 pixels — MDN's example happens to use the same 600-pixel threshold — one column narrow, two columns wide; the heading is only worth enlarging above 1200 pixels, and MDN's typography example uses that same 1200-pixel point. Do not be greedy with breakpoints either: three or four are enough — one for phone to tablet, one for tablet to desktop, one for extra-wide screens. Any more is trouble you make for yourself. The check for this step is: drag the browser window narrower slowly and note the width where the layout first looks bad. That width is your breakpoint — not any phone's spec sheet.
Step 7: Mobile first — write the narrow screen first, then add the wide one
With breakpoints decided, which end do you write first? MDN's answer is mobile first: create a simple single-column layout for narrow-screen devices, then check for wider screens and implement a multiple-column layout when you know you have enough screen width. Why narrow first? Because the narrow screen is the tightest constraint: not enough width, buttons must be big, content can only stack in one column. Getting the narrow screen right first means doing the hardest part first; the wide screen only adds to it, instead of squeezing a desktop layout down. MDN's "Responsive web design" gives two ready-made examples. The Flexbox version: the narrow screen is a single column by default, and inside @media screen and (width >= 600px), .wrapper becomes display: flex, the first column gets flex: 1, the second flex: 2, plus a 5% right margin, so the two columns split the space 1 to 2. The Grid version: the narrow screen is a single column by default, and inside @media screen and (width >= 600px), .wrapper becomes display: grid with grid-template-columns: 1fr 2fr and column-gap: 5% — three lines turn one column into two columns at a 1 to 2 ratio. Both examples share the same structure: base styles sit outside the media query and apply at every width; enhanced styles sit inside the media query and apply only to wide screens. The cafe order page follows this structure: the menu is a single column by default with dish cards stacked; above 600 pixels it becomes two columns, recommended dishes taking two parts and regular dishes one; above 1200 pixels a sidebar appears on the right with opening hours and the address. Writing it the other way — desktop first, with a multi-column desktop layout and max-width queries to shrink it — also runs, but it is where beginners trip most: the desktop styles are so numerous that the narrow screen has to override them one by one, and whatever is missed stays behind — usually as a horizontal scrollbar on a phone. Mobile first has a side benefit too: phone users receive the fewest styles first, so the first screen is faster. The rule for this step is: for a new page, write the narrow screen first, check it at phone width, and only then write the wide-screen media queries.
Step 8: Font sizes must respond too — rem, vw, and calc each have a job
When the layout changes, the font size has to change with it. Otherwise there are two kinds of discomfort: a heading on a phone so large it fills half the screen, or a heading on a computer so small it looks like body text. MDN's "Responsive web design" describes three levels of approach. The first is changing the font size inside a media query: h1 defaults to 2rem, and inside @media (width >= 1200px) it becomes 4rem — the heading is twice the root font size by default and four times on a wide enough screen, so the jumbo heading only appears on larger screens. MDN adds in the same article that media queries are not restricted to changing layout: any element can be tweaked for alternate screen sizes, and font size is only one of them. The second level uses viewport units, vw: 1vw equals one percent of the viewport width, so setting h1 to font-size: 6vw makes the heading follow the viewport without any breakpoint. But MDN follows immediately with a warning: doing this means the user loses the ability to zoom any text set using the vw unit, because that text is always related to the size of the viewport — therefore you should never set text using viewport units alone. The third level combines the two: font-size: calc(1.5rem + 4vw) — the heading equals 1.5 times the root font size plus four percent of the viewport width. The rem part keeps the text zoomable, the vw part lets the heading grow smoothly with the viewport, and there is no need to redefine it at every breakpoint. The cafe menu heading follows the third level: the big menu heading uses calc(1.5rem + 4vw), body text stays at 1rem, and prices use 1.25rem in bold. One matching habit: write all font sizes on the site in rem, not px, because rem follows the root font size — when a user enlarges the browser's default font size, the whole site grows with it, while px values stay put. The judgment for this step is: whether a heading size should change with the screen depends on how many lines it takes on a phone. More than three lines means it should be smaller; one line with half the screen empty means it should be larger — then use calc to make the transition smooth.
Step 9: Test capability, not just width — hover, pointer, and orientation
Media queries can test more than width. MDN's "Using media queries" lists a full set of media features, and several of them test what a device can do. The first is hover: @media (hover: hover) applies only when the user's primary input mechanism can hover over elements — a mouse can hover, a finger cannot. On the cafe order page, dish cards on a computer show an "add to cart" button on hover; on a phone there is no mouse, hover never fires, and the button stays hidden forever, so the user can never tap it. The correct approach is to put hover effects inside @media (hover: hover) only, and show the button outright on phones instead of hiding it. The second is pointer and any-pointer: pointer tests the precision of the primary input, any-pointer tests any input device — a touchscreen phone reports coarse, a mouse reports fine. The third is orientation: portrait for vertical, landscape for horizontal. MDN's example is @media print and (orientation: portrait), applying only when printing in portrait; on the order page, landscape can switch the menu to two columns and portrait back to one. The fourth is prefers-color-scheme, which tests whether the user prefers a light or dark theme — the dark-mode guide on this site covers how to use it, so this guide does not repeat it, except to say that testing this feature is enough and there is no need to guess with JavaScript. The fifth is prefers-reduced-motion, which tests whether the user asked for less motion — the motion guide covers its use. Two more are easy to overlook: display-mode tests whether the page is opened in fullscreen or standalone mode, and forced-colors tests whether the user turned on forced-colors mode. The judgment for this step is: before writing any style that should apply "only on a certain kind of device", ask whether you are testing width or capability. Test capability with the matching media feature, and do not guess the device from the width: 800 pixels wide could be a small laptop or a tablet turned sideways, and a wrong guess means the wrong style.
Step 10: Media and video need responsive treatment too — picture, source, and bandwidth
Once the layout and the font sizes respond, images and video can still hold the page back. In its image section, MDN's "Responsive web design" starts with the baseline from step 4 — max-width: 100% — and then covers what lies beyond it: serving one large image to every device wastes bandwidth, because a phone user downloads a desktop-sized image just to see a small one; and a landscape image that looks good on a widescreen monitor can be too small to see on a phone, where a portrait image would fit better. The fix is the picture element and the srcset and sizes attributes on img, which serve different sizes and crops to different devices. MDN states plainly that these are advanced features beyond the scope of that tutorial, pointing to a dedicated responsive images guide. Video and audio have a matching method: the source elements inside video and audio can use the media attribute to select files by condition, which MDN notes in the same article. The cafe example: the dish photo uses an 800-pixel-wide landscape image on a computer and a 400-pixel-wide portrait image on a phone, switched by picture and the media attribute on source; the promotional video at the shop entrance does not autoplay on a phone at all — it shows a cover image with a play button instead, saving bandwidth and battery. One more detail is high-density screens: MDN's viewport reference page explains the pixel ratio — below 200dpi the ratio is 1.0, between 200 and 300dpi it is 1.5, and above 300dpi it is the integer floor of density divided by 150dpi. A phone screen looks small but packs many pixels, so bitmap images can look soft on it; to keep images sharp on high-density screens, use vector graphics or prepare images at twice the size and shrink them with CSS. The rule for this step is: first write max-width: 100% across the whole site, then optimize images for bandwidth and sharpness — never the other way around.
Step 11: The usual ways it breaks — and a chart of the causes
Collecting the traps from the ten steps above, the order page's phone failures come in seven kinds, ranked here. The chart below shows a fictional breakdown of 100 phone failures in one month on the Greenstone Cafe order page. The numbers are invented examples, mapped against the traps in the ten steps above: no viewport meta tag 24 times — the phone rendered at the 980-pixel virtual viewport and the narrow layout never triggered once; fixed pixel widths overflowing horizontally 19 times — a container fixed at 1200 pixels dragging a horizontal scrollbar onto the phone; layout not following portrait-to-landscape rotation 15 times — portrait was fine, but rotating the phone left a single column; images without max-width bursting their container 13 times — one dish photo pushing the whole page wider; breakpoints copied from phone models 11 times — a new model arrives and the size table goes stale; font size written in vw alone 10 times — zoom stopped working and the text was too small to read; buttons too small to tap 8 times. 24 + 19 + 15 + 13 + 11 + 10 + 8 = 100, and the account balances. Of the seven failures, the first two are "not written", the middle three are "written in the wrong place", and the last two are "not thinking about the user". The "not written" pair is the easiest to fix: one viewport meta tag plus three lines of max-width: 100% clears 37 of the 100 failures. The "wrong place" trio needs a habit change: set breakpoints by content, not by phone model; test both portrait and landscape instead of only portrait; write font sizes as calc(1.5rem + 4vw) instead of vw alone. The "not thinking about the user" pair needs a change of viewpoint: buttons on a phone must be big enough for a finger to tap, and a button hidden behind a hover effect does not exist on a phone at all. The numbers in the chart are fictional, but the order is real: if the viewport and fixed widths are not right, no amount of careful work on breakpoints, font sizes, and buttons will save the page.
Step 12: A pre-launch checklist — drag from 360 pixels to 1920 pixels
Once the order page's responsive layout is written, run through these five questions before launch. First, is the viewport written? Open the head: the first line should be <meta name="viewport" content="width=device-width" />. If it is missing, write it before anything else. Second, is there a horizontal scrollbar? Drag the browser window slowly from 360 pixels to 1920 pixels: at no width should a horizontal scrollbar appear, and wherever one does, a width is fixed or an image is not contained. Third, were the breakpoints set by content? Look back at every media query and ask: "Is this width where the content starts to look bad, or is it some phone's spec?" If it is the latter, change it to the former. Fourth, can the font size be zoomed? Enlarge the browser font size by two steps: the whole site should grow with it. Anywhere written in vw alone should become calc(1.5rem + 4vw). Fifth, can you order on a phone? Tap through it on a real phone or in the developer tools' phone mode: buttons are visible without hovering and can be tapped; rotating between portrait and landscape keeps the layout following; no image bursts its container. After those five checks, look back at the three MDN documents: the Learn article covers the whole responsive approach, Using media queries covers the syntax details of media queries, and the viewport reference page covers what each key in content means. They have different jobs, so do not knock on the wrong door: if you do not know how to write a media query, go to Using media queries; if you do not know what a viewport key does, go to the viewport reference page; if you do not know how the whole thing should be done, go to the Learn article. One last note: container queries, the srcset and sizes attributes of responsive images, and the full use of prefers-color-scheme were left out of this guide, and each has its own MDN document — when you really need one of them, read the original first instead of writing from memory.
Comments (0)
Sources
Related articles
You already call functions every day; this guide names what you have been doing. Following MDN's Functions, Arrow function expressions, and Rest parameters: declarations versus expressions, parameters and arguments, default values, rest parameters, spread syntax, return values, arrow function shorthand, the this trap, callbacks, and scope.
Numbers in JavaScript are one type, Number, stored as 64-bit floats — which is why 0.1 + 0.2 is 0.30000000000000004. This guide follows MDN's Basic math in JavaScript, Math, and Number: one number type, float error, counting money in cents, converting strings, NaN and Infinity, safe integers, the four rounding methods, Math.random, the Math toolbox, and formatting for display.
Flexbox lays things out in one direction at a time, and when it is done right, the nav bar, the card wall, and the cart rows all behave. This guide follows MDN's Basic concepts of flexbox, Aligning items in a flex container, and Controlling ratios of flex items along the main axis: the two axes, containers and items, direction and wrapping, free space, the three flex properties, alignment on both axes, and gaps and auto margins.