Sub topic 7
Making links and assets work under a project path
This chapter assumes you can publish a site from a branch or a workflow, and that you know basic HTML. It also assumes you know how a browser resolves a URL against the current page.
Why project sites break
A user or organization site lives at the root of its host, such as https://USERNAME.github.io/. A project site lives under a path named after the repository, such as https://USERNAME.github.io/my-repo/.
A link that starts with / is root-relative. The browser resolves it against the host and ignores the repository path. On a project site, /style.css points at https://USERNAME.github.io/style.css, which is not your file. The result is a 404 and an unstyled page.
The same page can work on your laptop and break once deployed. A local server usually serves your site from /, so root-relative links succeed there.
Link forms compared
The table shows how each form resolves from a page at /my-repo/docs/page.html.
| Link in the HTML | Resolves to | Works on a project site? |
|---|---|---|
| `style.css` | `/my-repo/docs/style.css` | Yes, if the file is in `docs/` |
| `../style.css` | `/my-repo/style.css` | Yes |
| `/style.css` | `/style.css` | No |
| `/my-repo/style.css` | `/my-repo/style.css` | Yes, but it breaks if you rename the repository or add a custom domain |
Prefer relative links. They work under any path, so the same files work on a project site, a user site, and a custom domain.
Fixing hand-written pages
Replace the leading slash with a path relative to the current file.
<!-- Broken on a project site -->
<link rel="stylesheet" href="/css/style.css">
<img src="/images/logo.png" alt="Logo">
<a href="/about.html">About</a>
<!-- Works from the site root page -->
<link rel="stylesheet" href="css/style.css">
<img src="images/logo.png" alt="Logo">
<a href="about.html">About</a>Pages in subfolders need ../ to climb back up. If that becomes tedious, a <base> element sets the prefix for every relative URL on the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="/my-repo/">
<title>My site</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<a href="about.html">About</a>
</body>
</html>The <base> approach hard-codes the repository name. It also changes how fragment links such as #section resolve, so they point at the base URL instead of the current page. Use it only when you accept both costs.
Fixing generated sites
Static site generators and bundlers have a setting for the base path. Set it to the repository path, with a leading and trailing slash.
| Tool | Setting |
|---|---|
| Jekyll | `baseurl: "/my-repo"` in `_config.yml`, and `{{ site.baseurl }}` or the `relative_url` filter in templates |
| Vite | `base` in `vite.config.js` |
import { defineConfig } from "vite";
export default defineConfig({
base: "/my-repo/",
});Remove the setting, or set it to /, if you later move to a user site or a custom domain. A custom domain serves the site from the root, so the old prefix would break every link.
Other causes of broken paths
- Case sensitivity. GitHub Pages serves paths exactly as named.
Logo.pngandlogo.pngare different files, even if your local machine treats them as the same. - Missing files. Check that the asset is inside the published folder. With a
/docssource, files outsidedocsare not served. - Wrong cached copy. After a fix, hard-refresh the browser. Then check the Network tab in developer tools for the exact URL that returned 404.
Exercise
Your project site is at https://USERNAME.github.io/notes/. The page notes/guides/setup.html contains <img src="/img/shot.png">, and the file is at img/shot.png in the repository root. Why does the image fail, and what href-style path fixes it without hard-coding the repository name?
Show answer
The leading slash resolves against the host root, giving https://USERNAME.github.io/img/shot.png, which does not exist. Use a relative path from the page's folder, ../img/shot.png, which resolves to /notes/img/shot.png.