How to Create an Anchor Link to Jump to a Specific Part of a Page

An anchor link is a link that allows 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 scrolling) or to a specific section of another page.

Let’s see how to jump to a marked section of the page by using the <a> tag. It’s quite simple!

  1. Add an id attribute to your 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 underscores instead).
<a id="anchor-name">The section name here where you want to jump</a>

Alternatively you can also have the following types of anchors:

  1. anchor in a header: <h2 id="anchor-name">Section name</h2>
  2. anchor in an image: <img id="anchor-name" src="/images/imgname.jpeg"/>
  3. anchor in a paragraph: <p id="anchor-name">Paragraph name</p>
Remember that each id can appear only once in one page.
  1. Create a hyperlink using the id of the link target, preceded by #.
<a href="#anchor-name">Jump to the part of the page with “anchor-name” id </a>

Now you are ready! Just add the preferred text, and you will be able to fly through the page sections.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .main-content {
      height: 100vh;
      text-align: justify;
      }
    </style>
  </head>
  <body>
    <h2 id="Lorem_Ipsum">Lorem Ipsum</h2>
    <p class="main-content">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="#Lorem_Ipsum">top</a>.</p>
  </body>
</html>

How to Style the Jumping Anchor Link

It is also possible to give additional styling to the anchor where you jump. For that use, add a <style> tag in the <head> element and give styling to the section to be jumped using color and background properties.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .main-content {
      height: 100vh;
      text-align: justify;
      }	
      h2 {
      color: #1c87c9;
      }
      h2:target {
      color: white;
      background: #1c87c9;
      }
    </style>
  </head>
  <body>
    <h2 id="Lorem_Ipsum">Lorem Ipsum</h2>
    <div  class="main-content ">
      <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>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. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    </div>
    <p>Go to the <a href="#Lorem_Ipsum">top</a>.</p>
  </body>
</html>

Link to the Anchor from Another Web page

You can also link to your anchor link from other websites. To do that, add the URL followed by # and the anchor value. There are two types of this usage:

Link to Another Page in the Same Domain:

<a href="/learn-html/html-links.html#html_links">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

<!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#html_links">Go to the HTML Links in our website.</a></p>
    <p><a href="https://en.wikipedia.org/wiki/Main_Page#wikipedia">Go to the main page of Wikipedia.</a></p>
  </body>
</html>

Do you find this helpful?

Related articles