Published by Patrick Mutisya · 14 days ago
Know and understand why relative file paths must be used for attached stylesheets.
| Aspect | Absolute Path | Relative Path |
|---|---|---|
| Definition | Full URL including protocol and domain (e.g., https://www.example.com/css/style.css) | Path expressed from the location of the HTML file (e.g., css/style.css or ../styles/main.css) |
| Portability | Low – changes if domain or folder changes | High – works as long as relative folder structure is preserved |
| Typical Use Cases | Linking to external resources such as CDNs, APIs, or assets on another site | Linking to internal resources (stylesheets, scripts, images) within the same project |
| Maintenance | More effort required when moving the site | Less effort – only folder hierarchy matters |
css/style.css – when the CSS folder is a sub‑folder of the HTML file’s folder.../css/style.css – when the CSS folder is one level up.../../assets/css/style.css – when you need to go up two levels then down into a different branch.<link> element inside the <head> of the HTML document:<link rel="stylesheet" href="css/style.css">
/css/style.css) – this makes the path absolute to the root of the domain, breaking portability./) in URLs.Folder structure:
project/
│
├─ index.html
├─ about.html
├─ css/
│ └─ main.css
├─ pages/
│ ├─ contact.html
│ └─ services.html
└─ assets/
└─ images/
└─ logo.png
Relative paths for each HTML file:
| HTML File | Relative Path to main.css | Relative Path to logo.png |
|---|---|---|
| index.html | css/main.css | assets/images/logo.png |
| about.html | css/main.css | assets/images/logo.png |
| pages/contact.html | ../css/main.css | ../assets/images/logo.png |
| pages/services.html | ../css/main.css | ../assets/images/logo.png |