How to Add an Anchor Link to Jump to a Specific Part of a Page
An anchor link is a link, which allows the users to flow through a website page. It helps to scroll and skim-read easily. A named anchor can be used to link to a different part of the same page (like quickly navigating) or to a specific section of another page.
Creating an anchor link
Let’s see how to jump to a marked section of the page by using the <a> tag. It’s quite simple!
- Add an id attribute to the anchor element to give a name to the section of the page. The value of the attribute may be a word or a phrase (when using phrases remember not to have spaces, use dashes or underscores instead).
<a id="anchor-name">The name where you want to jump</a>
Alternatively you can also have the following types of anchors:
- anchor within a header: <h2 id="anchor-name">Section name</h2>
- anchor within an image: <img id="anchor-name" src="/images/imgname.jpeg"/>
- anchor within a paragraph: <p id="anchor-name">Paragraph name</p>
- Create a hyperlink by using the id of the link target, preceded by #.
<a href="#anchor-name">Jump to the part of the page with the “anchor-name” id </a>
Now, just add the preferred text, and you will be able to fly through the page sections.
Example of creating a jumping anchor link:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<a href="#section-2">Jump to section 2</a><br />
<a href="#section-3">Jump to section 3</a><br />
<a href="#section-4">Jump to section 4</a><br />
<h2 id="section-1">Section 1</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<h2 id="section-2">Section 2</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<h2 id="section-3">Section 3</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<h2 id="section-4">Section 4</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<p>Go to the
<a href="#section-1">top</a>.
</p>
</body>
</html>
How to style a jumping anchor link
It is also possible to give additional styling to the jumping anchor. For that, use a <style> tag in the <head> element and give styling to the section to be jumped by using color and background properties.
Example of styling a jumping anchor link:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
h2 {
color: #1c87c9;
}
h2:target {
color: white;
background: #1c87c9;
}
</style>
</head>
<body>
<a href="#section-2">Jump to section 2</a><br />
<a href="#section-3">Jump to section 3</a><br />
<a href="#section-4">Jump to section 4</a><br />
<h2 id="section-1">Section 1</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<h2 id="section-2">Section 2</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<h2 id="section-3">Section 3</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<h2 id="section-4">Section 4</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.
</p>
<p>Go to the
<a href="#section-1">top</a>.
</p>
</body>
</html>
Link to the anchor from another web page
You can link to your anchor link from other websites, as well. For that, add the URL followed by # and the anchor value. There are two types of such usage.
Link to another page in the same domain:
<a href="/learn-html/html-links.html#syntax-2">Go to the HTML Links in our website.</a>
Link from another website:
<a href="https://en.wikipedia.org/wiki/Main_Page#wikipedia">Go to the main page of Wikipedia .</a>
Example of linking to the anchor from another web page:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<h2 id="Lorem_Ipsum">Lorem Ipsum</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
<a href="/learn-html/html-links.html#syntax-2">Go to the HTML Links in our website.</a>
</p>
</body>
</html>
How to load a content page in an iframe and jump to a specific section of that page:
To clarify, imagine you have two iframes on the same page, and you want to create a link in the first iframe that, when clicked, loads a specific page in the second iframe and jumps to a specific section of that page. Here's how you can achieve this:
First, you need to give the second iframe an ID so that you can target it from the first iframe. For example, let's say you give it an ID of "iframe2":
<iframe id="iframe2" src="content-page.html"></iframe>
Next, in the first iframe, you can create a link that loads the content page in the second iframe and jumps to a specific section using the target attribute. For example, if you want to jump to a section with an ID of "section2" on the content page, you can use this code:
<a href="content-page.html#section2" target="iframe2">Load Content Page and Jump to Section 2</a>
This code creates a hyperlink that, when clicked, loads the "content-page.html" in the second iframe with the ID "iframe2", and jumps to the section with an ID of "section2" on that page.
Sure, here's a full example of how to load a content page in an iframe and jump to a specific section of that page:
<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<!-- First iframe -->
<h2>Section 1</h2>
<p>This is the first iframe:</p>
<iframe id="iframe1" src="https://www.example.com"></iframe>
<!-- Second iframe -->
<h2>Section 2</h2>
<p>This is the second iframe:</p>
<iframe id="iframe2" src=""></iframe>
<!-- Link to load content page in second iframe and jump to section -->
<p>Click the button to load the content page in the second iframe and jump to the section:</p>
<button onclick="loadContentPage()">Load Content Page and Jump to Section 2</button>
<!-- JavaScript function to load content page and jump to section -->
<script>
function loadContentPage() {
// Get reference to the second iframe
var iframe2 = document.getElementById("iframe2");
// Set the source of the second iframe to the content page with the desired section ID
iframe2.src = "https://www.example.com/content-page.html#section2";
}
</script>
</body>
</html>
In this example, we have two iframes on the page: "iframe1" and "iframe2". We also have a button that, when clicked, loads a content page in the second iframe and jumps to a section with an ID of "section2" on that page.
To achieve this, we use JavaScript to set the source of the "iframe2" to the desired URL with the section ID appended to the end. When the button is clicked, the "loadContentPage()" function is called, which gets a reference to the "iframe2" using its ID, and sets its source to "https://www.example.com/content-page.html#section2".