A link can point at a place within a page as well as at another page. The mechanism is decades old, requires no scripting, and remains the right way to build a table of contents, a skip link or a return to top control.
How the fragment works
Give the destination element an id, then link to that id preceded by a hash.
<h2 id="specificity">Understanding specificity</h2> <a href="#specificity">Jump to specificity</a> Clicking scrolls the heading into view and appends the fragment to the address, which means the position can be bookmarked and shared. Anyone opening that address lands directly at that section, which is why documentation sites link this way so heavily.
The id must be unique on the page. A duplicate means the browser jumps to the first match and quietly ignores the rest, which produces a link that appears to work while landing in the wrong place.
Naming ids for links
Because the id becomes part of a shareable address, treat it the way you would treat a filename. Lowercase, hyphens between words, descriptive and short. It must begin with a letter and contain no spaces.
Avoid changing them once published. An id is effectively a permanent address, and altering it breaks every existing link to that section, including ones on other people's sites.
Linking to a section of another page
Fragments combine with a path, which lets you point at a specific section of a different page.
<a href="/css/selectors/#specificity">Read about specificity</a> This works across sites too, and it is a considerate way to link when the relevant material is partway down a long document.
Returning to the top
A link containing only a hash jumps to the top of the page. It is more reliable than a link to an id on the body element and needs no scripting.
Be aware of the side effect, which is that it adds a history entry, so the back button returns the user to their previous scroll position rather than to the previous page. Some consider that useful and some find it confusing.
The sticky header problem
The most common complaint about fragment links is that with a fixed header, the target scrolls to the very top of the viewport and ends up underneath it. The heading you jumped to is hidden behind the navigation bar.
The old fixes were unpleasant, involving invisible offset elements or padding tricks that broke the layout. A CSS property now solves it directly.
:target { scroll-margin-top: 90px; } Scroll margin reserves space above the element when it is scrolled to, without affecting the layout at any other time. Set it to the height of your header and the problem disappears entirely.
Smooth scrolling
A single declaration replaces the instant jump with an animated scroll, which helps users understand that they have moved within the same page rather than arriving somewhere new.
html { scroll-behavior: smooth; } Wrap it in a media query that respects the reduced motion preference. Animated scrolling over a long distance is genuinely unpleasant for people with vestibular conditions, and honouring that setting is a two line addition.
Skip links
The most valuable fragment link on most sites is the one nobody sees. A skip link is the first focusable element on the page and jumps past the navigation straight to the main content.
<a href="#main" class="skip-link">Skip to content</a> Without it, a keyboard user tabs through every navigation item on every page before reaching anything unique. The convention is to hide it visually until it receives focus, at which point it appears. Hiding it with display none instead removes it from the focus order entirely and defeats the purpose, which is the usual implementation mistake.
Focus does not always follow the scroll
A fragment link moves the viewport, and it should also move keyboard focus to the target so that the next tab press continues from there rather than from the link. Browsers handle this correctly when the target is naturally focusable, such as a link or an input, and less reliably when it is a heading or a section.
The usual remedy is to add a tabindex of minus one to the target, which makes it programmatically focusable without inserting it into the normal tab order. Without this, a keyboard user activating a skip link can find the page scrolled to the content while their focus is still stranded in the navigation, which is worse than having no skip link at all.
Building a table of contents
For a long article, a list of links to each section at the top is straightforward to build and genuinely useful. Give each heading an id, list them in order, and the whole thing needs no JavaScript. Search engines sometimes surface these as sub links beneath a result, which is a pleasant side effect of doing something that was already worth doing.