Anchor links—often called jump links, fragment identifiers, or in-page links—are one of the most underutilized yet powerful tools in web design and content strategy. They allow visitors to skip directly to a specific section of a long article, a pricing table, an FAQ answer, or a contact form without endless scrolling. Whether you are a blogger trying to improve dwell time, a developer building a single-page application, or a marketer crafting a high-converting landing page, mastering how to link to a certain part of a page is essential for user experience (UX) and accessibility That's the whole idea..
You'll probably want to bookmark this section.
Understanding the Core Concept: The Fragment Identifier
At the heart of this functionality lies the fragment identifier. In a standard URL, the fragment is the portion following the hash symbol (#). When a browser encounters a URL like https://example.com/long-article#pricing, it performs two actions: it loads the page long-article and then immediately searches the Document Object Model (DOM) for an element with an id attribute matching pricing. If found, the viewport scrolls to bring that element into view Small thing, real impact. Took long enough..
Short version: it depends. Long version — keep reading.
This mechanism is native to HTML and requires zero JavaScript to function. It works across all modern browsers, screen readers, and devices, making it a dependable, standards-compliant solution for on-page navigation The details matter here. That alone is useful..
Method 1: The Standard HTML Approach (ID Attribute)
The most reliable and semantic way to create a jump link is using the id attribute on the target element. This method works on any HTML element: headings (<h2>, <h3>), divs, spans, sections, or even images.
Step-by-Step Implementation
- Identify the Destination: Decide which section you want the user to land on. Let’s say it is a heading titled "Pricing Plans".
- Add a Unique ID: Open your HTML editor and locate the opening tag of that element. Add
id="pricing-plans"(or any unique, descriptive string without spaces).
Best Practice: Use lowercase letters, numbers, and hyphens (kebab-case). Avoid special characters. Ensure the ID is unique across the entire page; duplicate IDs invalidate HTML and cause unpredictable scrolling behavior.Our Pricing Plans
- Create the Source Link: Go to the location where you want the link to appear (e.g., a table of contents at the top of the page). Create a standard anchor tag (
<a>) but set thehrefattribute to#followed by the exact ID you just created.View Pricing Options - Test Thoroughly: Save the file and open it in a browser. Click the link. The page should jump instantly to the "Our Pricing Plans" heading.
Cross-Page Linking
You are not limited to the current page. To link to a specific section on a different page, simply prepend the full URL path before the hash.
Enterprise Tier Details
This is invaluable for deep linking from email newsletters, social media posts, or external documentation directly to the relevant answer Simple, but easy to overlook..
Method 2: The Legacy name Attribute (And Why to Avoid It)
In older HTML versions (HTML 4 and XHTML), developers used the <a name="section-name"></a> syntax to create anchors. You will still encounter this in legacy codebases.
Contact Us
Why avoid it?
- Semantics: An anchor tag (
<a>) without anhrefis not a link; it’s a placeholder. Using a heading or section tag with aniddescribes the structure of the content, not just a coordinate. - Accessibility: Screen readers manage by landmarks and headings. An empty anchor tag adds noise to the accessibility tree without providing structural context.
- Validation: The
nameattribute on<a>tags is obsolete in HTML5. While browsers still support it for backward compatibility, valid HTML5 demandsidon the relevant structural element.
Enhancing UX: Smooth Scrolling and Visual Polish
By default, the browser performs an instant "jump.On the flip side, " This can be disorienting for users, causing them to lose context of where they were on the page. Modern CSS and JavaScript offer solutions.
CSS Scroll Behavior (The Modern Standard)
The cleanest, most performant way to enable smooth scrolling is a single line of CSS. Add this to your global stylesheet:
html {
scroll-behavior: smooth;
}
This tells the browser to animate the scroll position change for all anchor links (and programmatic scrolling) automatically. It requires no JavaScript, respects user preferences for reduced motion (via prefers-reduced-motion media query), and works on all modern browsers That's the part that actually makes a difference..
The "Sticky Header" Problem
A common frustration arises when a fixed (sticky) navigation bar covers the target element after the scroll completes. The user lands on the section, but the heading is hidden behind the header That alone is useful..
Solution 1: CSS scroll-padding-top (Recommended)
Define a scroll padding on the root element equal to the height of your sticky header Worth keeping that in mind. Still holds up..
html {
scroll-behavior: smooth;
scroll-padding-top: 80px; /* Adjust to match your header height */
}
The browser will now stop scrolling 80px above the target element, ensuring the heading is fully visible below the header.
Solution 2: The :target Pseudo-class Highlight
To give users a clear visual cue that they have arrived at the correct spot, use the :target pseudo-class in CSS. This styles the element only when its ID matches the current URL fragment.
:target {
background-color: #fff3cd; /* Soft yellow highlight */
border-left: 4px solid #ffc107;
padding-left: 1rem;
animation: fade-out 3s ease-out forwards;
}
@keyframes fade-out {
0% { background-color: #fff3cd; }
100% { background-color: transparent; border-left: none; padding-left: 0; }
}
This creates a "flash" effect on the target section, drawing the eye immediately to the content.
JavaScript Control: When You Need Programmatic Power
While CSS handles 95% of use cases, JavaScript is necessary for:
- Scrolling to elements that don't have IDs (e.g., third-party widgets).
- Triggering scrolls based on user actions other than clicks (e.On the flip side, g. But , "Scroll to error" on form validation). * Offsetting scroll position dynamically (e.Consider this: g. , header height changes on mobile vs desktop).
The scrollIntoView() API
This is the modern, native DOM method. It is supported everywhere and highly configurable Still holds up..
const targetElement = document.getElementById('pricing-plans');
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth', // 'auto' or 'smooth'
block: 'start', // 'start', 'center', 'end', 'nearest'
inline: 'nearest'
});
}
behavior: 'smooth': Triggers the animation (ignored if user prefers reduced motion).block: 'start': Aligns the top of the element to the top of the viewport (respectsscroll-padding-topin modern browsers).
Handling Dynamic Content (SPAs and AJAX)
In Single Page Applications (React, Vue, Svelte) or pages loading content via AJAX, the target element might not exist in the DOM when the link is clicked. You must ensure the