
Long web pages such as tutorials, documentation, FAQs, and policy pages often contain a lot of information. Forcing visitors to scroll through everything to find what they need creates a frustrating experience. Fortunately, HTML allows you to link directly to a specific part of a web page, taking users exactly where they need to go. This technique improves usability, helps share precise information, and can even enhance how search engines display your content. This guide explains how to create these links step by step.
AAMAX.CO: Creating User-Friendly Navigation Experiences
Thoughtful navigation, including in-page links, makes websites easier to use and more engaging. AAMAX.CO is a full-service digital marketing company providing web development, digital marketing, and SEO services worldwide. Their designers and developers build intuitive navigation systems, tables of contents, and smooth-scrolling experiences that help users find information quickly. Businesses that want content-rich websites with excellent usability can rely on their website design services for thoughtful, user-centered layouts.
How In-Page Linking Works
Linking to a specific part of a page relies on two things: an element with a unique id attribute and a link that references that id using a fragment identifier. A fragment identifier is the part of a URL that begins with a hash symbol. When the browser sees a fragment in a URL, it scrolls to the element whose id matches that fragment.
Step 1: Add an ID to the Target Element
Choose the element you want to link to, such as a heading, and give it a unique id attribute:
<h2 id="pricing">Pricing Plans</h2>Each id must be unique within the page. Use lowercase letters and hyphens for readability, such as contact-form or shipping-policy.
Step 2: Create a Link to the ID on the Same Page
To link to that element from elsewhere on the same page, use the hash symbol followed by the id in the href attribute:
<a href="#pricing">View Pricing</a>When users click this link, the browser instantly scrolls to the Pricing Plans heading.
Step 3: Link to a Specific Part of Another Page
You can also link to a specific section on a different page by combining the page path with the fragment:
<a href="services.html#pricing">See Our Pricing</a>For external websites, include the full URL followed by the fragment, such as https://www.example.com/services#pricing. The target page must contain an element with the matching id for this to work.
Using Text Fragments
Modern browsers support text fragments, which allow you to link to specific text on a page even if it does not have an id. The syntax uses a special directive in the URL:
https://www.example.com/article#:~:text=specific%20phraseSupporting browsers scroll to and highlight the matching text. Text fragments are useful when you cannot edit the target page, but because support varies, ids remain the most reliable method for your own websites.
Add Smooth Scrolling
By default, the browser jumps instantly to the target. You can create a smoother experience with a single CSS rule:
html {
scroll-behavior: smooth;
}Respect users who prefer reduced motion by wrapping this rule in a prefers-reduced-motion media query that only enables smooth scrolling when motion is acceptable.
Handle Fixed Headers
If your website has a fixed or sticky header, it may cover the target element after scrolling. Use the scroll-margin-top property on target elements to add space above them:
h2 {
scroll-margin-top: 80px;
}Adjust the value to match your header height so headings are always fully visible.
Accessibility Considerations
Write clear, descriptive link text so users know exactly where each link will take them. Ensure target elements are meaningful, such as headings that describe the section. Skip links, which allow keyboard users to jump directly to the main content, are a common accessibility pattern built with the same technique.
SEO Benefits of Linking to Specific Sections
Search engines sometimes display direct links to page sections in search results, helping users jump straight to relevant content. Well-structured headings with ids, combined with a table of contents, make it easier for search engines to understand your page structure and can improve click-through rates.
Build a Table of Contents
A table of contents is one of the most practical uses of in-page links. Place an ordered or unordered list of links near the top of a long article, with each link pointing to the id of a heading below. Wrap the list in a nav element with an accessible label such as Table of contents. Readers can scan the outline, understand the article structure at a glance, and jump directly to the sections that matter to them. Many content management systems and plugins can generate tables of contents automatically from your headings.
Common Mistakes to Avoid
Duplicate ids cause links to jump to the wrong element, so ensure each id is unique. Ids are case-sensitive, so the link and the target must match exactly. Avoid spaces in ids and refrain from changing them after publishing, since others may already link to them.
Conclusion
Linking to a specific part of a web page is a simple yet powerful technique. Add unique ids to target elements, link to them with fragment identifiers on the same page or other pages, and consider text fragments when you cannot modify the target. Enhance the experience with smooth scrolling and scroll margins, and follow accessibility best practices. These small improvements make long pages far easier to navigate and more valuable for your visitors.