Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 122 ยท Links and Navigation

File Paths and URLs

Learn how URLs are built and how browsers resolve relative paths like ./ and ../, root-relative paths and absolute URLs, with folder examples and the base element.

Every link, image, stylesheet and script on a page points to a file with a URL. Get the address wrong and you get a broken image or a "404 Not Found" page. Get it right and your site keeps working even when you move it to a new domain.

This lesson explains what a URL is made of, the three ways to write an address in HTML (absolute, root-relative and relative), and how the browser turns a short path like ../images/logo.svg into a full address.

The parts of a URL

Here is a complete URL with every common part filled in:

TEXT
https://shop.example.com:8443/books/fiction/index.html?sort=new&page=2#reviews
Part In the example What it is
Scheme https: The protocol. Nearly always https on today's web.
Host shop.example.com The server's domain name (or an IP address).
Port :8443 Optional. Left out, it defaults to 443 for https and 80 for http.
Path /books/fiction/index.html Which file or page on that server, written like folders.
Query ?sort=new&page=2 Optional extra data as name=value pairs, separated by &.
Fragment #reviews Optional. A spot inside the page. It is never sent to the server.

The scheme, host and port together are called the origin. Two URLs with the same origin belong to the same site as far as the browser is concerned.

You do not have to split URLs by eye. JavaScript's built-in URL object parses any address into these parts, exactly the way the browser does. Edit the address in the example and run it again:

Taking a URL apart

Try removing the :8443: the port becomes empty because 443 is the default for https. Try deleting https:// and the parser refuses, because a URL without a scheme is not absolute. That is exactly the mistake behind many broken links, as you will see below. The searchParams line shows how scripts read single values out of the query string.

Three ways to write an address

Absolute URLs

An absolute URL contains the scheme and host. It works the same no matter which page it appears on:

HTML
<a href="https://example.org/events/">Upcoming events</a>

Use absolute URLs to link to other websites. For pages on your own site they work too, but they tie your code to one domain, which makes moving between a local copy, a staging server and the live site harder.

Root-relative paths

A path that starts with a single / begins at the root of the current site, the top-level folder of the domain:

HTML
<a href="/about/team.html">Meet the team</a>
<img src="/images/logo.svg" alt="Company logo">

On any page of https://example.com, those resolve to https://example.com/about/team.html and https://example.com/images/logo.svg. Root-relative paths are popular because they work from every page, at any folder depth. The lessons on this site link to each other that way, for example /html/links.

Root-relative paths need a server. If you open an HTML file directly from your disk (the address bar shows file:///...), the "root" is the root of your whole drive, not your project folder. Use a local development server, or relative paths, while you experiment.

Relative paths

A path that does not start with a scheme or a / is relative to the folder of the current page:

Path Meaning
contact.html A file in the same folder.
./contact.html Exactly the same thing; ./ means "this folder".
images/cat.jpg Go down into the images folder.
../index.html Go up one folder.
../../style.css Go up two folders.

Following paths through a real folder tree

Imagine a small site with this structure:

TEXT
my-site/
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ css/
โ”‚   โ””โ”€โ”€ style.css
โ”œโ”€โ”€ images/
โ”‚   โ””โ”€โ”€ logo.svg
โ””โ”€โ”€ blog/
    โ”œโ”€โ”€ index.html
    โ””โ”€โ”€ 2026/
        โ””โ”€โ”€ first-post.html

From blog/2026/first-post.html, here is how to reach the other files:

HTML
<!-- Same folder, sibling page -->
<a href="second-post.html">Next post</a>

<!-- Up one folder: the blog index -->
<a href="../index.html">All posts</a>

<!-- Up two folders: the home page -->
<a href="../../index.html">Home</a>

<!-- Up two, then down into images/ -->
<img src="../../images/logo.svg" alt="My Site">

<!-- The same image, root-relative -->
<img src="/images/logo.svg" alt="My Site">

Each ../ climbs exactly one folder. Read a path from left to right like directions: "go up, go up, go into images, take logo.svg".

Watch the browser resolve paths

Browsers resolve every relative path against the address of the current page, called the base URL. JavaScript's URL constructor uses the same rules, so you can try them out. Change the base or add your own paths:

Resolving relative URLs

The last row shows a protocol-relative URL (//cdn.example.net/...). It keeps the current scheme and replaces the host. It was common when sites served both http and https; today, write https:// explicitly instead.

Practice: plan your own paths

Reading paths is one skill; writing them is another. In the next example, choose which page you are "on" and type a path. The browser's own rules resolve it and show where you would land, and whether that file exists in the small site from earlier.

Path practice

Some challenges to try:

  • From /blog/2026/first-post.html, reach the logo with a relative path. Then do it with a root-relative path.
  • From /index.html, reach the blog's index page. Does blog/ work, or do you need blog/index.html? (In this simulation only exact file names count; a real server would usually serve the index for you.)
  • Type ../../../../index.html from the home page. The browser cannot climb above the root, so the extra ../ steps are simply ignored.

Common path mistakes

Most broken links and missing images come from a short list of slips:

  • Forgetting the scheme. href="www.example.org" looks like a web address, but without https:// the browser treats it as a relative path and looks for a folder called www.example.org on your own site.
  • Backslashes. Windows shows paths as images\logo.svg. URLs always use forward slashes: images/logo.svg.
  • Paths to your own disk. src="C:/Users/sam/Desktop/site/logo.svg" or file:///... works on your computer only. Every visitor's browser would look on their disk.
  • One ../ too many or too few. Count the folders between the current page and the target, starting from the page's own folder, not from the page file.
  • A stray leading slash. /images/logo.svg and images/logo.svg are different paths. The first starts at the site root; the second starts at the current folder.

Debugging a broken path: open the Network panel in DevTools and reload. Failed requests show up in red with the full URL the browser actually tried. Comparing that URL with where the file really lives usually reveals the problem in seconds.

Case sensitivity and file names

The scheme and host are not case-sensitive: HTTPS://Example.COM is the same as https://example.com. The path is a different story. Most web servers run on Linux, where Logo.svg and logo.svg are two different files. Windows and macOS usually ignore case on your own computer, so a mismatch can work perfectly while you develop and then break as soon as you publish.

A few habits avoid that whole class of bugs:

  • Use lowercase for every file and folder name.
  • Use hyphens instead of spaces: summer-menu.html, not Summer Menu.html. Spaces must be encoded as %20 in URLs, which is easy to get wrong (see URL Encoding).
  • Stick to letters, digits, hyphens and dots.

Folder URLs and index.html. When a URL ends in a folder, such as /blog/, most servers send that folder's index.html. That is why you can link to /blog/ instead of /blog/index.html, and it gives you cleaner addresses.

Changing the base with <base>

Normally the base URL is simply the address of the page. The <base> element, placed in the <head>, lets you change it for the whole document:

HTML
<head>
  <base href="https://example.com/docs/">
</head>
<body>
  <!-- Resolves to https://example.com/docs/intro.html -->
  <a href="intro.html">Introduction</a>
</body>

Only one <base> is allowed, and it must come before any other element that uses a URL. Use it rarely: it affects every relative URL on the page, including fragment links. With the base above, <a href="#top"> points to https://example.com/docs/#top and leaves the page instead of scrolling. You will see <base> again in The head Element.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which part of a URL is never sent to the server?

    The fragment (everything after #) is handled by the browser only, to find a spot inside the page.
  2. 2. The current page is https://example.com/shop/shoes/boots.html. Where does ../cart.html point?

    The page lives in /shop/shoes/. ../ climbs one folder to /shop/, then adds cart.html.
  3. 3. What does a path that starts with a single / (like /images/logo.svg) start from?

    Root-relative paths start at the top of the current origin, so they work from any page of the site.
  4. 4. Your image works on your laptop but is broken on the live server. The file is called Photo.JPG and the src says photo.jpg. Why?

    Most servers treat paths as case-sensitive, so Photo.JPG and photo.jpg are different files. Use lowercase names everywhere.
  5. 5. A link is written as href="www.example.org". What happens when someone clicks it?

    Without a scheme such as https://, the browser has no way to know it's another site, so it resolves the text as a relative path.

Summary

  • A URL is made of a scheme, host, optional port, path, optional query and optional fragment.
  • Absolute URLs include the scheme and host; use them for other sites.
  • Root-relative paths start with / and begin at the site's root.
  • Relative paths start from the current page's folder; ./ means "here" and ../ means "up one folder".
  • Paths are usually case-sensitive on servers, so name files in lowercase with hyphens.
  • <base href> changes the base URL for every relative URL on the page, so use it with care.

Next lesson: Link Targets and Page Jumps, where you will control where links open and jump to exact spots on a page.