How to set an anchor in HTML?

An anchor is kind of like a special link you can put on a webpage. When you click on the anchor, it takes you to a different part of the same webpage.

It’s kind of like turning to a new page in a book, but without having to actually go to a different webpage. That way, you can quickly jump to the part of the webpage you want to see, instead of scrolling through a lot of stuff you don’t care about. Does that make sense?

Create the anchor and it’s hyperlink

To create an anchor in HTML, you can use the “<a>” element with the “href” attribute, like this:

<a href="#section">Go to Section</a>
  • This creates a hyperlink that will take a user to a section within the current page when clicked.
  • The “href” attribute specifies the target section of the page, and the target section is indicated using the “id” attribute on the element you want to link to, like this:
<h2 id="section">Section 1</h2>
  • The “id” attribute finally creates the anchor the link is adressed to.

Set anchors to navigate on a single HTML page

In the following example, we have a header with a navigation menu that links to the different sections within the main content area, which is structured using “<section>” elements with unique “id” values.

<!DOCTYPE html>
<html>
<head>
	<title>My Website</title>
	<style>
		/* Add styles for navigation links here */
	</style>
</head>
<body>
	<header>
		<h1>My Website</h1>
		<nav id="nav">
			<ul>
				<li><a href="#section1">Section 1</a></li>
				<li><a href="#section2">Section 2</a></li>
				<li><a href="#section3">Section 3</a></li>
			</ul>
		</nav>
	</header>
	<main>
		<section id="section1">
			<h2>Section 1</h2>
			<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.

Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna.</p>
<p><a href="#nav">Back to top</a></p>
		</section>
		<section id="section2">
			<h2>Section 2</h2>
			<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.

Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna.</p>
<p><a href="#nav">Back to top</a></p>
		</section>
		<section id="section3">
			<h2>Section 3</h2>
			<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.

Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna.</p>
<p><a href="#nav">Back to top</a></p>
		</section>
	</main>
</body>
</html>

Quick notes

  • An anchor is a clickable link that takes you to a different part of the same webpage.
  • To set an anchor, you create a unique identifier (id) for the content you want to link to.
  • You can then create an anchor link that points to that id with the <a> element and the href attribute.
  • The href value starts with a #, followed by the id of the content you want to link to.
  • For example: <a href=”#my-section”>Go to my section</a> links to the section with the id of my-section.
  • The content you want to link to must have its id set with the id attribute.
  • For example: <section id=”my-section”> sets the id of the section to my-section.

Leave a Reply

Your email address will not be published. Required fields are marked *