Basic HTML directory problem (difference between relative path and absolute path)

Basic HTML directory problem (difference between relative path and absolute path)
Relative path - a directory path established based on the location of the web page that references the file. Therefore, when web pages saved in different directories reference the same file, the paths used will be different, so it is called relative.
Absolute path - a directory path based on the root directory of the Web site. It is called absolute because when all web pages reference the same file, the path used is the same.
In fact, the difference between an absolute path and a relative path is only that different reference points are used when describing the directory path. Since the root directory is the same reference point for all files on the website, the path description method using the root directory as a reference point is called an absolute path.
The following are several special symbols used to establish paths and their meanings.
"."--represents the current directory.
".."--represents the upper directory.
"/"--represents the root directory.
Next, we assume that the website created by the reader has the directory path shown in the following figure.

If you want to reference the BeRef.gif file in the Ref.htm file, the relative path is as follows:

./SubDir2/BeRef.gif

In the reference path above, "." represents the current directory (Dir1), so "./SubDir2" represents SubDir2 in the current directory. In fact, you can also omit "./" and directly use this method to quote.

SubDir2/BeRef.gif

If you use an absolute path to reference the file with the root directory as the reference point, the reference path is as follows:

/Dir1/SubDir2/BeRef.gif

If the directory structure of the Web site is as shown below

What about the relative path of the BeRef.gif file?

If you want to reference the BeRef.gif file in the Ref.htm file, its relative path is as follows:

../SubDir2/BeRef.gif

In the reference path above, ".." represents the parent directory, so "/Dir2" represents the Dir2 subdirectory under the parent directory. If an absolute path is used for reference, the reference path is as follows:

/Dir2/BeRer.gif

Let's take a more complicated example to compare the use of relative paths and absolute paths. Assume that the website you created has the directory path as shown in the figure below.


We use a table to illustrate the relative path and absolute path that should be used when a file references another file in the above situation.

Cited by
Cited by
Relative Path
Absolute path
Ref1.htm BeRef1.gif ../SubDir2/BeRef1.gif /Dir1/SubDir2/BeRef1.gif
Ref2.htm BeRef1.gif ../../Dir1/SubDir2/ BeRef1.gif /Dir1/SubDir2/ BeRef1.gif
Ref1.htm BeRef2.htm ../../Dir2/ BeRef2.htm /Dir2/BeRef2.htm
Ref2.htm BeRef2.htm ../BeRef2.htm /Dir2/BeRef2.htm

What needs to be explained in the above table is the meaning of "../../".

".." represents the parent directory, and "../../" represents the parent directory of the parent directory. Therefore, as can be seen from the above table, if the referenced file exists in a subdirectory of the current directory, or in another subdirectory of the upper directory, it is more convenient to use a relative path. If not, just use the absolute path, which is more convenient. From the above table, we can also see that when the same file is referenced, the absolute path used to reference the file is the same.

<<:  Detailed use cases of vue3 teleport

>>:  How to connect Navicat to the docker database on the server

Recommend

UTF-8 and GB2312 web encoding

Recently, many students have asked me about web p...

How to use jsx syntax correctly in vue

Table of contents Preface Virtual DOM What is Vir...

How to use CSS media query aspect-ratio less

CSS media query has a very convenient aspect rati...

MySQL complete collapse: detailed explanation of query filter conditions

Overview In actual business scenario applications...

Detailed explanation of JavaScript prototype chain

Table of contents 1. Constructors and instances 2...

Docker port mapping and external inaccessibility issues

The Docker container provides services and listen...

Build a file management system step by step with nginx+FastDFS

Table of contents 1. Introduction to FastDFS 1. I...

Web designers should optimize web pages from three aspects

<br />With the increase of bandwidth, there ...

Vue conditional rendering v-if and v-show

Table of contents 1. v-if 2. Use v-if on <temp...

Analysis of the process of building a LAN server based on http.server

I don’t know if you have ever encountered such a ...

How to turn off eslint detection in vue (multiple methods)

Table of contents 1. Problem Description 2. Probl...