Published by Patrick Mutisya · 14 days ago
By the end of this lesson you will be able to create a bookmark (also called an internal link) in a web page by using the id attribute.
id attribute.#) followed by the id value.id attribute to the element that marks the start of that section.<a>) elsewhere on the page and set its href to #yourID.The following example shows a simple page with a table of contents that links to three sections further down the page.
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>Bookmark Example</title>
</head>
<body>
<h2>Table of Contents</h2>
<ul>
<li><a href="#section1">Section 1 – Introduction</a></li>
<li><a href="#section2">Section 2 – Details</a></li>
<li><a href="#section3">Section 3 – Summary</a></li>
</ul>
<h3 id="section1">Section 1 – Introduction</h3>
<p>...content for section 1...</p>
<h3 id="section2">Section 2 – Details</h3>
<p>...content for section 2...</p>
<h3 id="section3">Section 3 – Summary</h3>
<p>...content for section 3...</p>
</body>
</html>
| Issue | Consequence | How to Fix |
|---|---|---|
Missing id on target element | Link does nothing or jumps to top of page | Add a unique id attribute to the target element |
Duplicate id values | Browser may jump to the first matching element | Ensure each id is unique within the page |
Using spaces in id value | Invalid identifier; link fails | Use only letters, numbers, hyphens, underscores, and periods (no spaces) |
bookmark_test.html.<h2> called “My Favourite Recipes”.<h3> with a unique id and a paragraph of placeholder text.id.#id in the href.id.