LilyTech

Anchor Links: How to Create Them in HTML

Đoàn Lê Minh AnhReviewed byHoàng LinhContent managerLê Mạnh

Anchor links in HTML: A guide to creating links that take visitors to specific page sections

When a web page contains a lot of content, readers often want to go straight to the information they need instead of scrolling through the entire page. Anchor links make this possible by taking users to a specific location on the same page or another page.

This article explains what anchor links are, how the id and href attributes work, and how to create anchor links in HTML step by step. You’ll also learn how to check for and fix common issues so your links work reliably across your website.

An anchor link is a link that takes users to a predefined point on a web page. The destination is usually a heading or a section of content with an id attribute. When someone clicks the link, the browser finds the element with the matching identifier and scrolls the page to it.

Marketing specialist illustrating how anchor links take readers to the right location on a web page
Anchor links help readers quickly find the content they need on a long web page.

A standard link uses an a tag with an href attribute to point to another page. With an anchor link, the href value includes a hash symbol (#) followed by the id of the destination. For example, #pricing points to an element with id="pricing".

Anchor links are often used in tables of contents for long articles, “View pricing” buttons, “Back to top” links, and company overview pages with multiple content sections. They help readers navigate more easily and reduce the steps needed to find information.

To create an anchor link, you need two elements: a destination element with an id and a link whose href points to that id. The names in the two attributes must match.

Developer illustrating the basic HTML structure of an anchor link pointing to a destination element
An anchor link consists of a destination element with an id and a link with an href pointing to that identifier.

For example, suppose you want to link to the “Pricing” section on the same page. You could write the destination element as follows: <h2 id="pricing">Pricing</h2>. The link to that location would look like this: <a href="#pricing">View pricing</a>.

When a reader clicks “View pricing,” the browser finds the element with the id pricing and brings it into view. The # in the href tells the browser that the link destination is an identifier on the page, rather than a standard web address.

The id attribute should follow a few guidelines:

  • Each id should be unique on a page. Do not assign the same value to multiple elements.
  • Use lowercase letters, numbers, and hyphens to make names easy to read, such as payment-methods.
  • Do not include # in the id value; use it only in the href.
  • Choose a descriptive name that reflects the destination content, making links easier to manage on pages with many of them.

You can create anchor links in any HTML editor. The basic process is to choose a destination, add an identifier, create the link, and check the result.

Web specialist creating anchor links in HTML step by step at the office
Choosing a destination, adding an id, creating an href, and testing it in sequence helps ensure the anchor link works correctly.
  1. Choose where you want to take the reader. Select the content users need to reach, such as a “How to register,” “Frequently asked questions,” or “Contact information” heading.
  2. Add an id attribute to the destination element. Give the relevant HTML tag a short, descriptive identifier. For example: <h2 id="how-to-register">How to register</h2>.
  3. Create the link. At the desired location, use an a tag and set href to a hash followed by the id. For example: <a href="#how-to-register">See how to register</a>.
  4. Check that the values match. Make sure the text after the # exactly matches the id, including letters, numbers, and hyphens. HTML distinguishes between uppercase and lowercase letters in these values.
  5. Open the page and test the link. Click the link in a browser. If the page scrolls to the correct content, the link is set up properly. Test it on both desktop and mobile.

To create a table of contents, add a list of links at the beginning of the article and assign a corresponding id to each heading. For example, the link <a href="#common-issues">Common issues</a> points to the heading <h2 id="common-issues">Common issues</h2>. This approach is particularly useful for long guides, product documentation, and policy pages.

Anchor links can also point to a location on another page. To do this, combine the page address with the id. For example, a link might use href="https://example.com/services#pricing". When clicked, the browser opens the specified page and finds the element with id="pricing".

Specialists discussing anchor links between pages and website navigation experience
Combining a URL with an id lets an anchor link open another page and take users to the right section.

If your website has a fixed navigation bar at the top of the screen, it may cover the destination after the browser scrolls to it. You can address this with the CSS scroll-margin-top property on headings or destination elements. For example, the rule h2 { scroll-margin-top: 80px; } adds space when scrolling to a heading. This CSS is not a required part of an anchor link, but it can improve the user experience.

Another option is smooth scrolling with CSS, such as html { scroll-behavior: smooth; }. This makes the movement to the destination feel more gradual. However, consider the needs of users who prefer reduced motion, and test the design in practice before applying the setting widely.

In a content management system, you may not need to edit the HTML directly. Many editors let you select text, add an internal link, and enter the anchor name. Some platforms also provide a field for assigning an identifier to a heading. The interface may differ, but the principle is the same: the link must point to an id that exists on the page.

If a link does not take readers to the right location, check for these common causes:

Specialist testing anchor links, troubleshooting issues, and verifying the destination on a website
Comparing ids, checking URLs, and testing links across devices can help catch anchor link issues early.
  • Incorrect id: The value after the # differs from the destination element’s id. Compare every character carefully.
  • The destination element is missing: The content may have been deleted, or a heading may have been renamed without updating the link. Make sure the element with the id is present in the page’s HTML.
  • Duplicate ids: If multiple elements share an id, the browser may take users to an unexpected location. Each value must be unique on the page.
  • The hash is in the wrong place: In the destination tag, write id="payment", not id="#payment". The hash belongs in the href.
  • A fixed navigation bar is in the way: The link works, but the heading is obscured. You can adjust the scroll position with CSS, for example using scroll-margin-top.
  • The link points to the wrong page address: For links to another page, check both the URL and the anchor after the hash.

After making changes, reload the page and test the link again. If your website uses caching, the old HTML may still be displayed; clear or refresh the cache before concluding that the link is broken. Test on multiple devices and browsers, especially on pages with fixed menus or dynamically loaded content.

For a better user experience and accessibility, link text should clearly describe where it leads, such as “Go to the payment instructions” rather than “Click here.” When using a table of contents, make sure destination headings are meaningful and that links can be operated with a keyboard. These details help readers understand a link before selecting it and make the page easier to use.

Conclusion

What are anchor links? They connect a link to a specific location marked with an id attribute. To create anchor links in HTML, assign a unique id to the destination element, set the link’s href to #id-name, then check the values and test the link on the live page. For a link to another page, add the URL before the hash.

When implemented correctly, anchor links help readers find information faster, especially on long pages with a lot of content. If your business needs a user-friendly website, a better content structure, or an improved digital customer experience, contact LilyTech for advice on the right solution.

About the author

Đoàn Lê Minh Anh

Đoàn Lê Minh Anh là tác giả có nền tảng học tập tại Học viện Ngân hàng, với định hướng phát triển trong lĩnh vực nội dung và viết SEO. Minh Anh chú trọng xây dựng những bài viết rõ ràng, chính xác, dễ hiểu, cung cấp thông tin hữu ích và đáp ứng tốt nhu cầu tìm kiếm của độc giả.