From Zero to Senior · The LabالعربيةGitHub

Chapter 5

HTML: The Structure of a Page

8 examples · 3 projects · 5 exercises

In this chapter you will learn to

  • read and write HTML elements, tags and attributes
  • build the skeleton that every web page starts with
  • add headings, paragraphs, links, images and lists
  • give a page meaningful structure with header, nav, main and footer
  • build your first real page: a personal profile

Examples

5.2 The skeleton of every page

Every HTML page starts with the same few lines. In your code-from-zero folder, create a folder called chapter-05 and in it a file called first.html:

first.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My First Page</title>
  </head>
  <body>
    <h1>Hello, web!</h1>
    <p>This is my very first web page. I wrote it myself, in HTML.</p>
  </body>
</html>
first.html560px
Figure 5.2: Your first web page.

5.3 Text: headings and paragraphs

Most web content is text, and HTML has elements to give it structure. From now on, listings show only the <body>, because the <head> is the same skeleton every time; only the <title> changes. The full files are in the book’s code repository.

text.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cairo Travel Notes</title>
  </head>
  <body>
    <h1>Three Days in Cairo</h1>
    <p>Cairo is <strong>huge, loud and wonderful</strong>. I spent three days there and still saw only a <em>tiny</em> part of it.</p>

    <h2>Day 1: The pyramids</h2>
    <p>We arrived at Giza early, before the crowds. The Great Pyramid is even bigger than I imagined.</p>

    <h2>Day 2: Old Cairo</h2>
    <h3>Lunch</h3>
    <p>We ate koshari, a dish of rice, lentils, pasta and a spicy tomato sauce.</p>
  </body>
</html>
text.html560px
Figure 5.3: Headings, paragraphs and emphasis, with the browser’s default styles.

5.4 Links

Links are what make the web a web: pages connected to each other all over the world. The anchor element <a> creates a link, and its href attribute says where it goes:

links.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Useful Links</title>
  </head>
  <body>
    <h1>Places to learn</h1>
    <p>The best reference for HTML is <a href="https://developer.mozilla.org">MDN Web Docs</a>.</p>
    <p>Go back to <a href="first.html">my first page</a>.</p>
    <p>Open <a href="https://developer.mozilla.org/en-US/docs/Web/HTML" target="_blank" rel="noopener">the HTML guide</a> in a new tab.</p>
  </body>
</html>
links.html560px
Figure 5.4: Links are blue and underlined by default.

5.5 Images

The <img> element shows a picture. Download the file bit.svg from the book’s code repository into your chapter-05 folder, or use any picture you like:

image.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Meet Bit</title>
  </head>
  <body>
    <h1>Meet Bit</h1>
    <img src="bit.svg" alt="Bit, a small purple robot, waving" width="120" height="140">
    <p>Bit is the friendly robot who guides you through this book.</p>
  </body>
</html>
image.html560px
Figure 5.5: An image on the page.

5.6 Lists

HTML has two common kinds of list. Use <ul> (unordered list) when the order does not matter, and <ol> (ordered list) when it does. Inside both, each item is an <li>:

lists.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Lists</title>
  </head>
  <body>
    <h1>Making tea</h1>

    <h2>You need</h2>
    <ul>
      <li>Water</li>
      <li>Tea leaves</li>
      <li>A cup</li>
      <li>Sugar (optional)</li>
    </ul>

    <h2>Steps</h2>
    <ol>
      <li>Boil the water.</li>
      <li>Put the tea in the cup.</li>
      <li>Pour the water and wait three minutes.</li>
      <li>Add sugar if you like.</li>
    </ol>
  </body>
</html>
lists.html560px
Figure 5.6: The browser adds bullets and numbers for you.

5.7 Giving a page structure

Real pages have parts: a top area with the site’s name and menu, the main content, and a footer at the bottom. HTML has semantic elements for these parts. “Semantic” means they describe the meaning of their content, not its look:

structure.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bit’s Blog</title>
  </head>
  <body>
    <header>
      <h1>Bit’s Blog</h1>
      <nav>
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">Articles</a></li>
          <li><a href="#">About</a></li>
        </ul>
      </nav>
    </header>

    <main>
      <article>
        <h2>Why robots love HTML</h2>
        <p>Because every element has a clear job, and robots love clear jobs.</p>
      </article>
    </main>

    <footer>
      <p>© 2026 Bit. Made with HTML.</p>
    </footer>
  </body>
</html>
structure.html640px

5.8 A first look at forms

Forms let visitors send information: a search, a login, a sign-up. You will make forms come alive with JavaScript in Chapter 12, but the structure is pure HTML:

form.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Newsletter</title>
  </head>
  <body>
    <h1>Join the newsletter</h1>
    <form>
      <p>
        <label for="name">Your name</label>
        <input id="name" name="name" type="text" required>
      </p>
      <p>
        <label for="email">Your email</label>
        <input id="email" name="email" type="email" required>
      </p>
      <button type="submit">Sign me up</button>
    </form>
  </body>
</html>
form.html560px
Figure 5.8: A form with two fields and a button.

5.9 Project: your profile page

Let us combine everything into a real page: a profile of Sara, a new web developer. Replace her details with your own as you type.

profile.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>About Sara</title>
  </head>
  <body>
    <header>
      <img src="bit.svg" alt="Sara’s avatar: a smiling purple robot" width="90" height="105">
      <h1>Sara Khan</h1>
      <p>Beginner web developer from Alexandria</p>
      <nav>
        <a href="#about">About</a> |
        <a href="#skills">Skills</a> |
        <a href="#contact">Contact</a>
      </nav>
    </header>

    <main>
      <section id="about">
        <h2>About me</h2>
        <p>I started learning to code this year with <em>Code from Zero</em>. I love building small tools that make life easier.</p>
      </section>

      <section id="skills">
        <h2>Skills I am learning</h2>
        <ul>
          <li>HTML</li>
          <li>CSS</li>
          <li>JavaScript</li>
        </ul>
      </section>

      <section id="contact">
        <h2>Contact</h2>
        <p>Write to me at <a href="mailto:sara@example.com">sara@example.com</a>.</p>
      </section>
    </main>

    <footer>
      <p>Built by Sara, with HTML only.</p>
    </footer>
  </body>
</html>
profile.html560px
Figure 5.9: The profile page, with HTML only and no styling yet.
+ 1 files
bit.svg⤓ File

Practice projects

Project 5.1 · Easy

A recipe page

Write the page for a recipe you love, with a picture, a list of ingredients and numbered steps.

You will practise: the page skeleton, headings, paragraphs, strong, images with alt, and both kinds of list.

Steps
  1. Create a folder projects-05, and in it recipe.html with the full skeleton from Section 5.2.
  2. Add an <h1> with the recipe’s name, and a short paragraph that says how long it takes and how many people it serves. Mark the time with <strong>.
  3. Add a picture of the dish with <img>. Write an alt that describes it in one sentence, and give it a width and height.
  4. Add an <h2> “Ingredients” with an unordered list, and an <h2> “Steps” with an ordered list.
  5. Open the page in the browser, and then in the DevTools Elements panel. Is the tree what you expected?

Hint: the order of ingredients does not matter, so use <ul>; the order of steps does, so use <ol>. If you have no photo, use any picture, or soup.svg from the code repository.

Solution
recipe.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Lentil Soup</title>
  </head>
  <body>
    <h1>Grandma’s Lentil Soup</h1>
    <img src="soup.svg" alt="A bowl of orange lentil soup with lemon" width="160" height="120">
    <p>A warm, simple soup ready in <strong>30 minutes</strong>. Serves four.</p>

    <h2>Ingredients</h2>
    <ul>
      <li>1 cup of red lentils</li>
      <li>1 onion, chopped</li>
      <li>1 carrot, chopped</li>
      <li>1 teaspoon of cumin</li>
      <li>5 cups of water</li>
      <li>Salt and lemon</li>
    </ul>

    <h2>Steps</h2>
    <ol>
      <li>Wash the lentils.</li>
      <li>Cook the onion and carrot in a little oil for 5 minutes.</li>
      <li>Add the lentils, cumin and water, and boil for 20 minutes.</li>
      <li>Blend until smooth, add salt, and serve with lemon.</li>
    </ol>
  </body>
</html>
recipe.html520px
Project 5.1: a recipe page with HTML only.

Project 5.2 · Medium

A website for a small café

Build the one-page website of a neighbourhood café, with a proper semantic structure and a menu that jumps to each section.

You will practise: header, nav, main, section, footer, links to #id, and tel: links.

Steps
  1. Plan the page on paper first: draw boxes for the header, the three sections (menu, opening hours, address) and the footer.
  2. Write the <header> with the café’s name, a one-line description, and a <nav> with three links: #menu, #hours and #find.
  3. Inside <main>, write three <section>s, each with the matching id, an <h2>, and a list or a paragraph.
  4. In the address section, add a phone link: <a href="tel:+201000000000">. On a phone, tapping it starts a call.
  5. Finish with a <footer>. Then make the window small and click the menu links: the page jumps to each section.

Hint: the & sign has a special meaning in HTML, so write it as &amp;, for example Bean &amp; Book.

Solution
cafe.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bean & Book Café</title>
  </head>
  <body>
    <header>
      <h1>Bean &amp; Book Café</h1>
      <p>Coffee, cake and a quiet corner to read.</p>
      <nav>
        <a href="#menu">Menu</a> |
        <a href="#hours">Opening hours</a> |
        <a href="#find">Find us</a>
      </nav>
    </header>

    <main>
      <section id="menu">
        <h2>Menu</h2>
        <ul>
          <li>Coffee: 30</li>
          <li>Tea with mint: 15</li>
          <li>Chocolate cake: 45</li>
        </ul>
      </section>
      <section id="hours">
        <h2>Opening hours</h2>
        <ul>
          <li>Saturday to Thursday: 8:00 to 22:00</li>
          <li>Friday: 14:00 to 23:00</li>
        </ul>
      </section>
      <section id="find">
        <h2>Find us</h2>
        <p>12 Library Street, Alexandria. Call us on <a href="tel:+201000000000">010 0000 0000</a>.</p>
      </section>
    </main>

    <footer>
      <p>© 2026 Bean &amp; Book Café</p>
    </footer>
  </body>
</html>
cafe.html520px
Project 5.2: a café website with a semantic structure.

Project 5.3 · Challenge

A course sign-up form

Build a complete sign-up form with six kinds of field, each with a proper label.

You will practise: forms, label with for, input types, select, textarea, checkboxes and required.

Steps
  1. Create signup.html with a heading and a <form>.
  2. Add text, email and phone fields (type="text", "email" and "tel"), each with a <label> whose for matches the field’s id.
  3. Add a <select> for the student’s level, with three <option>s.
  4. Add a <textarea> for “Why do you want to learn?”, and a checkbox for agreeing to emails, with its label after it.
  5. Make the name and email required, and test: press the button with empty fields, then with an email that has no @.

Hint: on a phone, type="email" shows a keyboard with @, and type="tel" shows a number pad. Try it with the DevTools device toolbar, or on your own phone.

Solution
signup.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Course sign-up</title>
  </head>
  <body>
    <h1>Join the web course</h1>
    <form>
      <p>
        <label for="name">Full name</label><br>
        <input id="name" type="text" required>
      </p>
      <p>
        <label for="email">Email</label><br>
        <input id="email" type="email" required>
      </p>
      <p>
        <label for="phone">Phone</label><br>
        <input id="phone" type="tel">
      </p>
      <p>
        <label for="level">Your level</label><br>
        <select id="level">
        <option>Complete beginner</option>
        <option>I know some HTML</option>
        <option>I know HTML and CSS</option>
        </select>
      </p>
      <p>
        <label for="why">Why do you want to learn?</label><br>
        <textarea id="why" rows="3" cols="40"></textarea>
      </p>
      <p>
        <input id="agree" type="checkbox">
        <label for="agree">I agree to receive emails about the course</label>
      </p>
      <button type="submit">Sign me up</button>
    </form>
  </body>
</html>
signup.html520px
Project 5.3: a sign-up form with six kinds of field.
+ 1 files
soup.svg⤓ File

Exercises

Exercise 5.1 · Easy

Create favourites.html with a heading and an unordered list of your five favourite films, books or games.
The book's short answer
The page skeleton with an <h1> and a <ul> containing five <li>.
Show the solution
favourites.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My favourite books</title>
  </head>
  <body>
    <h1>My five favourite books</h1>
    <ul>
      <li>The Little Prince</li>
      <li>The Alchemist</li>
      <li>Robinson Crusoe</li>
      <li>One Thousand and One Nights</li>
      <li>Code from Zero</li>
    </ul>
  </body>
</html>
favourites.html640px

Exercise 5.2 · Easy

Add a link from favourites.html to profile.html, and a link back again.
The book's short answer
<a href="profile.html">My profile</a> in one page and <a href="favourites.html">My favourites</a> in the other.
Show the solution
favourites.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My favourite books</title>
  </head>
  <body>
    <h1>My five favourite books</h1>
    <ul>
      <li>The Little Prince</li>
      <li>The Alchemist</li>
      <li>Robinson Crusoe</li>
      <li>One Thousand and One Nights</li>
      <li>Code from Zero</li>
    </ul>
    <!-- A relative link: both files are in the same folder. -->
    <p><a href="profile.html">My profile</a></p>
  </body>
</html>
favourites.html640px
profile.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>About Sara</title>
  </head>
  <body>
    <header>
      <img src="../../examples/bit.svg" alt="Sara’s avatar: a smiling purple robot" width="90" height="105">
      <h1>Sara Khan</h1>
      <p>Beginner web developer from Alexandria</p>
      <nav>
        <a href="#about">About</a> |
        <a href="#contact">Contact</a> |
        <a href="favourites.html">My favourites</a>
      </nav>
    </header>
    <main>
      <section id="about">
        <h2>About me</h2>
        <p>I started learning to code this year with <em>Code from Zero</em>.</p>
      </section>
      <section id="contact">
        <h2>Contact</h2>
        <p>Write to me at <a href="mailto:sara@example.com">sara@example.com</a>.</p>
      </section>
    </main>
  </body>
</html>
profile.html640px

Exercise 5.3 · Medium

Write a recipe page for a dish you like, with an <h1>, an image with a good alt, a list of ingredients and numbered steps.
The book's short answer
An <h1>, an <img> with a descriptive alt, a <ul> of ingredients and an <ol> of steps.
Show the solution
recipe.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Ful Medames</title>
  </head>
  <body>
    <h1>Ful Medames for Breakfast</h1>
    <!-- A good alt describes what the picture shows, for someone who cannot see it. -->
    <img src="ful.svg" alt="A clay bowl of mashed fava beans topped with olive oil, cumin and chopped tomato" width="200" height="140">
    <p>Ready in <strong>10 minutes</strong>. Serves two.</p>

    <h2>Ingredients</h2>
    <ul>
      <li>1 can of cooked fava beans</li>
      <li>2 tablespoons of olive oil</li>
      <li>1 lemon</li>
      <li>1 clove of garlic, crushed</li>
      <li>1/2 teaspoon of cumin and a pinch of salt</li>
      <li>1 tomato, chopped</li>
    </ul>

    <h2>Steps</h2>
    <ol>
      <li>Warm the beans with a little of their water for 5 minutes.</li>
      <li>Mash them with a fork, leaving some whole.</li>
      <li>Stir in the garlic, cumin, salt and the juice of the lemon.</li>
      <li>Put it in a bowl, pour the oil over it and add the tomato.</li>
    </ol>
  </body>
</html>
recipe.html640px
ful.svg⤓ File

Exercise 5.4 · Medium

Open any large website, open DevTools and look at the Elements panel. Find its <header>, <nav> and <footer>. Does it use semantic elements, or only <div>s?
The book's short answer
It depends on the site. Many modern sites use semantic elements; older ones often use only <div>s with classes.
Show the solution
div-soup.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head><meta charset="utf-8"><title>The same page, with divs only</title></head>
  <body>
    <div class="header">
      <div class="title">The Daily Byte</div>
      <div class="menu"><a href="#">News</a> | <a href="#">Sport</a></div>
    </div>
    <div class="content">
      <div class="story"><div class="story-title">Bit learns to fly</div><div>…</div></div>
      <div class="side"><div class="side-title">Most read</div></div>
    </div>
    <div class="footer">© 2026</div>
  </body>
</html>
div-soup.html640px
semantic.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head><meta charset="utf-8"><title>A semantic page</title></head>
  <body>
    <header>
      <h1>The Daily Byte</h1>
      <nav><a href="#">News</a> | <a href="#">Sport</a></nav>
    </header>
    <main>
      <article><h2>Bit learns to fly</h2><p>…</p></article>
      <aside><h2>Most read</h2></aside>
    </main>
    <footer><p>© 2026</p></footer>
  </body>
</html>
semantic.html640px
count-tags.js⤓ File
Runs with Node.js: node count-tags.js
// Does a page use semantic elements, or only <div>s? This program counts them in two saved pages.
// In the browser, the same check is one line in the Console (see snippet.js).
const fs = require("node:fs");
const path = require("node:path");

const tags = ["header", "nav", "main", "section", "article", "aside", "footer", "div"];

for (const file of ["semantic.html", "div-soup.html"]) {
  const page = fs.readFileSync(path.join(__dirname, file), "utf8");
  const counts = tags.map((tag) => `${tag}: ${(page.match(new RegExp(`<${tag}[\\s>]`, "g")) || []).length}`);
  console.log(`${file} -> ${counts.join(", ")}`);
}
Recorded output$ node count-tags.js
semantic.html -> header: 1, nav: 1, main: 1, section: 0, article: 1, aside: 1, footer: 1, div: 0
div-soup.html -> header: 0, nav: 0, main: 0, section: 0, article: 0, aside: 0, footer: 0, div: 10
snippet.js⤓ File
Paste into the browser's Console
// Paste into the Console of any website: how many of each element does it use?
console.table(["header", "nav", "main", "section", "article", "aside", "footer", "div"]
  .map((tag) => ({ tag, count: document.querySelectorAll(tag).length })));

Exercise 5.5 · Challenge

Turn your profile page into a small three-page website: index.html (about you), projects.html and contact.html with a form. Every page should have the same <header> and <nav> linking to the other two.
The book's short answer
Three files that share the same <header> and <nav>; contact.html contains a <form> with labels, like form.html.
Show the solution
contact.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sara Khan: Contact</title>
  </head>
  <body>
    <header>
      <h1>Sara Khan</h1>
      <nav>
        <a href="index.html">About</a> |
        <a href="projects.html">Projects</a> |
        <a href="contact.html" aria-current="page">Contact</a>
      </nav>
    </header>
    <main>
      <h2>Write to me</h2>
      <form>
        <p>
          <label for="name">Your name</label>
          <input id="name" name="name" type="text" required>
        </p>
        <p>
          <label for="email">Your email</label>
          <input id="email" name="email" type="email" required>
        </p>
        <p>
          <label for="message">Message</label><br>
          <textarea id="message" name="message" rows="4" cols="40" required></textarea>
        </p>
        <button type="submit">Send</button>
      </form>
    </main>
    <footer><p>Built by Sara, with HTML only.</p></footer>
  </body>
</html>
contact.html640px
index.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sara Khan: About</title>
  </head>
  <body>
    <header>
      <h1>Sara Khan</h1>
      <nav>
        <a href="index.html" aria-current="page">About</a> |
        <a href="projects.html">Projects</a> |
        <a href="contact.html">Contact</a>
      </nav>
    </header>
    <main>
      <h2>About me</h2>
      <img src="../../examples/bit.svg" alt="Sara’s avatar: a smiling purple robot" width="90" height="105">
      <p>I am a beginner web developer from Alexandria. I started learning this year, and this small website is my first.</p>
    </main>
    <footer><p>Built by Sara, with HTML only.</p></footer>
  </body>
</html>
index.html640px
projects.html↗ Open page⤓ File
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Sara Khan: Projects</title>
  </head>
  <body>
    <header>
      <h1>Sara Khan</h1>
      <nav>
        <a href="index.html">About</a> |
        <a href="projects.html" aria-current="page">Projects</a> |
        <a href="contact.html">Contact</a>
      </nav>
    </header>
    <main>
      <h2>Projects</h2>
      <ul>
        <li><strong>A recipe page</strong>: ful medames, with a picture and numbered steps.</li>
        <li><strong>My favourite books</strong>: a list, linked to my profile.</li>
        <li><strong>This website</strong>: three pages that share one header.</li>
      </ul>
    </main>
    <footer><p>Built by Sara, with HTML only.</p></footer>
  </body>
</html>
projects.html640px