# Paths & Fragment Links

> Every `href` value is either a path to a resource or a fragment that jumps within the current page. Understanding how paths resolve makes links predictable wherever the page moves.

Subject: HTML | Lesson 15 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/link-paths-and-fragments
Previous: [The Anchor Element & href](https://codewithanimation.in/html/anchor-and-href.md) | Next: [New Tabs, rel & mailto/tel](https://codewithanimation.in/html/target-rel-and-protocols.md)

## What they are

A path in an `href` points to a file or route. It can be absolute (`https://example.com/about`) with a full scheme and domain, root-relative (`/about`) resolved from the site root, or relative (`../images/logo.png`) resolved from the current file's location. A fragment identifier starts with `#` and jumps within the same page to the element whose `id` matches the fragment.

Fragments and paths can combine: `https://example.com/docs#installation` takes the browser to the docs page and then scrolls to the element with `id="installation"`. The fragment is always the last piece of the URL.

## Why it matters

Relative paths are what keep a site portable. If you hard-code `https://example.com/styles.css` in every page, moving the site to a staging subdomain breaks every link. A root-relative `/styles.css` resolves correctly wherever the domain is. A file-relative path resolves from the file's own location, which is useful for assets that always travel alongside their page.

Fragment links are the original single-page navigation: a long article with a table of contents uses `href="#section-3"` to let readers jump directly to a heading. Screen readers expose heading and landmark navigation separately, but fragment links give sighted users and keyboard navigators a fast direct route without any JavaScript.

## How it works

For relative paths, the browser resolves the href against the current page's URL. A leading `/` makes the path root-relative, anchoring it to the site origin. No leading slash means file-relative: `images/logo.png` looks for `images/` in the same directory. `../` steps up one directory level. These rules are the same as in a terminal.

For fragment links, give the target element a unique `id` and write `href="#that-id"`. The browser scrolls the target into view (smooth scroll if `scroll-behavior: smooth` is set on `:root`) and updates the URL bar. If no element matches the fragment, nothing happens. An `href="#"` with no id scrolls back to the very top.

The full URL anatomy: `scheme://host:port/path?query#fragment`. Each piece is optional except in context: you always need a scheme for absolute links, but root-relative and file-relative links omit it. The fragment is always last and never sent to the server.

## Common mistake: File-relative paths that break when the page moves

A relative path like `images/photo.jpg` resolves from the current file's location. If you reorganise the folder structure and move the page one directory up, the path now looks for `images/` in the wrong place. Root-relative paths (`/images/photo.jpg`) solve this: they always resolve from the site root, so moving a page never breaks them. Use root-relative paths for shared assets; use file-relative only for assets that always live next to the page that uses them.

## Tip: Smooth-scroll fragment links with one CSS line

Add `scroll-behavior: smooth` to `:root` or `html` in your stylesheet and every fragment link on the site animates to its target instead of jumping. No JavaScript required. Users who prefer reduced motion get the instant jump automatically because browsers respect `prefers-reduced-motion` for this property.

## Try it: Try it

Click a section link and watch the target row highlight. Each chip shows a different `href` value and where it resolves.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/link-paths-and-fragments

## Check yourself

### Question 1

A page lives at /blog/post.html. The href is /images/photo.jpg. Where does the browser look for the image?

- A. At the site root: /images/photo.jpg
- B. In the blog folder: /blog/images/photo.jpg
- C. Relative to the domain: https://example.com/blog/images/photo.jpg

Correct answer: A

A leading `/` makes a path root-relative: the browser resolves it from the site origin, not from the current page's folder. `/images/photo.jpg` always points to the root-level `images` folder regardless of where the page lives.

- Why not B: The leading slash makes the path root-relative, not file-relative. It resolves from the site origin, not from the current file's directory.
- Why not C: A leading slash anchors to the root of the origin, so the result is https://example.com/images/photo.jpg, not the blog subdirectory.

### Question 2

A link has href="#contact". There is no element with id="contact" on the page. What happens?

- A. Nothing; the browser ignores the fragment silently
- B. The browser scrolls to the top of the page
- C. The browser throws a 404 error

Correct answer: A

Fragment identifiers are resolved entirely in the browser. If no element has a matching `id`, the browser does nothing. No error, no scroll. This is different from `href="#"` (empty fragment) which scrolls to the page top.

- Why not B: Scrolling to the top is what href="#" (empty fragment) does. A non-empty fragment with no matching id produces no scroll.
- Why not C: Fragments are client-side only and never sent to the server. A missing id match is a silent no-op, not a server error.

### Question 3

A page is at /docs/guide/intro.html. The href is ../../images/logo.png. Where does it resolve?

- A. /images/logo.png (two directories up from /docs/guide/)
- B. /docs/images/logo.png (one directory up)
- C. /docs/guide/images/logo.png (resolved in the current directory)

Correct answer: A

Each `../` moves one directory level up. From `/docs/guide/intro.html`, the current directory is `/docs/guide/`. One `../` reaches `/docs/`; a second reaches `/`. So `../../images/logo.png` resolves to `/images/logo.png`.

- Why not B: ../ steps up one level (from /docs/guide/ to /docs/). A second ../ steps up again to the root, giving /images/logo.png.
- Why not C: No leading ../ means current directory. Each ../ moves one level up. Two of them from /docs/guide/ land at the site root.

## Remember this

- Root-relative paths (`/path`) resolve from the site origin; file-relative paths resolve from the current file's directory.
- `../` steps one directory up; chain them to climb multiple levels.
- Fragment links (`href="#id"`) scroll to the element with that `id`; no match means no scroll.
- `href="#"` (empty fragment) scrolls to the page top; fragments are never sent to the server.
