Sub topic 1
What Responsive Design Is and Why It Matters
Prerequisites
This chapter assumes you already know the following:
- Basic HTML structure: elements, attributes, and the document
<head>and<body>. - CSS selectors, properties, and the cascade, including how later rules override earlier ones of equal specificity.
- The CSS box model: content, padding, border, and margin.
- Common CSS units such as
px,em, andrem.
You do not need to know media queries yet. This chapter explains the problem they solve and the pieces that fit together around them. Later chapters cover each piece in detail.
The problem: one page, many screens
A web page is opened on phones, tablets, laptops, wide desktop monitors, TV browsers, and printers. The widths of these surfaces range from about 320 CSS pixels to more than 2500. People also rotate their devices, resize their browser windows, and split screens side by side.
Early websites handled this in one of two ways:
- Fixed-width layouts. The page was built for one width, often 960px. On a narrower screen the user scrolled sideways. On a wider screen the page sat in the middle with empty space around it.
- Separate mobile sites. A second site, often on an
m.subdomain, was served to phones. Two sites meant two codebases, duplicated content, and broken links when a shared URL opened the wrong version.
Neither approach scales when new screen sizes keep appearing. Responsive design replaces both with a single page that adapts.
What responsive design is
Responsive design is an approach in which one HTML document and one set of styles adjust to the environment they are displayed in. The term was introduced by Ethan Marcotte in 2010. He described it as a combination of three techniques:
- Fluid grids. Layout widths are relative (percentages,
frunits,rem) instead of fixed pixel values. - Flexible images. Media scale within their containers instead of overflowing them.
- Media queries. CSS rules apply only when certain conditions are true, such as the viewport being at least a given width.
Modern CSS extends this list. Grid and flexbox give you layouts that reflow without hand-written widths. Functions like clamp() give you values that scale smoothly. Preference queries let a page respond to the user's settings, not just the screen size. Container queries let a component respond to the space it was given.
The important idea is that the page does not detect "a phone" or "a tablet". It reads measurable conditions, such as width, input type, or user settings, and applies styles that match. Those conditions are far more reliable than guessing the device.
What a media query looks like
A media query is a condition attached to a block of CSS. The browser applies the block only while the condition is true. The condition is re-evaluated continuously, so the styles switch live when you resize the window or rotate the device.
Here is a first example. It is intentionally small. The next chapters explain every part of the syntax.
.nav {
display: block;
}
@media (min-width: 40em) {
.nav {
display: flex;
gap: 1rem;
}
}Below a viewport width of 40em, the navigation is a plain block. At 40em and wider, the second rule applies and turns it into a horizontal flex row. Both rules target the same selector. The media query block has the same specificity as the first rule, so it wins because it comes later in the stylesheet. This is the cascade doing its normal work.
How the pieces fit together
A responsive page is built from several cooperating layers. Each layer handles a different part of the problem.
| Layer | What it does | Example tool |
|---|---|---|
| Viewport setup | Tells mobile browsers to use the device width as the layout width | `<meta name="viewport">` |
| Conditions | Decide when a block of styles applies | `@media`, `@container` |
| Fluid sizing | Lets values scale between conditions | `%`, `rem`, `vw`, `clamp()` |
| Flexible layout | Rearranges content as space changes | Grid, flexbox |
| Flexible media | Keeps images inside their containers | `max-width: 100%` |
| User context | Adapts to settings and input methods | `prefers-color-scheme`, `hover` |
Media queries are only one layer. A page with fluid sizing and a flexible layout often needs very few queries. A page with fixed sizes needs many, and it is still fragile between the breakpoints.
Why it matters
Responsive design is standard practice for practical reasons.
- One codebase. You fix a bug or change content once. Every screen gets the update.
- One URL per page. Links, bookmarks, and shared addresses work the same on every device. Search engines also index a single version. Google has recommended responsive design for mobile-friendly sites for years.
- Mobile traffic. On many sites, more than half of visits come from phones. A layout that fails on a small screen fails for most of your audience.
- Accessibility. Users who zoom to 200% or 400% effectively shrink the viewport in CSS pixels. A responsive layout reflows at that zoom level. A fixed layout forces horizontal scrolling. WCAG 2.1 success criterion 1.4.10 (Reflow) asks for exactly this reflow behavior.
- Future devices. Foldables, new window sizes, and embedded browsers need no special handling if the design reacts to conditions instead of device names.
The viewport problem on mobile
Responsive CSS has one trap that catches almost everyone the first time. Mobile browsers do not report their true screen width to a page by default. To keep old fixed-width sites usable, they pretend to be about 980px wide, render the page at that width, and then scale the result down to fit. Your min-width: 40em query is evaluated against that pretend width, so the mobile layout never appears.
The fix is one line in the document head:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-width sets the layout width to the real width of the device in CSS pixels. initial-scale=1 sets the starting zoom to 100%. Without this tag, nothing else in this tutorial works as described on a phone. The next chapter covers it in detail.
A complete minimal responsive page looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive starter</title>
<style>
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
.page {
padding: 1rem;
}
img {
max-width: 100%;
height: auto;
}
@media (min-width: 40em) {
.page {
max-width: 60rem;
margin: 0 auto;
padding: 2rem;
}
}
</style>
</head>
<body>
<main class="page">
<h1>Hello, any screen</h1>
<p>This text reflows to fit whatever width it is given.</p>
</main>
</body>
</html>Save this as an HTML file and open it in a browser. Drag the window narrower and wider. Below 40em (640px at the default font size), the content fills the width with small padding. Above it, the content is capped at 60rem and centered.
Common mistakes
These errors appear again and again when people start with responsive design.
- Forgetting the viewport tag. Symptom: the page looks like a shrunken desktop site on a phone, and media queries seem to be ignored.
- Designing for named devices. Writing queries for "iPhone" or "iPad" widths breaks as soon as a new device ships. Base your conditions on where your content needs to change.
- Fixed pixel widths on containers. A
width: 800pxelement overflows any screen narrower than 800px, and no media query can fix it unless you write one for every width. - Hiding content on small screens. Setting
display: noneon content for phones means mobile users get less. Rearrange or collapse content instead, unless it is truly decorative. - Testing only by resizing a desktop window. This misses touch input, real font rendering, and the viewport behavior above.
What comes next
The following chapters build this model step by step. You will set the viewport, read the syntax of a media query, query the width, combine conditions, and adopt a mobile-first workflow. After that you will choose breakpoints, size things fluidly, build grid layouts, handle images, and respond to user preferences and printing. The tutorial finishes with container queries and testing.
Exercise
A developer writes @media (min-width: 40em) rules for a mobile layout, but when she opens the page on a phone she sees a shrunken desktop layout and the rules never apply. The stylesheet has no syntax errors. What is the most likely cause, and what single line fixes it?
Show answer
The page is missing the viewport meta tag. Without it, mobile browsers report a virtual width of about 980px, so the min-width: 40em condition is evaluated against that wider value. Adding <meta name="viewport" content="width=device-width, initial-scale=1"> to the <head> makes the layout width equal the real device width.
Exercise
Name the three techniques in Ethan Marcotte's original definition of responsive design, and explain why a page that uses only the first two can still need fewer media queries than a page that uses only fixed widths with many media queries.
Show answer
The three techniques are fluid grids, flexible images, and media queries. Fluid grids and flexible images make the layout adapt continuously to any width, so the page already works between breakpoints. A fixed-width page only works at the exact widths it was written for, so it needs a new query for each width it must support and still breaks in between.