The structure of a page · Frontend: HTML, CSS and JavaScript
What is a web page actually made of?
Course contentsWhy you need it
Every website you have ever used is HTML underneath. Browsers draw it, search engines read it, and screen readers speak it aloud to people who can't see the screen. Get the structure right and styling and scripts become much easier; get it wrong and no amount of CSS will fix it.
The intuition
HTML describes what each piece of the page is, not what it looks like. You wrap content in tags:
<h1>My reading list</h1><h1> opens, </h1> closes, and together with the text between them they make an element. Elements go inside other elements, like boxes in boxes: a list <ul> holds list items <li>.
Some elements need extra information, given as attributes inside the opening tag:
<a href="https://example.com">a link</a>
<img src="cat.jpg" alt="A grey cat asleep on a sofa"><img> has no closing tag: it has no text inside.
Elements you'll use every day:
| Element | What it is |
|---|---|
h1 … h6 | headings, from most to least important |
p | a paragraph |
ul / ol + li | a bulleted / numbered list and its items |
a | a link |
img | an image |
header nav main footer | the big regions of a page |
Press Run: the Preview tab shows the page, and the Output tab shows its outline, the tree of elements the browser built.
Definition
Element
An opening tag, its content and its closing tag: <p>Hello</p>. Elements nest inside each other to form a tree.
Attribute
Extra information in the opening tag, written name="value": href on a link, src and alt on an image.
Semantic HTML
Choosing the element that says what something is (nav for navigation, button for a button) rather than a generic div. Search engines and screen readers depend on it.
Examples
A first page
Run it in the lessonA heading, a paragraph and a list. Run it, then compare the Preview with the Output outline.
<h1>My reading list</h1>
<p>Books I want to finish this year.</p>
<ul>
<li>Dune</li>
<li>The Hobbit</li>
<li>Sapiens</li>
</ul>- L1The most important heading on the page. Use one h1 per page.
- L3A bulleted list starts.
- L4Each li is one item, inside the ul.
- L7The list closes. Everything between <ul> and </ul> belongs to it.
Links and images
Run it in the lessonAttributes carry the address of a link and the file and description of an image.
<p>Read more on <a href="https://example.com">example.com</a>.</p>
<img src="cat.jpg" alt="A grey cat asleep on a sofa">- L1The link sits inside the paragraph. href is where it goes; the text between the tags is what people click.
- L2cat.jpg doesn't exist here, so the browser shows the alt text instead. That's exactly what alt is for.
The regions of a page
Run it in the lessonMost pages have the same skeleton. Semantic elements name each region.
<header>
<h1>Daily Notes</h1>
<nav>
<a href="/">Home</a>
<a href="/archive">Archive</a>
</nav>
</header>
<main>
<h2>Today</h2>
<p>Learned how HTML elements nest.</p>
</main>
<footer>
<p>Written by me</p>
</footer>- L1The top of the page: title and navigation.
- L3A group of links for getting around the site.
- L8The main content: what this page is about.
- L12The bottom of the page.
Common mistakes
Closing tags in the wrong order
Tags close in the reverse order they opened, like boxes: <p><a>…</a></p>, never <p><a>…</p></a>. The checker warns you about tags that are never closed.
Choosing headings by size
Pick h1–h6 by importance, not by how big they look. Size is CSS's job. Screen-reader users jump between headings to find their way.
Images without alt
Without alt, a screen reader just says "image", and nothing shows if the file fails to load. Describe what the picture shows; use alt="" only for pure decoration.
Quick quiz
1. Which element makes a clickable link?
Show the answer
Answer: A
<a> is the anchor (link) element; href holds the address. <link> connects a stylesheet in the page head.
2. Which line is nested correctly?
Show the answer
Answer: A
The element opened last closes first.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictCount the items
How many
<li>elements are on this page? Count before you run it. - ModifyFix the broken page
The shopping list should show three items, and the photo needs a description for people who can't see it. Fix the two problems the checker reports.
- WriteA profile page
Build a small profile page with:
- one
h1with a name - a
pabout the person - a
ulwith at least 3 hobbies - a link to
https://example.com - an image with alt text
- one
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| HTML | HTML | HyperText Markup Language: the elements that give a page its structure. |
| Tag | 标签 | <p> opens an element and </p> closes it. |
| Alt text | 替代文字 | A text description of an image, read aloud by screen readers. |