Test the Site on a Small Screen

The Elm Street Community Garden’s website has its full layout: a header with the name and the menu in one row, Tomás’s note in a sidebar on the home page, and the season’s events in a grid of cards. It was built and checked in a window 1280 pixels wide. However, most of the garden’s visitors look up the work day times on their phones, and nobody has looked at the site there yet.

If you’ve worked on the site before, it’s your own work. If you’re new to it, the starter folder below gives you exactly that site, so you start level with everyone else.

In this lesson, you’ll set up the course’s starter site, open it at a phone’s width and at 200% zoom, and list what breaks. You’ll also find out why one line in every page’s <head> decides which width a phone reports. Start by getting the site in front of you.

Set Up the Starter Site

Over this course, you’ll make the garden’s site work on every screen, from a 320-pixel phone to a wide monitor, a core step on the front-end developer roadmap. You’ll still write plain HTML and CSS, with no framework and no build step. Each section ends with exercises, and a project carries every change across the whole site.

Skip any step you’ve already done:

  1. Get a current version of Chrome or Edge.
  2. Install VS Code, then install Live Preview by Microsoft from its Extensions view, which you open with Ctrl+Shift+X, or Cmd+Shift+X on macOS.
  3. Download and unzip the course’s sample code ZIP file, and copy its 01-small-screen/garden/ folder to where you keep your projects.
  4. If garden/images/ is empty, copy in the six photos from garden-photos.zip, in the course’s supporting materials.
  5. Make a copy of the whole garden/ folder next to it, and name the copy garden-practice/.
  6. Open garden-practice/ in VS Code using File → Open Folder, then open index.html.
  7. Open the Command Palette with Ctrl+Shift+P, or Cmd+Shift+P on macOS, and run Live Preview: Show Preview (External Browser). If another browser opens, paste its address into Chrome or Edge.

The course’s supporting materials also hold the full-size photos, html-css-garden-photos-full. You won’t need them until the lessons on images.

You keep two copies of the site because the lessons change garden-practice/style.css directly, one slice of the site at a time. If you’ve worked on the site before, you may have tried rules in a separate practice stylesheet, which can only override what style.css says. Here, the desktop rules in the layout layer have to move into rules for wider screens, and a second file can’t move them. You’ll work in garden/ for the project.

The starter is the garden’s finished layout with style.css unchanged, plus three additions, each under a New in this course comment:

  • Every page: The menu grows from four links to seven, because Rosa wants the volunteers page and the two event pages one tap away.
  • plots.html: Each free plot’s Tended by cell holds a <span class="badge-free">Free</span>, and plot 19 is free this season too.
  • volunteers.html: Its <main> gets class="volunteers".

Switching to the starter is simpler, and it’s what the screenshots show. To keep your own folder instead, copy every marked block into your pages. Here’s the new menu, the same on every page apart from the paths:

Language: HTML
<nav class="site-nav">
  <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="plots.html">Plots</a></li>
    <li><a href="volunteers.html">Volunteers</a></li>
    <li><a href="events/upcoming.html">Events</a></li>
    <li><a href="events/work-day.html">Work Day</a></li>
    <li><a href="events/seed-swap.html">Seed Swap</a></li>
    <li><a href="about.html">About</a></li>
  </ul>
</nav>

Adding three more links to the menu sounds harmless at 1280 pixels. On a phone, it’s the first thing to break.

Check the Site at 320 Pixels

A check at one width tells you nothing about the others, so inspect the site on small screens too. The smallest width to test is 320 CSS pixels, about the width of a small phone. It’s also the width the web’s accessibility guidelines use for reflow, which means that at 320, content should fit without scrolling sideways.

A CSS pixel is a unit of measurement in CSS, and it isn’t always one dot on the screen. A phone’s screen packs two or three dots into each CSS pixel, so its width in CSS pixels is much smaller than its resolution.

Open DevTools on the home page and click the Toggle device toolbar button, at the top left, to turn on device mode. Keep Dimensions on Responsive, and type 320 into the width field. Here’s the home page:

The home page at 320 pixels, with the garden's name broken over three lines, the menu's links running off the right edge, and the welcome heading broken over four lines.
The Home Page Running Off a 320-Pixel Screen

The page is wider than the screen. The menu runs off the right edge after Volunteers, and the welcome’s heading breaks over four lines. It looks like it fills the screen, but Tomás’s note sits beside it, off the screen. Scroll sideways to see the hidden links and Tomás’s note.

To measure the page, open the Console and type window.innerWidth, the width in CSS pixels that the browser gives the page, including any part that sticks out past the screen. It shows a number near 600, not 320. The exact width depends on the font: system-ui is a different font on each operating system, so your number may be off by twenty pixels or so. In the video Find Breakpoints Where Content Breaks, for example, it’s 579.

To find what pushes the page wide, use the method for a sideways scrollbar: in the Elements panel, hover over the elements from the outside in, and watch which box sticks out. Check two pages this way, and note three desktop-first parts:

  • Home page, the header row: The garden’s name and seven links share one flex row that can’t wrap. The name breaks over three lines, while Work Day and Seed Swap each break over two. The row still needs about 600 pixels. It’s the widest part, so it sets the page’s width.
  • Home page, the sidebar: The .home grid keeps Tomás’s note in its fixed 250-pixel column, so the welcome gets what’s left.
  • Volunteers page, the profiles: Open volunteers.html at the same width. Each photo keeps its 160-pixel width beside the text.

The header hides how bad the other two are, because it makes every page about 600 pixels wide. To see a page without it, select <header> in the Elements panel and press Del. This changes only what the browser shows, not your file. Do it on each page in turn:

  1. Home page: Go back to index.html, delete its <header>, and type window.innerWidth again. It drops to 526, the width the sidebar grid needs. Reload the page to bring the header back.
  2. Volunteers page: Open volunteers.html again, delete its <header>, and check window.innerWidth. It drops to 320, and the text beside each photo is 88 pixels wide, about ten characters a line. Reload this page too.

You don’t need to note the events page or the plot map, because neither pushes the page wide. Once the header fits, the events page’s card grid drops to one column by itself, because its 250-pixel minimum fits the 272-pixel column. The plot map squeezes its labels rather than overflowing, and the project gives it a small-screen layout.

If a part of your own pages sticks out and you can’t tell which element is to blame, you can work through it with the mentor:

Zoom the Page to 200%

Some visitors don’t use a phone, but they enlarge the text to read it comfortably. The second check is 200% zoom, also from the accessibility guidelines, which say everything should still be usable when a visitor doubles the size.

Turn off device mode and press Ctrl+0, or Cmd+0 on macOS, to make sure the zoom is at 100%. Type window.innerWidth in the Console to measure the page’s width. Docked DevTools takes some of the window, so the page is narrower than the window itself. The numbers below assume a page 1280 pixels wide, so widen the window until window.innerWidth is 1280 if your screen allows.

Then click the page so it receives your keyboard shortcuts, and press Ctrl++, or Cmd++ on macOS, until the zoom reaches 200%. Each CSS pixel now takes up twice its previous size on the screen, so half as many fit. A page 1280 CSS pixels wide becomes 640. Type window.innerWidth again to see about half your first number.

At 640, the page fits without scrolling sideways, but the same parts strain. The header grows to 117 pixels tall, with the name over three lines and Work Day and Seed Swap over two. The welcome is 318 pixels wide beside Tomás’s note. This is browser zoom, not device mode, but the effect on the layout is the same as a narrower screen. Here’s the home page at 200% zoom:

The home page at 200% browser zoom, laid out 640 pixels wide, with the garden's name over three lines in a taller header and the welcome squeezed beside Tomás's note.
At 200% Browser Zoom the Header and Welcome Strain

So zooming is another way the page gets narrow, and the fixes you make for phones work here too. Press Ctrl+0, or Cmd+0 on macOS, to reset the zoom.

Keep the Viewport Tag on Every Page

A phone also decides on its own how wide to lay the page out, and device mode copies this. That’s what this line in every page’s <head> is for:

Language: HTML
<meta name="viewport"
  content="width=device-width, initial-scale=1">

Without it, a phone’s browser assumes the page was written for a desktop. It lays the page out in a layout viewport, an area about 980 CSS pixels wide, then shrinks the result to fit the screen.

With the tag, width=device-width starts the layout viewport at the screen’s width, and initial-scale=1 shows the page unzoomed. That’s why the garden’s header made the page scroll sideways at 320 instead of shrinking it. Here’s what a phone does with and without the tag:

Without the viewport tag, a phone 390 CSS pixels wide lays the page out 980 CSS pixels wide and shrinks it to fit. With it, the layout viewport is 390.
The Viewport Tag Makes the Layout Match the Screen

You can confirm this on Rosa’s phone, which is 390 pixels wide. Turn device mode back on, keep Dimensions on Responsive, and type 390 as the width. Check that the device type next to it says Mobile. If you don’t see a device type, open the toolbar’s More options menu and choose Add device type.

Now delete the viewport <meta> from garden-practice/index.html. Before you save, predict two things: what window.innerWidth will show and whether the header’s seven links will fit on one row. Then save, and reload the page if it doesn’t reload by itself.

window.innerWidth shows 980. The page is laid out as if on a desktop, so the header has room for the name and all seven links on one row. Then the phone shrinks the whole page to about 40 percent of its size to fit the 390-pixel screen. Here’s the 980-pixel layout it shrinks:

The home page laid out 980 pixels wide, with the header's name and seven links on one row and Tomás's note in a sidebar beside the welcome.
The 980-Pixel Layout a Phone Uses Without the Tag

Shrunk onto the phone, that tidy layout’s text is about 7 pixels tall on the screen, far too small to read without zooming in. The missing tag also breaks the styles you’ll write next. Your small-screen styles would still apply, because they apply at every width. But styles meant only for wider screens would see a page about 980 pixels wide on most phones, so they’d switch on too, override the small-screen ones, and bring back the desktop layout.

Put the <meta> back and save. window.innerWidth goes back to about 600, not 390, even though the tag is working again. The tag sets the layout viewport, but window.innerWidth also counts any content that sticks out past it, and the header sticks out.

To see the layout viewport alone, type document.documentElement.clientWidth. document.documentElement is the page’s root <html> element. Its clientWidth reports the layout viewport’s width without the overflow. It shows 390.

So the page is laid out at the screen’s width, as the tag asks, and the header row is too wide for it, which makes the page scroll sideways. That overflow is the problem the coming lessons fix, and the styles they add will respond to the 390, not the 600.

When you first wrote a page’s skeleton, this line was called optional, and in one sense it is, because a page without it is still valid HTML. That doesn’t mean the page works on phones.

Check that every page has it. Open a second VS Code window with File → New Window, open garden/ in it, and search the folder for name="viewport" with Ctrl+Shift+F, or Cmd+Shift+F on macOS. Each of the seven pages needs one match, with width=device-width, initial-scale=1 after it. Then run the same search in garden-practice/ to make sure index.html has its tag back.

To check that the widths with and without the viewport tag are clear in your head, the mentor can quiz you:

Before moving on, check that you have these:

  • Your list: The header row breaks and makes every page scroll sideways, the home page’s sidebar squeezes the welcome, and the volunteers page’s photos leave their text a sliver.
  • The viewport tag: One in each of the seven pages in garden/, and back in garden-practice/index.html.
  • The zoom: Back at 100%.

You now have a list of what breaks on a small screen, and you can’t fix any of it by picking one width, because the page has to work at 320 and still look right at 1280. In the next lesson, Write Mobile-First Media Queries, you’ll write styles that change with the width, starting with the home page’s sidebar.

Become a Member to join the conversation.