Let’s continue diving into the fundamentals of HTML. Today, we’ll discuss how to work with anchors in HTML. Don’t worry, we’re not talking about maritime adventures! In this lesson, you’ll learn what HTML anchors are, how to create links to them on a single page, and how to navigate to anchors on other pages. 🚀
This is an essential HTML feature that will be useful for creating well-structured web pages or single-page websites.
An anchor is a special marker used for quickly jumping to a specific section of a webpage. Think of it as the table of contents in a book: each section is linked to a particular page — that’s how anchors work in HTML.
Anchors are commonly used for:
Here’s a simple example:
<a href="#section1">Go to Section 1</a>
<h2 id="section1">Section 1</h2>
<p>This is the content of the first section...</p>
Clicking the link will take you directly to the section with the id section1.
To create an anchor, add the id attribute to an element that will serve as the marker:
<a id="myAnchor">Text or Heading</a>
Now, create a link to this anchor:
<a href="#myAnchor">Jump to the text</a>
⚠️ Note: The anchor name (id) must be in English and unique on the page.
To navigate to a specific section on another page, use the following format:
<a href="page.html#myAnchor">Go to a section on another page</a>
On the target page, create an anchor with the corresponding id:
<a id="myAnchor">Target Text</a>
index.html
<p>Read <a href="about.html#author">about the author</a></p>
about.html
<h2 id="author">About the Author</h2>
<p>This site is designed for those learning HTML from scratch!</p>
Pro Tip: Anchors are indispensable for enhancing user experience. With them, you can easily organize your content and simplify navigation for your visitors.
Subscribe to the Overcod blog updates to learn more about HTML! 😊
Knowledge Check: HTML Basics Test
63
HTML from Scratch: Adding Audio and Video to Your Website — For Beginners. Lesson #26
82
HTML from Scratch: The download Attribute in HTML5 (for downloading files). Lesson #25
150
HTML from Scratch: Commenting in HTML. Lesson #24
508
HTML from Scratch: The DOCTYPE Tag. Lesson #23
124
HTML from Scratch: How to Create a Horizontal Line in HTML. Lesson #10 1238
HTML from Scratch: How to Add Links in HTML. Lesson #6 614
HTML from Scratch: Basics for Beginners. Lesson #1 612
HTML from Scratch: Commenting in HTML. Lesson #24 508
HTML from Scratch: Creating Lists in HTML. Lesson #8 470
Leave a Reply