Sub topic 1
What responsive design is and why it matters
Prerequisites
This chapter assumes you know the following:
- Basic HTML structure: elements, attributes, and the
<head>and<body>sections. - CSS selectors, properties, and the cascade.
- The CSS box model.
- Common CSS units such as
px,em, andrem.
The problem responsive design solves
People open the same web page on phones, tablets, laptops, wide monitors, and printers. Screen widths range from about 320 pixels to more than 2,500 pixels. A layout fixed at 960 pixels forces phone users to pan and zoom. On a wide monitor, the same layout leaves large empty margins.
Responsive design is an approach in which one page adapts its layout to the space and the device it is shown on. You write one HTML document and one stylesheet. The browser applies different rules depending on conditions it measures.
The approach has three main parts:
- A flexible layout that uses relative units and layout systems such as grid and flexbox, so content can stretch and wrap.
- Flexible media, such as images that shrink to fit their container.
- Media queries, which apply CSS rules only when a condition is true.
The alternative is to maintain a separate mobile site. That means two codebases, two sets of content to keep in sync, and separate URLs. A responsive page avoids all three problems.
What a media query does
A media query is a condition attached to a block of CSS. If the condition is true, the browser applies the rules inside it. If not, it ignores them.
body {
font-size: 1rem;
}
@media (min-width: 40em) {
body {
font-size: 1.125rem;
}
}The first rule applies everywhere. The second applies only when the viewport is at least 40em wide. The viewport is the visible area of the page in the browser window. A later chapter covers the syntax in detail. For now, note that the query changes the styling and the HTML stays the same.
How the pieces fit together
The diagram shows how one page reaches different screens.
The viewport meta tag
Mobile browsers have a quirk. Many of them pretend the screen is about 980 pixels wide and then scale the page down to fit. Under that setting, a media query such as min-width: 40em would never behave as you expect on a phone. This tag in the document head turns the quirk off:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-width sets the layout width to the real width of the device. initial-scale=1 sets the starting zoom to 100%. Add this tag to every responsive page.
Common errors
- Forgetting the viewport meta tag, as described above.
- Designing for specific devices. Device sizes change every year, so base your layout on your content instead.
- Hiding content on small screens only because it is hard to fit. Phone users often need the same information as desktop users.
- Using fixed pixel widths on containers. These cause horizontal scrolling on narrow screens.
Where this tutorial goes next
The next chapters cover the viewport, the syntax of media queries, the mobile-first approach, and breakpoints. They then cover fluid units, grid and flexbox layouts, flexible images, user preference queries, print styles, container queries, and testing.
Exercise
A page looks correct when you narrow a desktop browser window, but on a real phone it appears as a tiny zoomed-out desktop layout and your @media (max-width: 40em) rules never apply. What is the most likely cause, and how do you fix it?
Show answer
The page is missing the viewport meta tag, so the phone uses a virtual width of about 980 pixels. Add <meta name="viewport" content="width=device-width, initial-scale=1"> to the <head>.
Exercise
Name the three main parts of responsive design described in this chapter.
Show answer
Flexible layouts built with relative units and tools such as grid or flexbox, flexible media such as images that shrink to fit, and media queries that apply rules conditionally.