HTML File Paths

HTML File Paths: Locating Resources

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.


Types of File Paths

There are two main ways to reference a file path in HTML: Absolute and Relative.

1. Absolute File Paths

An absolute file path specifies the full URL or complete location of a resource, starting from the root of the website or including the domain name.

Absolute Path Syntax

<img src="https://www.example.com/images/picture.png" alt="My Image">

Absolute File Paths Example:

<!DOCTYPE html>
<html>
<head>
    <title>Absolute file path</title>
</head>
<body>
    <h2>Loading an image from an external URL</h2>
    <img src="images/intricate.png" 
         alt="Absolute Path Image" 
         style="width: 400px;" />
</body>
</html>

2. Relative File Paths

Relative file paths locate resources based on the HTML file’s current location, keeping links portable.

Relative Path Syntax

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

Relative File Paths Example:

<!DOCTYPE html>
<html>
<head>
    <title>Relative file path</title>
</head>
<body>
    <h2>File present in the same folder or subfolder</h2>
    <img src="images/intricate.png" 
         alt="IntricateDevo Logo" 
         style="width: 400px;">
</body>
</html>

In this example, the relative file path "images/intricate-logo.png" indicates that the image file is located in a subfolder named "images" relative to the current HTML file.


Relative Path Variants

When using relative file paths, you can use specific characters to navigate through your project's folders:


Best Practices for Using HTML File Paths

To maintain a healthy and easily manageable project, follow these best practices:


Exercise

?

Which relative path notation is used to move UP one directory level before looking for a file?