HTML File Paths

HTML File Paths

An HTML file path specifies the location of a file that you want to include or link to in your HTML document. This could be images, stylesheets, scripts, or other HTML files. Understanding file paths is crucial for creating well-organized and functional websites.

Types of File Paths

There are two main types of file paths in HTML:
⦁ Relative Paths
⦁ Absolute Paths

1. Relative Paths

A relative path points to a file relative to the location of the current document. It’s like giving directions from your current location.
There are a few ways to use relative paths:

Same Directory

If the file is in the same directory as the HTML file.

Example:

<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <title>Image Example – Same Directory</title>
</head>
<body>
    <h1>Image in the Same Directory</h1>
    <img src=”study.jpg” alt=”Image in the same directory”>
</body>
</html>

Output:
In this example:

⦁ <img src=”study.jpg” alt=”Image in the same directory”>: Embeds an image in the document.
⦁ src=”study.jpg”: Specifies the path to the image file. In this case, it indicates that the image file study.jpg is in the same directory as the HTML file.

Subdirectory

If the file is in a subdirectory of the current directory.

Example:

<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <title>Image Example – Subdirectory</title>
</head>
<body>
    <h1>Image in a Subdirectory</h1>
    <img src=”images/log.jpg” alt=”Image in a subdirectory”>
</body>
</html>

Output:
In this example:

⦁ <img src=”images/log.jpg” alt=”Image in the same directory”>: Embeds an image in the document.
⦁ src=”images/log.jpg”: Specifies the path to the image file. In this case, it indicates that the image file log.jpg is located in the images subdirectory relative to the HTML file.

Parent Directory

If the file is in the parent directory of the current directory.

Example:

<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <title>Image Example – Parent Directory</title>
</head>
<body>
    <h1>Image in the Parent Directory</h1>
    <img src=”../home-office.jpg” alt=”Image in the parent directory”>
</body>
</html>

Output:
In this example:

⦁ <img src=”../home-office.jpg” alt=”Image in the parent directory”>: Embeds an image in the document.
⦁ src=”../home-office.jpg”: Specifies the path to the image file. In this case, it indicates that the image file home-office.jpg is located in the parent directory of the directory containing the HTML file.

Nested Directories

If the file is in a deeper directory structure.

Example:

<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <title>Image Example – Nested Directories</title>
</head>
<body>
    <h1>Image in Nested Directories</h1>
    <img src=”assets/images/coffee.jpg” alt=”Image in nested directories”>
</body>
</html>

Output:
In this example:

⦁ <img src=”assets/images/coffee.jpg” alt=”Image in nested directories”>: Embeds an image in the document.
⦁ src=”assets/images/coffee.jpg”: Specifies the path to the image file. In this case, it indicates that the image file coffee.jpg is located in the images subdirectory, which is within the assets subdirectory, relative to the HTML file.

2. Absolute Path

An absolute path is the complete URL or full address of a file or resource on the internet. It tells you exactly where the file is located, no matter where you are on the web.

Example:

<!DOCTYPE html>
<html lang=”en”>
<head>
    <title>Image Example – Absolute Path</title>
</head>
<body>
    <h1>Image with Absolute Path</h1>
    <img src=”https://iqratechnology.com/wp-content/uploads/2024/05/image.jpg” width=”400″>
</body>
</html>

Output:
In this example:

<img src=”https://iqratechnology.com/wp-content/uploads/2024/05/image.jpg” width=”400″>: Embeds an image in the document.
⦁ src=”https://iqratechnology.com/wp-content/uploads/2024/05/image.jpg”: Specifies the absolute URL to the image file. This URL points to an image hosted on the iqratechnology.com website.
⦁ width=”400″: Sets the width of the image to 400 pixels.

Best Practices

⦁ Relative Paths:
⦁ Same Directory: Use the filename directly (e.g., src=”image.jpg”).
⦁ Subdirectory: Include the subdirectory in the path (e.g., src=”images/image.jpg”).
⦁ Parent Directory: Use .. to go up one level (e.g., src=”../image.jpg”).
⦁ Nested Directories: Chain directories together (e.g., src=”images/photos/image.jpg”).

⦁ Absolute Paths:
⦁ Provide the full URL of the resource (e.g., src=”https://www.example.com/images/image.jpg” ).
Understanding and using the correct file paths ensures that your images and other resources load correctly, enhancing the functionality and appearance of your website

YouTube Reference :

Practice Scenarios

Scenario 1: Relative File Path

Objective: Link to an image using a relative file path.
Expected Output: A web page displaying an image located in a subfolder.

Directory Structure:

<!DOCTYPE html>
<html>
<head>
    <title>Relative File Path Example</title>
</head>
<body>
    <h1>Relative File Path Example</h1>
    <img src=”images/example.jpg” alt=”Example Image”>
</body>
</html>

Output:

Scenario 2: Parent Directory Path

Objective: Link to a JavaScript file located in the parent directory.
Expected Output: A web page that executes a JavaScript file located in the parent directory.

Directory Structure:

HTML Code (subfolder/index.html):

<!DOCTYPE html>
<html>
<head>
    <title>Parent Directory Path Example</title>
<script src=”../script.js”></script>
</head>
<body>
    <h1>Parent Directory Path Example</h1>
</body>
</html>

JavaScript Code (script.js):

alert(‘This is a script from the parent directory!’);

Output:

Scenario 3: Same Directory Path

Objective: Link to a stylesheet located in the same directory.
Expected Output: A web page styled using a CSS file located in the same directory.

Directory Structure:

HTML Code (index.html):

<!DOCTYPE html>
<html>
<head>
    <title>Same Directory Path Example</title>
    <link rel=”stylesheet” href=”style.css”>
</head>
<body>
    <h1>Same Directory Path Example</h1>
    <p>This page is styled using a CSS file located in the same directory.</p>
</body>
</html>

CSS Code (style.css):

body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
text-align: center;
}

h1 {
color: #333;
}

p {
color: #666;
}

Output:

Scenario 4: Linking to a File in a Subdirectory

Objective: Link to a JavaScript file located in a subdirectory.
Expected Output: A web page that executes a JavaScript file located in a subdirectory.

Directory Structure:

Output:

Scenario 5: Linking to a File in a Nested Subdirectory

Objective: Link to a CSS file located in a nested subdirectory.
Expected Output: A web page styled using a CSS file located two levels deep in a subdirectory.

Directory Structure:

Output:

Scenario 6: Linking to a File in a Different Subdirectory

Objective: Link to a JavaScript file located in a different subdirectory.
Expected Output: A web page that executes a JavaScript file located in a sibling subdirectory.

Directory Structure:

Output: