Lesson 1, Topic 1
In Progress

Lesson 2.8: Relative vs. Absolute File Paths

Lesson 2.8 – Relative vs. Absolute File Paths | Knowledge Pillars HCCS
Unit 2 · Lesson 2.8

Relative vs. Absolute File Paths

Every broken image and missing stylesheet traces back to a wrong file path. Learn exactly how paths work — and which type to use — so your links never break.

HTML & CSS Web Design ~45 Minutes Beginner

Lesson Objectives

  • Understand what a file path is and why it matters
  • Explain the difference between relative and absolute paths
  • Write relative paths to link files in the same folder, a subfolder, and a parent folder
  • Know when to use an absolute URL vs. a relative path
  • Apply correct paths to href, src, and link attributes
  • Diagnose and fix broken path errors

Part 1: What Is a File Path?

Any time your HTML needs to reference another file — an image, a CSS stylesheet, another webpage, or a downloadable PDF — it uses a file path to tell the browser exactly where to find it. Think of a file path like a street address: it describes the location of a file within a folder structure on your computer or web server.

There are two fundamentally different types of paths: relative paths and absolute paths. Understanding the difference between them — and knowing which to use when — is one of the most practical skills in web development. Every broken image or missing stylesheet you've ever seen was caused by a wrong file path.

🗂️ Where do paths appear in HTML? File paths are used in the src attribute of <img> and <script> tags, the href attribute of <a> and <link> tags, and the action attribute of forms. The same rules apply in all of these places.

Part 2: Understanding Your Project's Folder Structure

Before writing any path, you need a mental picture of your project's folder structure — which files are in which folders and how they relate to each other. All relative paths are calculated from the location of the current file doing the linking.

All the examples in this lesson assume you are working from index.html at the root of this project. The paths you write tell the browser how to navigate from that file to any other file in the structure.

Part 3: Relative vs. Absolute Paths — The Key Difference

📁 Relative Path

Relative to the current file's location. Think of it as directions from where you are right now — "go into the images folder and grab profile.jpg."

Does not start with https:// or /. Always describes a location in relation to the file doing the linking.

Example: images/profile.jpg

Use for: All files within your own project — images, stylesheets, other HTML pages, downloads.

🌐 Absolute Path

The full, complete address from the beginning. Like a GPS coordinate — it works from anywhere, no matter where you are.

Always starts with a protocol: https:// or http://. Specifies the exact location on the entire internet.

Example: https://example.com/images/logo.png

Use for: External websites, images or resources hosted on other servers, CDN-hosted libraries.

🏆 The Golden Rule: Use relative paths for everything inside your own project, and absolute paths for everything outside it. This is the single most important rule for writing paths correctly. If you move your project folder to a different computer or upload it to a web server, relative paths keep working — absolute paths to external resources keep working too. It's only when you mix them up that things break.

Part 4: Writing Relative Paths — All Four Patterns

Relative paths have four distinct patterns depending on where the target file lives in relation to the current file. Using the project structure from Part 2, with index.html as the starting point:

Pattern 1 — Same Folder
href="about.html"

When the target file is in the same folder as the current file, just use the filename. No folder prefix needed.

Pattern 2 — Into a Subfolder
src="images/profile.jpg"  or  href="pages/contact.html"

Use the folder name followed by a forward slash /, then the filename. You can nest multiple levels deep: assets/images/icons/star.png

Pattern 3 — Up One Folder Level
href="../index.html"

The ../ prefix means "go up one folder level." Used when your current file is inside a subfolder (like pages/contact.html) and needs to reference something in the parent folder.

Pattern 4 — From the Root (Absolute-Style Relative)
href="/index.html"  or  src="/images/logo.png"

A leading / means "start from the root of the website" — not the root of the whole computer, but the top-level public folder of the web server. Useful on multi-page sites but doesn't work when opening files locally.

<!-- All examples assume we're working from index.html --> <!-- Link to a CSS file in the same folder --> <link rel="stylesheet" href="style.css"> <!-- Image inside the images/ subfolder --> <img src="images/profile.jpg" alt="Profile photo" width="150" height="150"> <!-- Link to another page in the pages/ subfolder --> <a href="pages/contact.html">Contact Me</a> <!-- Link to another page in the same folder --> <a href="about.html">About Me</a> <!-- Download a PDF from the files/ subfolder --> <a href="files/resume.pdf" download>Download Resume</a>

Part 5: When to Use Absolute Paths

Absolute paths are the right choice any time you're referencing a resource that lives outside your own project — on a different server or domain. They are also the only option when linking to external websites, loading fonts from Google Fonts, or using a CDN-hosted library.

<!-- External website link — must use full URL --> <a href="https://www.wikipedia.org" target="_blank" rel="noopener noreferrer"> Visit Wikipedia </a> <!-- Google Font loaded from an external server --> <link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet"> <!-- Image hosted on another server --> <img src="https://via.placeholder.com/300x200" alt="Placeholder image from external server" width="300" height="200">
Relative vs. Absolute — Quick Decision Guide
Situation Use Example
Linking to another page in your projectRelativehref="about.html"
Displaying an image from your images folderRelativesrc="images/photo.jpg"
Linking your CSS stylesheetRelativehref="style.css"
Offering a file download from your projectRelativehref="files/resume.pdf"
Linking to an external websiteAbsolutehref="https://example.com"
Loading a Google Font or CDN libraryAbsolutehref="https://fonts.googleapis.com/..."
Displaying an image from another serverAbsolutesrc="https://example.com/img.jpg"

Part 6: Common Path Mistakes & How to Fix Them

❌ Broken Paths
src="profile.jpg" /* File is in images/ subfolder — needs images/profile.jpg */ href="www.wikipedia.org" /* Missing https:// — browser looks for a local file! */ src="Images/photo.jpg" /* Wrong case — folder is images/ not Images/ */ src="my photo.jpg" /* Space in filename breaks the path */
✅ Correct Paths
src="images/profile.jpg" /* Correct subfolder path ✓ */ href="https://www.wikipedia.org" /* Full absolute URL ✓ */ src="images/photo.jpg" /* Lowercase matches folder name exactly ✓ */ src="my-photo.jpg" /* Hyphen instead of space ✓ */
⚠️ Case Sensitivity on Web Servers: Your own Windows or Mac computer may not care whether you write Images/ or images/ — it's forgiving. But web servers (running Linux) are case-sensitive. A path that works perfectly on your laptop can break the moment you upload to a hosting service. Always use all lowercase for folder and file names.

🖥️ Code Skills Lab Reminder

💾 Save to Compile! As you work through today's activity in the Knowledge Pillars Code Skills Lab, press Ctrl + S (Windows) or Cmd + S (Mac) after each step. After saving, check the preview — a working path displays the image or follows the link correctly, while a broken path shows a missing image icon or navigates nowhere.

Student Activity

📂 Path It

Practice File Paths in Your Project — Code Skills Lab

Open your project in the Code Skills Lab. You're going to audit and improve all the file paths in your index.html — making sure every image, stylesheet, and link uses the correct path type. Work through each step carefully!

  1. Map your project structure. Before writing any paths, open the file explorer panel in the Code Skills Lab and sketch (or mentally note) where every file in your project lives. Confirm you have an images/ folder and that your images are inside it.
    /* Your project should look like this: */ my-project/ ├── index.html ← working from here ├── about.html ├── style.css ├── images/ │ └── profile.jpg └── files/ └── resume.pdf
  2. Check your CSS link path. In your <head>, confirm your stylesheet is linked with a relative path. If style.css is in the same folder as index.html, the path is just the filename.
    <head> <!-- style.css is in the same folder as index.html --> <link rel="stylesheet" href="style.css"> </head>
    💾 Save — your styles should still apply correctly!
  3. Audit your image paths. Look at every <img> tag in your file. If your images are in an images/ subfolder, each src should start with images/. Fix any that are missing the folder prefix.
    <!-- Image INSIDE the images/ subfolder --> <img src="images/profile.jpg" alt="Portrait of Jordan smiling outdoors" width="150" height="150"> <!-- NOT just: src="profile.jpg" --> <!-- The browser won't find it there! -->
    💾 Save — all images should appear in the preview. Any broken icon means the path is wrong.
  4. Add an internal link to a second page. Create a new file called about.html in your project (it can be a basic HTML shell). Then add a relative link from index.html to it.
    <!-- about.html is in the same folder as index.html --> <a href="about.html">Learn more about me</a>
    💾 Save — clicking the link should open about.html.
  5. Verify your external links use absolute paths. Find any links that go to outside websites in your HTML. Confirm each one starts with https://. If any start with just www., fix them by adding https://.
    <!-- ❌ Wrong — missing https:// --> <a href="www.codecademy.com">Codecademy</a> <!-- ✅ Correct — full absolute URL --> <a href="https://www.codecademy.com" target="_blank" rel="noopener noreferrer"> Visit Codecademy </a>
    💾 Save — external links should now open correctly in a new tab.

🌟 Bonus Challenges

Push your path skills further:

1. Create a pages subfolder and move about.html into it. Then update the link from index.html to use the subfolder path, and add a "back to home" link from inside about.html using ../ to go up a level:

/* From index.html — link INTO a subfolder: */ <a href="pages/about.html">About Me</a> /* From pages/about.html — link BACK to parent: */ <a href="../index.html">Back to Home</a>

2. Intentionally break a path (change images/ to imgs/), save, and observe the broken image icon. Then fix it and confirm it comes back. This is exactly how you'd diagnose a real-world path error.

3. Load a Google Font using an absolute path — add this to your <head> and update your CSS font-family to use it:

<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">

💾 Remember to save after each change to see the result!

Troubleshooting Tips

  • Broken image icon showing? The src path is wrong. Check: (1) Is the folder name spelled correctly and in the right case? (2) Is the filename spelled correctly including the extension (.jpg, .png)? (3) Is the file actually saved in that folder?
  • CSS stylesheet not applying? Check the href in your <link> tag. If style.css is in the same folder as your HTML file, the path should be just "style.css" — not "css/style.css" unless you've moved it into a subfolder.
  • Internal link goes to a blank page or 404? The filename in href must match the actual file name exactly — including the .html extension and letter case. Also verify the file exists in the location the path describes.
  • External link not working — browser looking for a local file? You've used a relative-style path for an external link. External links must start with https://. Without it, the browser interprets the path as local, e.g., "www.google.com" looks for a folder named "www.google.com" in your project.
  • Can't navigate back to the parent folder? Use ../ to go up one directory level. Each ../ moves up one folder. From two levels deep you'd use ../../ to reach the root.

Lesson Summary

Lesson 2.8 — File Paths Reference
Concept Example Use When
Relative — same folderhref="about.html"Target file is in the same folder as the current file
Relative — subfoldersrc="images/photo.jpg"Target is inside a subfolder of the current location
Relative — parent folderhref="../index.html"Current file is in a subfolder; target is in the folder above
Relative — root-relativehref="/style.css"Referencing from the website root — only works on a live server
Absolute URLhref="https://example.com"Linking to any resource on another domain or server
Lowercase ruleimages/photo.jpg ✓Always — web servers are case-sensitive; uppercase breaks paths
No spaces rulemy-photo.jpg ✓Always — spaces in filenames break paths; use hyphens instead

Knowledge Check

Read each question carefully and select the best answer. Each question has one correct response.

1. You're working in index.html and want to display an image called banner.jpg that is saved inside a folder named images/. Which src value is correct?

2. You're editing pages/contact.html and want to link back to index.html, which is in the parent folder. Which path is correct?

3. Which path type should you use when linking to an external website like Wikipedia?

4. A page works perfectly on your laptop, but after uploading to a web server all images are broken. The images are referenced as src="Images/photo.jpg". What is the most likely cause?

5. Which of the following is the correct relative path to link a CSS file named style.css that is stored in the same folder as your HTML file?

Lesson Checklist

  • I can explain the difference between a relative and an absolute file path
  • All images in my project use relative paths with the correct subfolder prefix
  • My CSS stylesheet is linked with a relative path from my HTML file
  • I added an internal link to another page in my project using a relative path
  • All external links use full absolute URLs starting with https://
  • All file and folder names in my project use lowercase letters with no spaces
  • I understand what ../ means and when to use it
  • I saved my file in the Code Skills Lab after each change