CHAPTER 45 · HTML

Small-screen pages: flexible content and a simple media query

Keep one useful page readable at different widths. Connect viewport settings with flexible images, sensible text and a small CSS layout change.

Download example files (ZIP)

The same notice in a narrow notebook

A large classroom notice may need a different arrangement when printed on a small card. Its information should stay useful. Responsive design means arranging a page so it works across available sizes and other relevant conditions. It is an approach, not a special HTML tag or a framework you must install.

Start with a readable single column. Keep the same information and a sensible source order. Do not create a separate copy of the explanation for every phone model.

Begin with the viewport setting

The viewport is the area the browser uses to show the page. The familiar meta name=viewport setting width=device-width, initial-scale=1 helps mobile browsers lay out the page using the device width rather than a much wider virtual page that is scaled down.

This is a starting instruction, not a complete responsive layout. A hard-coded wide box can still overflow. Keep zoom available; people may need larger text. In an embedded preview, width-dependent rules use the preview frame's viewport, which is narrower than the outer browser window.

Let paragraphs and images fit

Ordinary paragraphs can wrap into shorter lines. Avoid putting long prose inside pre or using repeated non-breaking spaces to force alignment. The image example has width and height attributes to reserve its aspect ratio; CSS max-width:100% keeps it from exceeding its container and height:auto preserves its proportions.

This sizing rule does not download a smaller image file. Srcset and picture, covered earlier, address resource selection. Do not stretch an image into a different shape by forcing unrelated width and height values.

Read the small sizing rules

Main has max-width:44rem, which caps the reading width without forcing that width onto a smaller screen. Margin:0 auto leaves no vertical margin from that declaration and shares spare horizontal space to centre the block. Body margin:1rem leaves an outer gap. Rem relates to the root text size.

The long practice identifier uses overflow-wrap:anywhere. This permits a break inside the long run when necessary rather than pushing the whole page wider. It changes wrapping, not the stored identifier. Use such a rule where long strings really can occur; do not split every normal word by habit.

A media query asks a conditional question

A media query applies some CSS only when a condition matches. Our example asks whether the layout viewport is at least 48rem wide. Below that width, the two steps appear in a single column. At or above it, they appear in two equal columns. This threshold is called a breakpoint.

Example fragment — use inside a complete program
@media (min-width: 48rem) {
  .practice-steps {
    grid-template-columns: 1fr 1fr;
  }
}

It is a teaching threshold chosen for this content, not a rule that every tablet starts at one fixed size. For media queries, relative font-size units use the initial font-size basis; do not assume changing a parent's font-size moves this breakpoint.

Read the grid example without guessing

Display:grid establishes a grid layout for the direct children. Gap:1rem leaves space between them. The base rule has no explicit pair of columns, so these items form a single column. The wide rule requests 1fr 1fr: two equal shares of the available column space. Min-width:0 on each section lets it shrink within its grid area.

The source still lists Prepare a file before Explain the result. We are changing presentation, not the learning sequence. These introductory rules are enough for this example; the full CSS course will explain layout in more depth.

Test between the neat screenshots

Resize the saved page from narrow to wide and back. Check widths just below and above the breakpoint as well as an ordinary phone-sized view. Read the paragraphs, reach links with the keyboard and try a long string. Increase text size or browser zoom as another check. A screenshot at one width cannot prove a page is responsive.

Do not hide essential text just to make everything fit. A wide data table may need its own labelled scrolling region, as shown earlier. Page-wide overflow and deliberate scrolling inside a code box are different situations.

What this chapter does not claim

A flexible layout does not automatically make every image small, every interaction touch-friendly or every colour readable. Check image file weight, text contrast, focus visibility and usable controls too. Avoid guessing the visitor's device solely from screen width.

No CSS framework or script is needed for these two examples. Later accessibility and testing lessons will build on the same idea: preserve usable content, then test actual behaviour.

Follow the idea

Fit the available space
  1. 1Start with a readable column
  2. 2Let text and images fit
  3. 3Apply a query when space allows
  4. 4Test widths, zoom and reading order

A viewport tag starts the setup; flexible content and real checks complete it.

Complete program: flexible-notice.html

Save this as flexible-notice.html. Open it in your browser.

flexible-notice.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Flexible Practice Notice</title>

    <style>
      body {
        margin: 1rem;
        color: #202c3a;
        font-family: Arial, sans-serif;
        line-height: 1.6;
      }
      main {
        max-width: 44rem;
        margin: 0 auto;
      }
      img {
        max-width: 100%;
        height: auto;
      }
      .practice-address {
        overflow-wrap: anywhere;
      }
    </style>
  </head>
  <body>
    <h1>A notice that fits its space</h1>
    <main>
      <p>Keep the words readable instead of shrinking the whole page.</p>
      <img
        src="images/study-desk.svg"
        alt="An open notebook beside a laptop on a study desk"
        width="360"
        height="220"
      />
      <p class="practice-address">
        Practice identifier: notebook-workshop-room-B204-afternoon-preparation-checklist-2027
      </p>
    </main>
  </body>
</html>
Browser previewDocument title: A Flexible Practice Notice

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: a readable single-column notice whose content width is capped on a large screen. The image shrinks within narrow space without changing shape. The long practice identifier can wrap instead of widening the page. Keep the supplied images folder at the shown path.

Complete program: responsive-cards.html

Save this as responsive-cards.html. Open it in your browser.

responsive-cards.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Practice Cards for Different Widths</title>

    <style>
      body {
        margin: 1rem;
        color: #202c3a;
        font-family: Arial, sans-serif;
        line-height: 1.6;
      }
      main {
        max-width: 44rem;
        margin: 0 auto;
      }
      img {
        max-width: 100%;
        height: auto;
      }
      .practice-address {
        overflow-wrap: anywhere;
      }
      .practice-steps {
        display: grid;
        gap: 1rem;
      }
      .practice-steps section {
        min-width: 0;
        border: 1px solid #7990aa;
        padding: 1rem;
      }
      @media (min-width: 48rem) {
        .practice-steps {
          grid-template-columns: 1fr 1fr;
        }
      }
    </style>
  </head>
  <body>
    <h1>Two steps for your next practice</h1>
    <main>
      <p>The same two steps stay in the same reading order at every width.</p>
      <div class="practice-steps">
        <section>
          <h2>Prepare a file</h2>
          <p>Save a short page and give it a clear filename.</p>
        </section>
        <section>
          <h2>Explain the result</h2>
          <p>Open the page, test one change and describe what happened.</p>
        </section>
      </div>
    </main>
  </body>
</html>
Browser previewDocument title: Practice Cards for Different Widths

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result in a saved browser page: one column at narrow widths and two equal columns once the 48rem query matches. The text and reading order stay the same. The embedded preview may stay single-column because the query measures the frame, not the surrounding browser.

Problem and solution

Problem: adding viewport metadata did not stop a page from scrolling sideways.

Solution: find the element that is too wide. Replace an unnecessary fixed width with flexible sizing, let long strings wrap appropriately and resize oversized images. Do not disable zoom or hide essential content as a shortcut.

Common mistakes and useful tips

  • Viewport metadata does not repair every fixed width.
  • Keep image proportions when resizing.
  • Do not use spaces or line breaks to fake columns.
  • Choose breakpoints for content rather than brand-name devices.
  • Keep the same logical reading order.
  • Check several widths and preserve zoom.

Practice questions

  1. What does responsive design aim to do?
  2. Does max-width:100% choose a smaller image file?
  3. Why use height:auto with a resized image?
  4. When does the example have two columns?
  5. Does a two-column layout change the source order?
  6. Save both programs and test a narrow and wide window. Look for side scrolling and compare reading order.
Check your practice answers
  1. Keep a page useful at different available sizes and conditions.
  2. No. It limits displayed size. Resource selection is a separate job.
  3. To preserve its proportions.
  4. When its layout viewport matches min-width:48rem.
  5. No. The two sections remain in their original order.
  6. Content should fit normally, long identifiers can wrap, and the second program changes columns only at its query threshold while preserving the same content.

Interview questions

Is a responsive page a separate mobile copy?

Not necessarily. One well-structured page can adapt its presentation with CSS while retaining the same content.

How do viewport metadata and media queries differ?

Metadata helps establish the mobile layout viewport. Media queries conditionally apply CSS based on conditions such as its width.

Why is one screenshot insufficient?

Overflow and awkward wrapping can appear between selected widths or with different text and zoom. Test actual resizing and interactions.

Why avoid hiding important content on phones?

Readers still need the same essential information. Adjust layout and wrapping rather than removing the task's meaning.

Quick revision

  • Begin with a readable single column.
  • Combine viewport metadata and flexible content.
  • Use media queries for content-driven changes.
  • Test widths, wrapping, reading order and zoom.