The Wayback Machine - https://web.archive.org/web/20251226093507/https://www.geeksforgeeks.org/html/html-file-paths/

HTML File Paths

Last Updated : 29 Nov, 2025

HTML file paths tell the browser where to find resources like images, videos, scripts, and other documents. They ensure that all required files load correctly on a webpage.

  • File paths help the browser locate and load external resources.
  • The src attribute is used to specify the path for files like images, videos, or scripts.
  • Example: <img src="path/image.jpg" alt="description"> inserts an image using its file path.

Types of File Paths

There are two main types of HTML File Paths:

  1. Absolute File Paths
  2. Relative File Paths

1. Absolute File Paths

  • Point directly to a resource's location on the internet and include the full URL, which consists of the protocol (http:// or https://), domain, and path to the resource.
  • Best for resources that are hosted externally. The browser knows exactly where to find them regardless of the current document’s location.

Syntax:

<img src="https://media.geeksforgeeks.org/wp-content/uploads/geek.png" alt="My Image">

Example:

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Absolute file path</title>
</head>
<body>
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/geek.png" 
         alt="My Image" 
         style="width: 400px" />
</body>
</html>

Output:

Absolute File Paths example output

2. Relative File Paths

  • Specify the path to a resource in relation to the location of the HTML file currently being viewed.
  • Ideal for resources within the same website. Keeps your HTML portable if the domain changes since the path doesn’t need to be updated.

Syntax:

<img src="images/geeks.jpg" alt="My Image">

Example:

In This example, the relative file path "images/geeks.jpg" indicates that the image file "geeks.jpg" is located in a subfolder named "images" relative to the current HTML file.

index.html
<!DOCTYPE html>
<html>
<head>
    <title>Relative file path</title>
</head>
<body>
    <h2>File present in the same folder</h2>
    <img src="images/geeks.jpg" 
         alt="My Image" 
         style="width:400px">
</body>
</html>

Output:
 

output

Relative Path Variants

  • Document-relative paths: As in the above example, the path starts from the directory of the current HTML document.
  • Root-relative paths: Start with a slash (/), which tells the browser to look for the resource starting from the root directory of the server. Example:
<img src="/images/geeks.jpg">
  • Directory-relative paths: Use dot notation to navigate the directory structure:
    • ./ refers to the current directory.
    • ../ moves up one directory level.
<img src="../images/geeks.jpg"> <!-- Goes up one directory, then into the images folder -->

Best Practices for Using HTML File Paths

  • Keep a Consistent Structure: Organize your files in a logical structure which makes it easier to manage and reference your resources.
  • Use Relative Paths for Internal Resources: This makes your website more portable and easier to maintain, especially if you migrate to a different domain.
  • Test Paths Locally and on the Server: Paths that work on your local machine may not function the same way on a web server due to different directory structures or permissions.
  • Avoid Spaces in Filenames: Spaces can cause issues in URLs and make linking more complex. Use hyphens or underscores instead.
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment