Show contents
Chapter 2 of 7 · 20 min
A phone-friendly page
By the end of this chapter, you'll be able to
- Add the viewport tag and explain its role
- Write a fluid layout that adapts to the screen width
- Use a media query to enrich the page on large screens
- Make images flexible
The viewport tag
With no instruction, a phone browser acts as if the screen were about 980 pixels wide, then shrinks the whole page to fit: that is what makes the text tiny. The viewport tag, placed in the <head>, tells it to use the screen's real width:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Aminata Tailoring</title>
</head>
width=device-width: the page width is the screen's width;initial-scale=1: no zoom on load.
The text keeps its size: you can read without zooming.
Text description of the animation
- With no tag, the browser draws the page as on a computer.
- It then shrinks the whole page to fit in 360 pixels: the text becomes unreadable.
- With the viewport tag, the page is drawn directly at the screen's real width.
- The text keeps its size: you can read without zooming.
A fluid layout
A fluid page does not use fixed pixel widths for its main blocks: it uses percentages, maximum widths and relative units. You first write the phone version, a single column, then enrich it for large screens: this is the mobile first approach.
body {
margin: 0;
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.5;
}
.content {
max-width: 40rem;
margin: 0 auto;
padding: 1rem;
}
img {
max-width: 100%;
height: auto;
}
1remis the browser's base text size (usually 16 pixels): if the person has enlarged text in their settings, the page follows;max-width: 40remavoids overly long lines on a large screen, without stopping the block from shrinking on a small one;img { max-width: 100% }stops an image from overflowing the screen.
Media queries
A media query applies rules only when a condition is true, for example when the screen is at least 48 rem (768 pixels) wide. On a tablet or a computer, you can then switch to two columns:
.services {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
@media (min-width: 48rem) {
.services {
grid-template-columns: 1fr 1fr;
}
}
JavaScript can also know the screen's width, but for layout, CSS is almost always enough. Here is this media query's logic, written in JavaScript to test it:
function columns(widthPx, textSizePx = 16) {
return widthPx >= 48 * textSizePx ? 2 : 1;
}
for (const [device, width] of [["phone", 360], ["large phone", 430], ["tablet", 768], ["computer", 1280]]) {
console.log(`${device} (${width} px): ${columns(width)} column(s)`);
}
phone (360 px): 1 column(s)
large phone (430 px): 1 column(s)
tablet (768 px): 2 column(s)
computer (1280 px): 2 column(s)
Mini-lab: a dressmaker's pageHTML · CSS
Preview
The preview is isolated from the page and runs no scripts · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: fixing a page that overflows
This page overflows on a phone. Find the three problems and fix them.
<head>
<title>Konaté Garage</title>
</head>
<body>
<div style="width: 600px">
<h1>Konaté Garage</h1>
<img src="workshop.jpg" alt="The garage workshop" width="900">
<p>Motorbike and car repairs, Monday to Saturday.</p>
</div>
</body>
Hint 1
<head> first: which essential tag is missing?Hint 2
Show the solution and common mistakes
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Konaté Garage</title>
<style>
.content { max-width: 600px; margin: 0 auto; padding: 1rem; }
img { max-width: 100%; height: auto; }
</style>
</head>
<body>
<div class="content">
<h1>Konaté Garage</h1>
<img src="workshop.jpg" alt="The garage workshop" width="900" height="600">
<p>Motorbike and car repairs, Monday to Saturday.</p>
</div>
</body>
- The
viewporttag was missing: the page was drawn at 980 pixels then shrunk. width: 600pxbecomesmax-width: 600px: the block shrinks on a small screen.- The image keeps its
widthandheightattributes (they help the browser reserve space while loading), but the CSSmax-width: 100%; height: autostops it from overflowing.
Common mistakes:
- Removing the image's
widthandheightattributes: the page "jumps" when the image arrives, because the browser did not know its proportions. - Adding
user-scalable=noto the viewport tag: it prevents zooming, and makes the page unusable for people with poor eyesight.
Summary
- 1The viewport tag (width=device-width, initial-scale=1) makes the browser use the screen's real width.
- 2Mobile first: a fluid column for the phone, enriched later for large screens.
- 3max-width, percentages and rem replace fixed pixel widths.
- 4A media query (min-width) adds rules when the screen is wide enough.
- 5Images stay within the screen with max-width: 100% and height: auto.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.