Know Your Path/Directory
Tags: beginners, html, css, programming
Published: 2023-01-18 06:34:22


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb769aa5rsk6fk6sxz9b.jpg)
## What is file 📂 path/directory

A file path directory refers to the location of a file or folder within a file system. It is the sequence of folders that must be navigated to find the file or folder.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqeltw2rfrejbybmqau4.jpeg)

## Types of File Paths

File paths can be either relative or absolute.

### Relative File Path

A relative file path starts from the current directory and specifies the location of the file or folder relative to that directory. An absolute file path starts from the root directory of the file system and specifies the location of the file or folder relative to that root directory.

### Absolute File Path

On Windows, file paths are separated by backslashes () while on Linux and macOS, file paths are separated by forward slashes (/).

For example, the file path "C:\Users\username\Documents\file.txt" on Windows is an absolute path that describes the location of the file "file.txt" in the Documents folder of the user "username" on the C: drive.

A relative file path would be something like "../../file.txt" which would refer to the file.txt located 2 folders up from the current folder.

In addition to file paths, there are also URL paths which are used to navigate through a website and locate resources such as images, videos, and scripts. URLs use forward slashes (/) to separate the different sections of the path, and they always start with the protocol (such as http or https) and the domain name (such as www.example.com)

It's important to understand how file paths and directories work in order to navigate through your file system and locate files and folders.

## File Path Uses

In HTML and CSS, file paths are used to link to external resources such as images, videos, and stylesheets.

### Using file paths in HTML

In HTML, file paths are used to link to external resources using the src attribute in elements such as <img> and <script>. For example, the following code will display an image on a webpage using the file path "images/logo.png":

```<img src="images/logo.png" alt="Logo">```

For linking CSS file using the file path "styles/main.css":

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

Similarly, the following code will link to an external JavaScript file using the file path "js/script.js":

`
<script src="js/script.js"></script>`

### Using File Path in CSS

In CSS, file paths are used to link to external resources such as fonts and background images using the @font-face and background-image properties. For example, the following code will link to an external font file using the file path "fonts/custom-font.ttf":

`@font-face {
  font-family: 'CustomFont';
  src: url('fonts/custom-font.ttf') format('truetype');
}`

Similarly, the following code will set a background image for an element using the file path "images/bg-image.jpg":

`element {
  background-image: url("images/bg-image.jpg");
}`

It's also important to note that when using relative file paths, the path is relative to the location of the HTML or CSS file. For example, if the CSS file is in a directory called "css" and the image is in a directory called "images", the file path should be "../images/image.jpg"

Additionally, if you're working on a project with a complex file structure and you want your file paths to be more maintainable and scalable, you could use preprocessors such as SASS and LESS, they allow you to use variables to store file paths and easily update them throughout your project.

**NOTE:** It's important to ensure that the file paths are correct and that the files exist in the specified locations. If the file path is incorrect or the file does not exist, the resource will not be displayed or used on the webpage