10 / 10 · Intro
Build a small page
A complete one-page site for a neighbourhood bakery fits in about 50 lines of HTML, and every element in it comes from the last nine units.
Jump to the 9 practice questionsYou will learn
- Plan the content before writing tags
- Build a whole page from header to footer
- Check it like a reviewer: headings, keyboard, validator
Before this: Semantic page structure
1Explain
Plan before you type
Before writing any HTML, write down what the page has to say, in plain words and in order. For a small bakery that might be: the name, a short menu of links, what is baked today, the opening hours, a picture of the bread, a way to sign up for news, and the address.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Corner Bakery</title>
</head>
<body>
<header>
<h1>Corner Bakery</h1>
<nav>
<ul>
<li><a href="#today">Today</a></li>
<li><a href="#hours">Hours</a></li>
<li><a href="#news">News</a></li>
</ul>
</nav>
</header>
<main>
<section id="today">
<h2>Baked today</h2>
<ul>...</ul>
</section>
<section id="hours">
<h2>Opening hours</h2>
<table>...</table>
</section>
<section id="news">
<h2>Get our news</h2>
<form>...</form>
</section>
</main>
<footer>
<p>12 Mill Street, Springfield</p>
</footer>
</body>
</html>The outline of the finished page, with the details left out.
2Explain
Example: from the plan to elements
Take the plan line by line and pick the element for each part before you write any tags.
- The name of the bakery is the main title of the page: h1, inside header.
- The short menu of links goes in nav.
- Each topic of the page, such as what is baked today and the news sign-up, becomes a section with an h2 and an id for the menu links.
- The days and times of the opening hours are rows and columns, so they become a table.
- The address goes last, in footer. Only now start typing tags.
3Problem
Where the opening hours go
The opening hours of the bakery belong in
Hint
It is part of the main content of this page.
Solution
Answer: a table
Days and times form rows and columns.
4Problem
The viewport line
Two lines in the head are new. <meta name="viewport" content="width=device-width, initial-scale=1"> tells phones to use the real screen width instead of pretending to be a desktop screen and shrinking the page; the CSS course explains it in the unit on responsive design. The menu is a list of links inside <nav>, which screen readers announce as navigation, list, 3 items.
What does the viewport meta tag do?
Hint
It is about phones and screen width.
Solution
Answer: Makes phones use the real screen width
Without it, phones shrink a desktop-width layout.
5Problem
A link to #hours
A menu link points to #hours. What must exist on the page?
Hint
A hash link needs a matching id.
Solution
Answer: An element with id="hours"
A fragment link jumps to the element with that id.
6Problem
Outside to inside
Order these from outside to inside: body, main, section, h2
- body
- h2
- section
- main
Hint
body holds everything that is drawn.
Solution
Answer:
- body
- main
- section
- h2
body contains main, which contains sections, each starting with an h2.
7Problem
Testing with the keyboard
When the page is written, go through it the way a careful reviewer would. Read only the headings: do they tell the story of the page? Press Tab from the top: can you reach every link and form field, in a sensible order, and can you see where the focus is? Look at every image: does its alt text say what would be lost without it, or is it empty because the image is decoration?
Which key do you press repeatedly to test keyboard access?
Hint
The key that moves focus from link to link.
Solution
Answer: Tab
Tab moves focus through links and fields in order.
8Problem
What the validator checks
What does the W3C HTML validator check?
Hint
It reads the markup, not the words.
Solution
Answer: That the HTML follows the rules of the standard
The validator reports markup errors such as a stray closing tag or a missing alt. Speed and spelling need other tools.
9Explain
Build it in small steps
Write the page in small steps and look at the result after each one: the skeleton first, then the header and menu, then one section at a time. If something looks wrong, you only have a few new lines to check. On your own computer, save the file as index.html and open it by double-clicking; the browser shows it straight from the disk.
Add one part at a time and look at the preview after each step.
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Corner Bakery</title></head>
<body>
<header><h1>Corner Bakery</h1></header>
<main>
<section id="today"><h2>Baked today</h2><p>Rye and sourdough.</p></section>
</main>
</body>
</html>10Explore
A menu as a list
Both menus work. With the list, a screen reader announces navigation, list, 2 items before reading the links; CSS can still lay the list out in one line.
Tap a choice: the code and the preview change together.
<header>
<h1>Corner Bakery</h1>
<nav><a href="#today">Today</a> <a href="#news">News</a></nav>
</header>Nothing is drawn: there is no visible content.
11Problem
The name of the home page file
You save the bakery home page on your computer. Which file name is the usual one?
Hint
The page mentions it in the step about opening the file on your own computer.
Solution
Answer: index.html
index.html is the usual name for the main page of a folder, and most web servers send it when an address ends with that folder. The .html ending marks the file as HTML.
12Problem
Building in steps
Put the building steps in order.
- Add the header and menu
- Add the footer
- Write the skeleton
- Add the sections one at a time
Hint
From the outside in, then from the top of the page down.
Solution
Answer:
- Write the skeleton
- Add the header and menu
- Add the sections one at a time
- Add the footer
The skeleton holds everything; then the page is built from top to bottom, looking at the preview after each step.
13Practice
The bakery page
Build the page from the comments. Set lang to en and the title to Corner Bakery. Move the bread picture into the today section and add image alt text. Give the email input the id email, connect its label to it with for="email", and use a submit button.
- lang is en on html
- title
- header with h1
- nav link to #today
- nav link to #news
- section#today with h2
- image with alt text
- list of 3 breads
- labelled field
- email input with id
- button
- footer
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<!-- 1. header: h1 "Corner Bakery" and a nav with links to #today and #news -->
<!-- 2. main with section id="today": h2, this picture with alt text, a list of 3 breads -->
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 120 80'%3E%3Cellipse cx='60' cy='48' rx='52' ry='26' fill='%23d9a35b'/%3E%3Cpath d='M30 40 l12 -10 M55 36 l12 -10 M80 40 l12 -10' stroke='%23fff3dd' stroke-width='4' stroke-linecap='round'/%3E%3C/svg%3E" width="120" height="80">
<!-- 3. section id="news": a form with a labelled email field and a submit button -->
<!-- 4. footer with the address -->
</body>
</html>Hint
Follow the four comments in order. The picture goes inside the today section, and the label for must match the input id.
A solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Corner Bakery</title>
</head>
<body>
<header>
<h1>Corner Bakery</h1>
<nav><a href="#today">Today</a> <a href="#news">News</a></nav>
</header>
<main>
<section id="today">
<h2>Baked today</h2>
<img alt="A round loaf of bread on a board" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 120 80'%3E%3Cellipse cx='60' cy='48' rx='52' ry='26' fill='%23d9a35b'/%3E%3Cpath d='M30 40 l12 -10 M55 36 l12 -10 M80 40 l12 -10' stroke='%23fff3dd' stroke-width='4' stroke-linecap='round'/%3E%3C/svg%3E" width="120" height="80">
<ul>
<li>Sourdough</li>
<li>Rye</li>
<li>Seeded loaf</li>
</ul>
</section>
<section id="news">
<h2>News by email</h2>
<form>
<label for="email">Email</label>
<input type="email" id="email" name="email">
<button type="submit">Subscribe</button>
</form>
</section>
</main>
<footer>
<p>12 Mill Street, Springfield</p>
</footer>
</body>
</html>14Explain
Common mistakes
<nav><a href="#hours">Hours</a></nav>
<h2>Opening hours</h2>
<p>Daily from 7 am.</p><nav><a href="#hours">Hours</a></nav>
<h2 id="hours">Opening hours</h2>
<p>Daily from 7 am.</p>A menu link to #hours does nothing until some element has id="hours".
15
Summary
- Plan the content in order, then pick an element for each part.
- Skeleton, header with nav, main with sections, footer.
- Review with headings, Tab, alt text, labels and the W3C validator.