Skip to content
Tutorials Center
My learning

Topics

Lesson 121 of 122 Β· Practice and Review

HTML Practice Exercises

Practice HTML with 26 graded exercises, from headings and links to tables, forms, dialogs and accessibility fixes. Each has a live starter editor and a solution.

Reading about HTML is the easy part. Writing it from a blank editor is where the knowledge sticks. This page gives you 26 exercises that cover the whole course, grouped by topic and graded from Beginner to Advanced.

Each exercise has a short task, a live editor with starter code, and a hidden solution. Try the task first, run it, and compare your result with the solution only when you are done or truly stuck. There is usually more than one correct answer: if yours is valid, semantic and accessible, it counts. The HTML Cheat Sheet is a good companion while you work.

How to use the editors. Type in the code panel and the preview updates. If you break something, the reset button brings back the starter code. Forms here never send data anywhere: where a form needs to be "submitted", the starter already prints the form data on the page.

Text and structure

These practice Headings and Paragraphs, Text Formatting and Quotations.

Exercise 1: Fix the heading levels (Beginner)

The page below uses headings for their size, not their meaning. Rewrite it so there is one <h1>, the two main topics are <h2>, and the subtopic under "Watering" is an <h3>. Turn the bold "headings" into real headings too.

Exercise 1: heading levels
Show solution
HTML
<h1>Growing Tomatoes</h1>
<p>A short guide for balcony gardeners.</p>
<h2>Watering</h2>
<p>Water deeply, twice a week.</p>
<h3>In hot weather</h3>
<p>Check the soil every morning.</p>
<h2>Feeding</h2>
<p>Use a tomato feed once flowers appear.</p>

Heading levels describe the outline of the page, so they must go down one step at a time; use CSS when you only want a different size.

Exercise 2: Meaningful inline text (Beginner)

Mark up this notice with the right inline elements: make "Do not" important with <strong>, stress "before" with <em>, mark "SKU" as an abbreviation for "Stock Keeping Unit", show Ctrl and P as keyboard keys, and write the date as a machine-readable <time> (2026-11-02).

Exercise 2: inline semantics
Show solution
HTML
<p><strong>Do not</strong> open the box <em>before</em> the delivery is signed for.</p>
<p>Every item has a <abbr title="Stock Keeping Unit">SKU</abbr> on its label.</p>
<p>Print the receipt with <kbd>Ctrl</kbd> + <kbd>P</kbd>.</p>
<p>Returns close on <time datetime="2026-11-02">November 2</time>.</p>

Each element adds meaning that browsers, search engines and screen readers can use; the visual style is a side effect you can change with CSS.

Exercise 3: Quotes and their sources (Beginner)

Turn the review into a <blockquote> with a cite attribute pointing to https://example.com/reviews/42, followed by a <p> that credits the reviewer and puts the magazine name in <cite>. Then make the short phrase in the last paragraph an inline <q>.

Exercise 3: quotations
Show solution
HTML
<blockquote cite="https://example.com/reviews/42">
  <p>Clear, warm and genuinely useful. The best guide to city cycling I have read.</p>
</blockquote>
<p>β€” Mira Okafor, <cite>Urban Wheels Magazine</cite></p>
<p>Our editor called it <q>a joy to ride along with</q>.</p>

The attribution sits outside the <blockquote> because it is not part of the quotation, and <q> adds the quotation marks for you.

Practice for Links, Link Targets and Special Links.

Exercise 4: A small navigation bar (Beginner)

Build a <nav> containing an unordered list of three links: Home (/), Menu (/menu) and Contact (/contact). Mark Menu as the current page with aria-current="page".

Exercise 4: navigation
Show solution
HTML
<nav aria-label="Main">
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/menu" aria-current="page">Menu</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>

A list tells screen-reader users how many links the menu has, and aria-current announces which one is the current page.

Add a small table of contents at the top with links that jump to the "Ingredients" and "Method" sections. Give each section heading an id to link to.

Exercise 5: same-page links
Show solution
HTML
<nav aria-label="Contents">
  <a href="#ingredients">Ingredients</a> Β· <a href="#method">Method</a>
</nav>

<h2 id="ingredients">Ingredients</h2>
<p>Flour, water, salt, yeast.</p>
<div style="height: 400px"></div>

<h2 id="method">Method</h2>
<p>Mix, rest, shape and bake.</p>

A link whose href is # plus an id scrolls to that element; see IDs for the naming rules.

Turn the email address into a mailto: link that pre-fills the subject "Booking question", and the phone number into a tel: link. Make the "Our partner" link open in a new tab.

Exercise 6: special links
Show solution
HTML
<p>Email: <a href="mailto:[email protected]?subject=Booking%20question">[email protected]</a></p>
<p>Phone: <a href="tel:+15550100">+1 555 0100</a></p>
<p>Our partner: <a href="https://example.org" target="_blank">example.org (opens in a new tab)</a></p>

Spaces in the subject are encoded as %20 (see URL Encoding), the tel: number has no spaces, and the link text warns users before a new tab opens.

Lists

Practice for Lists, Nested Lists and Description Lists.

Exercise 7: A countdown list (Beginner)

Turn these lines into an ordered list that counts down from 5 to 1, without typing the numbers yourself.

Exercise 7: ordered list options
Show solution
HTML
<h2>Top 5 trails</h2>
<ol reversed>
  <li>Pine Ridge Loop</li>
  <li>Lakeshore Path</li>
  <li>Old Mill Track</li>
  <li>Falcon Steps</li>
  <li>Summit Way</li>
</ol>

reversed makes the numbers count down, starting from the number of items; add start if you need a different first number.

Exercise 8: A nested packing list (Beginner)

Make an unordered list with two items, "Clothes" and "Gear". Under "Clothes" nest "Rain jacket" and "Wool socks"; under "Gear" nest "Headlamp" and "Water filter".

Exercise 8: nested lists
Show solution
HTML
<h2>Packing list</h2>
<ul>
  <li>Clothes
    <ul>
      <li>Rain jacket</li>
      <li>Wool socks</li>
    </ul>
  </li>
  <li>Gear
    <ul>
      <li>Headlamp</li>
      <li>Water filter</li>
    </ul>
  </li>
</ul>

The inner <ul> must sit inside an <li>; a list directly inside another list is invalid.

Exercise 9: A glossary (Intermediate)

Rewrite the glossary as a description list. "Latency" has two descriptions; give each its own <dd>.

Exercise 9: description list
Show solution
HTML
<h2>Glossary</h2>
<dl>
  <dt>Bandwidth</dt>
  <dd>How much data can move per second.</dd>
  <dt>Latency</dt>
  <dd>The delay before data starts to arrive.</dd>
  <dd>Also called lag in games.</dd>
  <dt>Packet</dt>
  <dd>A small chunk of data sent over a network.</dd>
</dl>

A <dl> pairs terms with descriptions, and one term may have several <dd> elements.

Tables

Practice for Tables, Table Headers and Captions and Colspan and Rowspan.

Exercise 10: An accessible data table (Intermediate)

Build a table with the caption "Lunch menu prices". It needs a header row (Dish, Small, Large) inside <thead>, and two body rows: Soup (4.50, 6.00) and Salad (5.00, 7.50). The dish names should be row headers. Use scope on every <th>.

Exercise 10: table headers
Show solution
HTML
<table>
  <caption>Lunch menu prices</caption>
  <thead>
    <tr>
      <th scope="col">Dish</th>
      <th scope="col">Small</th>
      <th scope="col">Large</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Soup</th>
      <td>4.50</td>
      <td>6.00</td>
    </tr>
    <tr>
      <th scope="row">Salad</th>
      <td>5.00</td>
      <td>7.50</td>
    </tr>
  </tbody>
</table>

With column and row headers marked, a screen reader can announce "Salad, Large, 7.50" for a single cell.

Exercise 11: A timetable with merged cells (Intermediate)

Complete the timetable: "Yoga" runs on Monday from 8:00 to 10:00 (two rows), and "Closed" covers both days at 10:00. Remove the cells that the merged ones replace.

Exercise 11: colspan and rowspan
Show solution
HTML
<table>
  <tr><th>Time</th><th>Monday</th><th>Tuesday</th></tr>
  <tr><th>8:00</th><td rowspan="2">Yoga</td><td>Spin</td></tr>
  <tr><th>9:00</th><td>Pilates</td></tr>
  <tr><th>10:00</th><td colspan="2">Closed</td></tr>
</table>

A cell with rowspan="2" fills a slot in the next row too, so that row needs one fewer cell; colspan="2" replaces two cells in its own row.

Images and media

Practice for Images, Figures, Responsive Images and Video.

Exercise 12: A captioned figure (Beginner)

Show /samples/sunset.svg (320 Γ— 200 pixels) inside a <figure> with the caption "Evening over the valley". Give it useful alt text, its width and height, and make it lazy-load.

Exercise 12: figure and img
Show solution
HTML
<figure>
  <img src="/samples/sunset.svg" alt="A yellow sun setting behind green hills under a pink sky"
       width="320" height="200" loading="lazy">
  <figcaption>Evening over the valley</figcaption>
</figure>

width and height reserve space before the image loads, and the alt text describes what the picture shows rather than repeating the caption.

Exercise 13: Let the browser pick the size (Intermediate)

Three versions of one picture exist: /samples/responsive-images-small.svg (400 px wide), /samples/responsive-images-medium.svg (800 px) and /samples/responsive-images-large.svg (1200 px). Use srcset with width descriptors and sizes so the image is full width on small screens and half the viewport from 40em up. Keep the small one as src.

Exercise 13: srcset and sizes
Show solution
HTML
<img src="/samples/responsive-images-small.svg"
     srcset="/samples/responsive-images-small.svg 400w,
             /samples/responsive-images-medium.svg 800w,
             /samples/responsive-images-large.svg 1200w"
     sizes="(min-width: 40em) 50vw, 100vw"
     alt="A harbor scene" width="400" height="250">

srcset lists the files and their real widths, sizes tells the browser how wide the image will be displayed, and the browser picks the smallest file that looks sharp.

Exercise 14: A video with fallbacks (Intermediate)

Build a <video> with controls, the poster /samples/media/fractal-poster.jpg, preload="metadata", a width of 320 and height of 180, and two sources: /samples/media/fractal.webm and /samples/media/fractal.mp4, each with its type.

Exercise 14: video sources
Show solution
HTML
<video controls width="320" height="180" preload="metadata"
       poster="/samples/media/fractal-poster.jpg">
  <source src="/samples/media/fractal.webm" type="video/webm">
  <source src="/samples/media/fractal.mp4" type="video/mp4">
</video>

The browser plays the first source whose type it supports; on a real site you would also add a captions <track>, as shown in Captions and Subtitles.

Page structure

Practice for Semantic Elements and Page Layout.

Exercise 15: Replace the div soup (Intermediate)

Replace every <div> with the semantic element that fits: page header, navigation, main content, a blog post, a sidebar and a page footer. Keep the class names out; the CSS already targets the new elements.

Exercise 15: semantic layout
Show solution
HTML
<header><h1>Field Notes</h1></header>
<nav aria-label="Main"><a href="/">Home</a> Β· <a href="/archive">Archive</a></nav>
<main>
  <article>
    <h2>Spotting herons</h2>
    <p>Look for still water at dawn.</p>
  </article>
</main>
<aside><h2>About</h2><p>A birdwatching diary.</p></aside>
<footer><p>Β© 2026 Field Notes</p></footer>

Landmarks such as <nav>, <main> and <aside> let screen-reader users jump straight to each region, and the page looks exactly the same.

Exercise 16: A dated article (Intermediate)

Mark up a blog post as an <article> with its own <header> (the title and a published date of 2026-05-03 in a <time> element), two paragraphs, and a <footer> with the author's name.

Exercise 16: article anatomy
Show solution
HTML
<article>
  <header>
    <h2>Five Bread Mistakes</h2>
    <p>Published <time datetime="2026-05-03">May 3, 2026</time></p>
  </header>
  <p>Most dense loaves come from impatience.</p>
  <p>Let the dough rise until it has doubled.</p>
  <footer>
    <p>Written by Lena Park</p>
  </footer>
</article>

<header> and <footer> can belong to any sectioning element, not just the page, and <time datetime> gives machines an exact date.

Forms

Practice for Forms, Input Types, Labels and Fieldsets, Select and Datalist and Form Validation.

Exercise 17: A sign-up form (Beginner)

Add three labeled fields: a required name, a required email (with the right type) and a password of at least 8 characters. Give each a name and a sensible autocomplete value. The script already prints what would be sent.

Exercise 17: labels and input types
Show solution
HTML
<label for="name">Name</label>
<input id="name" name="name" required autocomplete="name">

<label for="email">Email</label>
<input id="email" name="email" type="email" required autocomplete="email">

<label for="password">Password (8+ characters)</label>
<input id="password" name="password" type="password" minlength="8" required autocomplete="new-password">

The browser now blocks submission until every rule passes, and new-password invites password managers to suggest a strong one.

Exercise 18: Radio buttons and checkboxes (Intermediate)

Add a <fieldset> with the legend "Size" holding three radio buttons (S, M, L) that share one name, with M selected by default. Add a second fieldset, "Extras", with two checkboxes: "Gift wrap" and "Express delivery".

Exercise 18: fieldsets
Show solution
HTML
<fieldset>
  <legend>Size</legend>
  <label><input type="radio" name="size" value="s"> S</label>
  <label><input type="radio" name="size" value="m" checked> M</label>
  <label><input type="radio" name="size" value="l"> L</label>
</fieldset>

<fieldset>
  <legend>Extras</legend>
  <label><input type="checkbox" name="extras" value="gift"> Gift wrap</label>
  <label><input type="checkbox" name="extras" value="express"> Express delivery</label>
</fieldset>

Radios with the same name form one group where only one can be chosen; checked checkboxes each send their own name=value pair. See Checkboxes and Radio Buttons.

Exercise 19: Grouped options and suggestions (Intermediate)

Add a labeled <select> named "seat" with two <optgroup>s: "Window" (12A, 14A) and "Aisle" (12C, 14C). Then add a labeled text input named "meal" that suggests "Vegetarian", "Vegan" and "Gluten-free" through a <datalist>.

Exercise 19: select and datalist
Show solution
HTML
<label for="seat">Seat</label>
<select id="seat" name="seat">
  <optgroup label="Window">
    <option>12A</option>
    <option>14A</option>
  </optgroup>
  <optgroup label="Aisle">
    <option>12C</option>
    <option>14C</option>
  </optgroup>
</select>

<label for="meal">Meal preference</label>
<input id="meal" name="meal" list="meals">
<datalist id="meals">
  <option value="Vegetarian"></option>
  <option value="Vegan"></option>
  <option value="Gluten-free"></option>
</datalist>

A <select> limits the user to its options, while a <datalist> only suggests values and still accepts free text.

Exercise 20: A validated booking code (Advanced)

The booking code must be two uppercase letters, a dash and four digits (like KL-2048). Add required, a pattern and a title that explains the format, and connect a hint paragraph to the input with aria-describedby. Style invalid input with :user-invalid.

Exercise 20: pattern validation
Show solution
HTML
<style>input:user-invalid { outline: 2px solid #dc2626; }</style>

<label for="code">Booking code</label>
<input id="code" name="code" required
       pattern="[A-Z]{2}-[0-9]{4}"
       title="Two uppercase letters, a dash and four digits"
       aria-describedby="code-hint">
<p id="code-hint">Two letters, a dash and four digits, for example KL-2048.</p>

pattern must match the whole value, so no ^ or $ is needed, and aria-describedby makes screen readers read the hint when the field gets focus.

Interactive elements

Practice for Details and Summary, Dialog and Popover.

Exercise 21: An exclusive FAQ (Beginner)

Turn the three questions into <details> elements so only one answer can be open at a time, with the first one open when the page loads.

Exercise 21: details accordion
Show solution
HTML
<h2>FAQ</h2>
<details name="faq" open>
  <summary>Can I cancel?</summary>
  <p>Yes, any time before the trip starts.</p>
</details>
<details name="faq">
  <summary>Are pets allowed?</summary>
  <p>Small dogs travel free.</p>
</details>
<details name="faq">
  <summary>Is there Wi-Fi?</summary>
  <p>On all trains, at no charge.</p>
</details>

Sharing the name makes the group exclusive, and open sets which item starts expanded.

Exercise 22: A confirmation dialog (Intermediate)

When "Empty trash" is clicked, open the dialog as a modal. Inside it, add a form with method="dialog" and two buttons with the values cancel and empty. The script already prints which button closed it.

Exercise 22: modal dialog
Show solution
HTML
<dialog id="ask">
  <p>Permanently delete 12 items?</p>
  <form method="dialog">
    <button value="cancel">Cancel</button>
    <button value="empty">Empty trash</button>
  </form>
</dialog>

<script>
  const dialog = document.querySelector("#ask");
  document.querySelector("#empty").addEventListener("click", () => dialog.showModal());
  // … the close listener stays the same
</script>

showModal() traps focus inside the dialog and makes the page behind it inert; a button in a method="dialog" form closes it and sets returnValue.

Exercise 23: A popover menu (Intermediate)

Without any JavaScript, make the "Share" button toggle a popover that holds two links: "Copy link" (#copy) and "Email" (mailto:?subject=Look).

Exercise 23: popover
Show solution
HTML
<button type="button" popovertarget="share-menu">Share</button>

<div id="share-menu" popover>
  <ul>
    <li><a href="#copy">Copy link</a></li>
    <li><a href="mailto:?subject=Look">Email</a></li>
  </ul>
</div>

popovertarget connects the button to the popover by id; the browser handles opening, light dismiss and the Esc key.

Accessibility and scripting

Practice for Accessibility Basics, ARIA, The DOM, Data Attributes and Templates and Web Components.

Exercise 24: Fix the accessibility bugs (Intermediate)

This snippet has four problems: an image with no alt, a clickable <div> pretending to be a button, an input with no label, and a link that says only "click here". Fix all four.

Exercise 24: accessibility fixes
Show solution
HTML
<img src="/samples/sunset.svg" width="160" height="100" alt="Sunset over green hills">
<button type="button" onclick="alert('Saved')">Save</button>
<label for="q">Search the site</label>
<input id="q" type="search" placeholder="e.g. returns">
<p>Read our <a href="/privacy">privacy policy</a>.</p>

A real <button> is focusable and works with Enter and Space, a placeholder is not a label, and link text should make sense on its own.

Exercise 25: Filter with data attributes (Advanced)

Each product has a data-category. Make the buttons filter the list: clicking "Fruit" shows only fruit (hide the others with the hidden attribute), and "All" shows everything. Use element.dataset to read the categories.

Exercise 25: data-* and the DOM
Show solution
JavaScript
const items = document.querySelectorAll("#products li");

document.querySelectorAll("[data-filter]").forEach((button) => {
  button.addEventListener("click", () => {
    const filter = button.dataset.filter;
    items.forEach((item) => {
      item.hidden = filter !== "all" && item.dataset.category !== filter;
    });
  });
});

data-filter becomes dataset.filter, and setting the hidden property adds or removes the hidden attribute.

Exercise 26: Render a list from a template (Advanced)

Use the <template> to render one card per city in the cities array. Clone the template content for each city, fill in the name and population with textContent, and append the clone to #cards.

Exercise 26: template element
Show solution
JavaScript
const template = document.querySelector("#city-card");
const cards = document.querySelector("#cards");

for (const city of cities) {
  const card = template.content.cloneNode(true);
  card.querySelector("h3").textContent = city.name;
  const population = card.querySelector(".population");
  population.value = city.population;
  population.textContent = city.population.toLocaleString("en-US");
  cards.append(card);
}

template.content is never rendered on its own; cloning it gives you a fresh copy to fill, and <data value> keeps the raw number next to the formatted text.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. In Exercise 11, why does the 9:00 row need only one data cell after you add rowspan="2" to Yoga?

    A cell that spans two rows occupies a slot in the following row, so that row needs one fewer cell.
  2. 2. What makes radio buttons work as one group where only one can be selected?

    Radios with the same name belong to one group. A fieldset groups them visually and for screen readers, but does not make them exclusive.
  3. 3. Which attribute connects a button to a popover without JavaScript?

    popovertarget takes the popover's id; the browser then handles toggling, Esc and light dismiss.
  4. 4. Why should you replace a clickable <div> with a <button>?

    A native button gives you focus, keyboard activation and the right role for free; a div provides none of these.

Summary

  • You practiced every chapter of the course: text, links, lists, tables, media, layout, forms, interactive elements, accessibility and scripting.
  • Correct HTML is about meaning first: the right element, a label for every control, alt text for every image.
  • Built-in features such as pattern, <details name>, <dialog> and popover replace a lot of custom JavaScript.
  • If an exercise felt hard, revisit the linked lesson and try it again from a blank editor a day later.

Next: HTML Interview Questions β€” test your understanding with the questions interviewers ask most.