File Paths

File paths are crucial in HTML as they help the browser locate and retrieve files, such as images, stylesheets, scripts, or other web resources. Understanding how to correctly specify file paths is essential for building functional and organized websites. In this chapter, we will cover everything from basic to advanced concepts about file paths, ensuring that by the end, you'll have a complete understanding of how file paths work in HTML.

What is a File Path?

A file path in HTML refers to the location of a file that you want to link or include in your HTML document. It tells the browser where to find the file. There are two main types of file paths:

  1. Absolute Path: The complete URL that points to a file, regardless of the current file location.
  2. Relative Path: A partial file path based on the current file’s location. It points to files within the same directory or structure.

Absolute File Paths

An absolute file path specifies a full URL or an exact location of a file, including the protocol (http://, https://), domain name, and file name. This is used when the file is hosted on an external server.

Example

				
					<img data-lazyloaded="1" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" decoding="async" data-src="https://www.example.com/images/logo.png" alt="Logo">

				
			

In this example, the image is located on an external server, and the full URL is provided to retrieve it. The browser can find this file, no matter where the HTML document is hosted.

Relative File Paths

Relative file paths are shorter and refer to files based on the location of the current HTML document. They are more commonly used for files that reside on the same server.

There are several types of relative paths:

  • Same folder: The file is in the same folder as the HTML document.
  • Subfolder: The file is in a subfolder of the HTML document’s folder.
  • Parent folder: The file is located in the parent folder of the current HTML file.

Examples

1.Same folder

In this case, the image is in the same folder as the HTML file.

				
					<img data-lazyloaded="1" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" decoding="async" data-src="logo.png" alt="Logo">

				
			

2.Subfolder

The image is stored in the “images” subfolder relative to the current HTML file.

				
					<img data-lazyloaded="1" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" decoding="async" data-src="images/logo.png" alt="Logo">

				
			

3.Parent folder

The ../ syntax indicates that the file is one level up in the directory structure (the parent folder).

				
					<img data-lazyloaded="1" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" decoding="async" data-src="../logo.png" alt="Logo">

				
			

Understanding Relative Paths with Directory Structure

Let’s visualize a typical website directory structure:

				
					/website
    /images
        logo.png
    /css
        styles.css
    index.html

				
			

1.To link styles.css from index.html, the relative path would be:

				
					<link rel="stylesheet" href="css/styles.css">

				
			

2.To display logo.png in index.html, the relative path would be:

				
					<img data-lazyloaded="1" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" decoding="async" data-src="images/logo.png" alt="Logo">

				
			

3.If you wanted to reference logo.png from styles.css (in the css folder), the relative path would be:

				
					background-image: url('../images/logo.png');

				
			

Here, ../ moves up one directory (from css to website), and then it goes into the images folder.

Working with URLs and File Paths

In HTML, paths aren’t limited to just files on your server. You can also use URLs to point to resources hosted elsewhere. This is often used for:

  • CDN (Content Delivery Network) resources: e.g., linking to external stylesheets or scripts.
  • External images, videos, or documents.

Example

				
					<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

				
			

This line loads an external stylesheet hosted on a CDN.

File Path Best Practices

  1. Use Relative Paths for Internal Resources: Relative paths are more flexible because they adapt to the document’s current location, making your website easier to move between directories or servers.

  2. Use Absolute Paths for External Resources: When referencing files from another domain (like CDN-hosted stylesheets), use absolute paths.

  3. Organize Your Directories: Keep your project structured. For example:

    1. /images for images
    2. /css for stylesheets
    3. /js for JavaScript files

    This organization makes it easier to manage your project and find files.

  4. Avoid Broken Paths: Make sure the paths you reference are correct and the files exist. A broken file path will result in missing images, non-functional scripts, or unloaded stylesheets.

Advanced Topics

Root Relative Paths

A root relative path starts from the root directory of the website, regardless of where the current HTML file is located. It always begins with a / and is useful when your files may be accessed from multiple directories.

Example

				
					<img data-lazyloaded="1" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" decoding="async" data-src="/images/logo.png" alt="Logo">

				
			

This points to logo.png starting from the root of the website, making it independent of the current file’s directory.

Protocol-relative URLs

When you’re using external resources, sometimes it’s useful to use protocol-relative URLs. These URLs start with //, allowing the browser to decide whether to use HTTP or HTTPS, depending on the protocol of the current page.

Example

				
					<script type="litespeed/javascript" data-src="//code.jquery.com/jquery-3.5.1.min.js"></script> 
				
			

If your page is loaded over HTTPS, the browser will load the jQuery library over HTTPS. If your page is loaded over HTTP, it will load the library over HTTP.

Understanding file paths in HTML is fundamental to building functional websites. Whether you’re linking an image, a stylesheet, or a script, correctly specifying the file path ensures your website works as expected. Happy coding !❤️

Table of Contents