HTML and CSS · Chapter 11 of 12

Responsive design

By the end of this chapter you can make a page that works on a phone and on a laptop from one set of files: tell a phone the page's real width with the viewport meta, write the phone's layout first and add to it with media queries for bigger screens, keep images and long lines inside their column, and find what makes a page scroll sideways. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: CSS: the layout
  • 4 tasks
  • HTML and CSS
  • In the all labs plan

The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.

Why you need this

Datta has looked at the box office's phone: most people who call him about the launch say they are "looking at the poster on WhatsApp", and Sayali expects most visitors to the site to come from a phone, too. On a phone, Parda's page is either tiny, the whole laptop layout shrunk until the text cannot be read, or, with that fixed, too wide: the four columns of venue cards are slivers, the nav squeezes, the 960 px banner sticks out, and Datta's launch week page slides sideways under your thumb.

Everything so far was checked at one or two widths and written for the laptop. A page has to say what a phone should do, and the browser needs a way to apply some rules only on some screens.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    images.css: the viewport meta, and images that fit

    In the <head> of both pages, index.html and week.html, add Ira's viewport meta: a <meta> with name="viewport" and content="width=device-width, initial-scale=1". Then, in images.css, make every img at most 100% of its parent's width, with its height worked out from its width (auto), so that it keeps its shape. Keep the banner's width="960" and height="320" attributes in index.html.

    The Check opens index.html: at 400 px wide the banner, launch.svg, must be exactly as wide as its column (360 px) and a third as tall (120 px); at 1200 px wide it must be its own 960 x 320, never stretched bigger.

  2. Task 2

    venues.css: the venue grid at every width

    In venues.css, give the venue grid (the list with class cards, holding the twelve venue cards) a layout for each width, mobile first, with a gap of 16px across and down: one column under 600 px wide; two equal columns from 600 px wide; four equal columns from 900 px wide. Write the one-column layout with no media query, then a media query with a smallest width for each bigger layout.

    The Check counts the columns at 400, 599, 600, 899, 900 and 1200 px wide, and the page must never scroll sideways.

  3. Task 3

    nav.css: the nav on a phone

    In nav.css, lay out the header's four links (Home, Venues, Launch week, Contact, in the list .site-nav ul) for each width. Under 700 px wide: one under another, 8px apart, each link as wide as the list. From 700 px wide: all four side by side in one row, 8px apart, centred in the header.

    The Check measures the links at 400, 699, 700 and 1000 px wide.

  4. Task 4 · interview question

    Interview: week.css, the page that scrolls sideways

    Datta's week.html (Launch week in the nav) scrolls sideways at 400 px wide. Fix week.css so that at 400 px the page never scrolls sideways: the week's column (class week) is never wider than the screen and still at most 960px on a laptop; the share line (class share), one long web address with no spaces, wraps onto more lines; and the table, captioned The launch week, keeps all five columns and Datta's smallest width of 640px, and scrolls sideways inside its own box (class table-box), not the page. At 1000 px wide the page must not scroll sideways either, and the table shows whole in its box.

    Ira will ask: what does the viewport meta do, and why mobile first?

What you will learn

  • The viewport meta: <meta name="viewport" content="width=device-width, initial-scale=1">, and what a phone does without it.
  • Media queries: @media (min-width: 600px) { ... }, rules that apply only from a width on.
  • Mobile first: the phone's layout with no query, then more for bigger screens.
  • Fluid images: max-width: 100% and height: auto.
  • Nothing wider than the screen: fixed widths, long unbroken words and wide tables, and how to test at 400 and 1000 px.

More in CSS: the layout

Build "Responsive design" yourself

Parda is launching its first website for Pune's own cinemas and stages. You build it, page by page. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.