Learn • Create • Explore

How to Create Links and Navigation in HTML

Build a keyboard-friendly library menu and learn relative URLs, absolute URLs and fragment links.

Navigation gives readers a map of a site. A useful map shows how to reach the main sections, how to tell where they are, and how to return if they followed the wrong branch. You can build a dependable first version using ordinary HTML links. In this guide you will make a menu for a small library site, connect it to sections on the page, and test whether the labels still make sense when read on their own.

Links can point to a section in the current document, another page on the site, or another website

Start with destinations that already exist

Before writing the menu, make a list of the destinations. Our library page has a collection section, a joining section, and an opening-hours section. Those are labels visitors can understand. A link called “Discover” might sound attractive, but if it takes people to opening hours, they should not have to guess. Name the destination using the vocabulary that appears on the page itself.

For a small site, a menu with a few stable links is often enough. Put the most important destinations near the beginning and keep the order consistent on related pages. A menu that changes position or labels on every page forces people to relearn it. If two links go to the same address, there may be a reasonable explanation, such as a footer repeating the main contact link. If two adjacent links sound similar but go to different places, rewrite their labels before adding more decoration.

The anchor element wraps the content that a visitor can activate. Its destination is the value in the href attribute. If you omit the destination, the element is not an ordinary navigable hyperlink. An empty destination is also not a harmless placeholder because it can resolve to the current page. Use real links when they are ready. For a section that has no page yet, display ordinary text or leave it out of the navigation until it has a destination.

Fragments are useful for long pages, and this example uses them so every link can be tried in one document. Paste the complete example into the HTML editor, show the preview, and activate each link. The examples use a real navigation landmark and a list of related links.

HTML example
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Willow Library visitor information</title>
  <style>
    body { max-width: 44rem; margin: 1rem auto; padding-inline: 1rem; font: 1rem/1.6 system-ui; color: #10244a; }
    nav ul { display: flex; flex-wrap: wrap; gap: .75rem; list-style: none; padding: 0; }
    nav a { display: inline-block; padding: .5rem .8rem; background: #e0f1ff; color: #00356d; border-radius: .5rem; }
    a:focus-visible { outline: 3px solid #963500; outline-offset: 3px; }
    main section { margin-block: 2rem; padding: 1rem; min-height: 140px; background: #f0faed; }
    section:target { outline: 3px solid #087643; }
  </style>
</head>
<body id="top">
  <a href="#main-content">Skip to main content</a>
  <header>
    <h1>Willow Library</h1>
    <nav aria-label="Visitor information">
      <ul>
        <li><a href="#collection">Collection</a></li>
        <li><a href="#join">How to join</a></li>
        <li><a href="#hours">Opening hours</a></li>
      </ul>
    </nav>
  </header>
  <main id="main-content">
    <section id="collection"><h2>Collection</h2><p>Find fiction, local history and study guides.</p></section>
    <section id="join"><h2>How to join</h2><p>Ask at the front desk for membership information.</p></section>
    <section id="hours"><h2>Opening hours</h2><p>Tuesday to Saturday, 10 a.m. to 4 p.m.</p></section>
    <p><a href="#top">Back to top</a></p>
  </main>
</body>
</html>
Try it in the HTML editor

Each fragment starts with a hash, followed by the ID of its target. The hash is part of the destination syntax but does not appear in the ID. Keep each ID unique. The preview outlines the selected section using a CSS rule for the current target. That outline is a way to see the effect when the page is short enough that the browser cannot scroll much. On a real site you can also reserve room above targets if a fixed header would otherwise cover the section title.

A fragment link resolves to a matching unique ID in the document

Choose a relative or absolute address

When the destination is a sibling file, a relative path can be easy to read. From a file called index.html, a destination of membership.html points to a file in the same directory. If the site has a subfolder for help pages, a destination of help/membership.html descends into it. Paths that start with two dots move up one directory. Work out the path from the document’s address, rather than from the project folder visible in your editor.

A site-root path begins with a slash. On a site installed at the domain root, /contact/ points to the contact directory from any page on that site. If you move the site into a subfolder, the same path can point outside the installation. A local mockup opened from disk may also resolve that slash differently from a deployed website. When a link works from one environment but fails in another, inspect the full address displayed by the browser rather than changing several paths at once.

Use a full HTTPS address for a destination on another site. Check the scheme and hostname before sharing it. Human-readable text can conceal a mistyped domain, so the link label alone does not prove where it goes. In a content system, keep important links in shared navigation or reusable page templates so you can update changed URLs in one place.

Make the menu usable without a pointer

Press Tab from the beginning of the example. The main-content link should be the first focusable item. It provides a shortcut past the menu, which makes sense on pages with repeated navigation. Then tab through the menu and activate a link with Enter. The order should follow the presentation on screen. A visible focus outline helps you keep track of the current item. Do not remove the focus indicator because it looks different from the hover state. People navigating with a keyboard need their own visible feedback.

The navigation landmark groups major navigation links, and its label distinguishes it from other navigation regions on a larger page. You do not need a landmark for every small collection of links. A single footer link or a link inside a sentence can remain where it belongs in the content. On a multi-page site, marking the current page link with the appropriate current-page attribute can make the active destination clear to assistive technology. A colour change by itself does not convey that information to everyone.

Tab moves through the skip link, menu links, and the content in reading order

If a link does not appear to move, check whether the browser address changes. When it changes to a fragment but nothing scrolls, confirm that the destination is not already visible and that the matching ID is present. When the address is wrong, inspect the actual href. Misspelled paths, duplicate slashes, incorrect capitalization, and missing file extensions often produce similar looking failures. Test from the real page that contains the link, not just by typing a known-good destination into a separate tab.

A menu can fail visually even when every link is valid. Narrow the viewport and see whether labels wrap or fall off the page. The example uses wrapping rather than a hamburger button, which gives a small menu room to reflow without any JavaScript. If you later design a collapsed menu, keep the links usable with the keyboard and make the button communicate whether the list is expanded. Add that complexity only when the number of links makes it useful.

Finish by replacing the library labels with three destinations on a real site of your choice. Read the link names without their surrounding paragraphs, and check that each one still makes sense. Then try every destination from the narrow view. For reference details see MDN’s guide to creating links.

Handle new tabs and downloads as exceptions

Allow an ordinary link to open in the same tab unless there is a reason to change its behaviour. The reader can choose to open it in a new tab using browser controls. A forced new tab can make the expected Back action confusing, especially on a phone. When a workflow does require a separate tab, say so in or near the label. Use the equivalent safe relationship setting appropriate for that link so the destination cannot control the original window.

A link to a PDF or a large downloadable file should identify what it opens. Include the file type and, when it helps someone on a slow connection, the approximate size. Browsers can handle downloads differently according to their settings, and an attribute does not guarantee that a cross-origin resource will be saved. Verify the real server response and the actual saved file rather than relying only on the wording of the link.

Test the link in the browser your intended readers are likely to use.

Related tools

Continue learning