Skip to content
KALANBLON
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.

Step 4 of 4
Text description of the animation
  1. With no tag, the browser draws the page as on a computer.
  2. It then shrinks the whole page to fit in 360 pixels: the text becomes unreadable.
  3. With the viewport tag, the page is drawn directly at the screen's real width.
  4. 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;
}
  • 1rem is the browser's base text size (usually 16 pixels): if the person has enlarged text in their settings, the page follows;
  • max-width: 40rem avoids 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
Look at the <head> first: which essential tag is missing?
Hint 2
Then look for fixed widths: the 600-pixel block and the 900-pixel image are wider than a 360-pixel screen.
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>
  1. The viewport tag was missing: the page was drawn at 980 pixels then shrunk.
  2. width: 600px becomes max-width: 600px: the block shrinks on a small screen.
  3. The image keeps its width and height attributes (they help the browser reserve space while loading), but the CSS max-width: 100%; height: auto stops it from overflowing.

Common mistakes:

  • Removing the image's width and height attributes: the page "jumps" when the image arrives, because the browser did not know its proportions.
  • Adding user-scalable=no to the viewport tag: it prevents zooming, and makes the page unusable for people with poor eyesight.

Summary

  1. 1The viewport tag (width=device-width, initial-scale=1) makes the browser use the screen's real width.
  2. 2Mobile first: a fluid column for the phone, enriched later for large screens.
  3. 3max-width, percentages and rem replace fixed pixel widths.
  4. 4A media query (min-width) adds rules when the screen is wide enough.
  5. 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.