Anchor Fragments — Linking Within a Page
A fragment identifier — the part of a URL after # — lets a link jump straight to a specific spot on a page. Combined with id attributes on target elements, this powers tables of contents, footnotes, and the widely used "skip to content" accessibility pattern.
Fragment Identifiers (#section)
Add an id to any element, then link to it by prefixing that id with #.
fragment-basic.html
<a href="#pricing">Jump to pricing</a> <!-- ... further down the page ... --> <section id="pricing"> <h2>Pricing</h2> <p>Plans start at $9/month.</p> </section>
Linking From a Different Page
Fragments also work appended to a full URL — the browser loads the target page, then scrolls straight to the matching id.
cross-page-fragment.html
<a href="/html/links#anatomy">Read about link anatomy</a>
Same-Page Smooth Scrolling
By default, jumping to a fragment is an instant, jarring cut. CSS's scroll-behavior: smooth makes it animate instead, purely as a visual enhancement — no JavaScript required.
smooth-scroll.html
<style>
html {
scroll-behavior: smooth;
}
</style>
<nav>
<a href="#intro">Introduction</a>
<a href="#details">Details</a>
</nav>
<section id="intro">...</section>
<section id="details">...</section>Skip-to-Content Accessibility Pattern
Keyboard and screen-reader users often have to tab through the entire header and navigation on every single page before reaching the main content. A "skip to content" link — the very first focusable element on the page — lets them jump straight past it.
skip-link.html
<style>
.skip-link {
position: absolute;
left: -9999px;
top: 0;
}
.skip-link:focus {
left: 0;
padding: 8px 16px;
background: #fff;
z-index: 100;
}
</style>
<body>
<a class="skip-link" href="#main-content">Skip to main content</a>
<header>
<nav><!-- long navigation menu --></nav>
</header>
<main id="main-content">
<h1>Page Title</h1>
<!-- ... -->
</main>
</body>The link is visually hidden off-screen until it receives keyboard focus (via Tab), at which point it becomes visible — sighted mouse users never see it, but keyboard users get an immediate shortcut.
Quick Reference
Pattern | Example |
|---|---|
Link to an id on the same page |
|
Link to an id on another page |
|
Target element |
|
Smooth scrolling |
|
Skip link | First focusable element, targets |
Fragment links are pure HTML/CSS — no JavaScript needed for basic jump-to-section or smooth scrolling behavior.
Always give your
<main>(or main content wrapper) anidso a skip link has somewhere to land.Test skip links with the keyboard (Tab key) — that is how real users encounter them.