Skip to content

Lesson 3 of 52HTML

Text and Links

Structure text with headings, paragraphs, emphasis and lists, then connect pages with links, paths, in-page anchors and mailto.

16 min read0/4 questions

Most of the web is text: articles, recipes, product descriptions, documentation. HTML gives you a small set of elements that describe what each piece of text is, and the browser, search engines and screen readers all rely on them. Then there are links, the feature that turned a pile of documents into a web. In this lesson you will build a recipe page with a proper outline, lists of ingredients and steps, and links that go to other pages, other sites, other spots on the same page, and even an email app.

Headings make an outline

HTML has six heading levels, h1 to h6. They are not size settings. They describe the outline of your page, like the chapters and sections in a book's table of contents.

  • h1 is the main title of the page. Use one per page.
  • h2 marks the main sections.
  • h3 marks subsections inside an h2, and so on down.
<h1>Lemon Drizzle Cake</h1>
  <h2>Ingredients</h2>
    <h3>For the sponge</h3>
    <h3>For the drizzle</h3>
  <h2>Method</h2>

(The indentation here is only to show the outline. You would normally write headings without it.)

Why does this matter? People using screen readers often jump from heading to heading to scan a page, much like sighted people skim the bold titles. Search engines use headings to understand what a page is about. So do not skip levels (an h2 followed straight by an h4), and never choose a heading level for its size. If you want a smaller heading, pick the right level and change its size with CSS later.

Quick check
You want a section title that looks small and subtle. The section is a main section of the page. Which element should you use?
Need a nudge? A hint rules out a wrong answer.

Paragraphs and emphasis

Wrap each paragraph of text in a p element. As you saw in the last lesson, the browser adds space between paragraphs automatically and ignores extra spaces inside them.

Inside a paragraph, two elements add meaning to words:

  • strong marks text as important, serious or urgent. Browsers show it in bold. "Do not open the oven door for the first 20 minutes."
  • em marks stress emphasis, the word you would say differently out loud. Browsers show it in italics. "I said two eggs, not three."

You may also see b and i. They exist and are valid, but carry weaker meaning: b is for text you want to draw attention to without extra importance (like a keyword), and i is for text in a different voice, such as a foreign phrase, a ship's name or a technical term. If you only want bold or italic as decoration, use CSS instead. If the words matter more, use strong or em.

Lists

HTML has two everyday lists. A ul (unordered list) is for items where order does not matter, like ingredients. An ol (ordered list) is for steps where order does matter, like a method. Every item inside either list is an li (list item).

HTML & CSS
<h1>Lemon Drizzle Cake</h1><p>A <strong>moist</strong> sponge with a <em>sharp</em> lemon topping.</p> <h2>Ingredients</h2><ul> <li>225g butter</li> <li>225g caster sugar</li> <li>4 eggs</li> <li>2 lemons</li></ul> <h2>Method</h2><ol> <li>Heat the oven to 180 degrees.</li> <li>Beat the butter and sugar together.</li> <li>Add the eggs one at a time.</li></ol>

Preview

Try these:

  • Add a fourth step to the method and watch it number itself.
  • Change <ol> to <ol reversed> (and the closing tag stays </ol>) to count down, or try <ol start="5">.
  • Swap the ul for an ol and back. The content is the same; only the meaning changes.

Nested lists

A list item can contain a whole list of its own. The inner list goes inside the li, not between two li elements:

<ul>
  <li>Sponge
    <ul>
      <li>Butter</li>
      <li>Sugar</li>
    </ul>
  </li>
  <li>Drizzle</li>
</ul>

A common mistake is putting the inner ul directly inside the outer ul. Only li elements are allowed as direct children of ul and ol, which a validator will point out.

Match the pairs0/4 matched

Match each piece of content to the best element.

Choose an item, then its match on the other side.

A link is an a element with an href attribute (hypertext reference) that says where it goes. The content is the clickable text.

<a href="https://developer.mozilla.org">MDN Web Docs</a>

Write link text that makes sense on its own. "Read the full allergy guide" tells everyone where they are going. "Click here" tells a screen reader user, who may be hearing a list of every link on the page, nothing at all.

Absolute and relative paths

The href value is a path, and there are two kinds.

An absolute URL includes everything: the scheme (https://), the domain and the path. Use it to link to other websites.

A relative path describes where a file is compared to the current page. Use it for pages on your own site, because it keeps working when you move the whole site to a new domain. Here is a small site:

A small recipe site
  • my-site/
  • index.htmlhome page
  • about.html
  • recipes/
  • cake.htmlyou are here
  • soup.html
  • images/
  • cake.jpg

From inside recipes/cake.html, these are the paths to other files:

You wanthref valueHow to read it
soup.html (same folder)soup.html"Next to me"
index.html (one folder up)../index.html.. means "go up one folder"
images/cake.jpg../images/cake.jpgUp one, then into images
The home page from anywhere/index.html or /A leading / starts at the site's root
Another websitehttps://example.comAbsolute URL

One catch: a path starting with / means the root of the website. When you open files straight from your disk with file://, the root is your whole drive, so those links break locally even though they work once the site is on a server. Relative paths without the leading slash work in both places.

Quick check
Using the file tree above, you are editing index.html. What href links to the soup recipe?
Need a nudge? A hint rules out a wrong answer.

Jumping within a page

Give any element an id, and you can link straight to it with # plus that id. This is how tables of contents and "back to top" links work. Ids must be unique on the page and should not contain spaces.

HTML & CSS
<style> section { min-height: 220px; border-top: 2px solid #ddd; }</style> <p>Jump to: <a href="#ingredients">Ingredients</a> | <a href="#method">Method</a></p> <section id="ingredients"> <h2>Ingredients</h2> <p>Butter, sugar, eggs, lemons.</p></section> <section id="method"> <h2>Method</h2> <p>Mix, bake, drizzle.</p> <a href="#top">Back to top</a></section>

Preview

Click the links in the preview to jump around. Then add a third section with id="serving" and a matching link in the list at the top. You can also combine a page and an id: cake.html#method opens the cake page and scrolls to the method. The special value #top scrolls to the top of the page even without an element of that id.

Opening in a new tab: target and rel

Add target="_blank" to open a link in a new tab:

<a href="https://example.com/pdf-guide" target="_blank" rel="noopener noreferrer">
  Download the guide (opens in a new tab)
</a>

Use this sparingly. People can open links in new tabs themselves, and surprise tabs break the Back button. Good reasons include links that would interrupt a task, such as a help page opened in the middle of filling in a form. When you do it, say so in the link text.

The rel attribute describes the relationship to the linked page. noopener stops the new page from getting control of your page through JavaScript. Modern browsers now apply this automatically to target="_blank" links, but many teams still write it for older browsers. noreferrer also hides which page the visitor came from. (The links in these previews may not open new tabs, because the preview runs in a locked-down sandbox.)

The href does not have to be a web page. A mailto: link opens the visitor's email app with the address filled in, and tel: offers to call a number on a phone:

<a href="mailto:hello@example.com">hello@example.com</a>
<a href="mailto:hello@example.com?subject=Cake%20order">Order a cake</a>
<a href="tel:+441234567890">Call us</a>

In the subject line, %20 stands for a space, because URLs cannot contain raw spaces. Showing the actual email address as the link text helps people who have no email app set up, because they can copy it instead.

HTML & CSS
<h2>Get in touch</h2><ul> <li><a href="mailto:hello@example.com">hello@example.com</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a">How the a element works (MDN)</a></li> <li><a href="#">A link that goes nowhere yet</a></li></ul>

Preview

Change the email address to your own, then add a ?subject=Hello to the end of the mailto: value. Try editing the second link's text so it reads naturally out of context.

Put it in order4 steps

From recipes/cake.html, build the path to images/cake.jpg step by step.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Key terms
heading level
h1 to h6, describing a heading's place in the page outline, not its size.
strong / em
Elements for important text and for stressed words. Shown bold and italic by default.
ul / ol / li
Unordered list, ordered list, and the list item element used inside both.
href
The attribute on a link that holds the destination: a URL, a path, an #id, or a scheme like mailto:.
relative path
A path worked out from the current file's location, such as ../index.html.
absolute URL
A complete address with scheme and domain, such as https://example.com/about.
fragment
The #id part of a link, which scrolls to the element with that id.