How to use the href attribute of the HTML a tag to specify relative and absolute paths

How to use the href attribute of the HTML a tag to specify relative and absolute paths

In actual Web development, inserting images, including CSS files, etc. all require a path. If the file path is added incorrectly, the reference will become invalid (unable to browse the linked file, or unable to display the inserted image, etc.). Many beginners are confused. Below I will introduce relative paths and absolute paths in detail.

HTML relative path
Refers to the path relationship between this file and other files (or folders) caused by the path where this file is located.
For example:
The absolute path of file 1.htm is: d:/www/html/1.htm
The absolute path of file 2.htm is: d:/www/html/2.htm
Then: the path of 1.htm relative to 2.htm is: 1.htm

How to use relative links:
If you are linking to the same directory, just enter the name of the document you want to link to, for example:

XML/HTML CodeCopy content to clipboard
  1. <   href ="1 htm " > Web link </a>   
  2. < img   src ="bg.jpg" />   

If you want to link to the next level directory, you need to enter the directory name first, then add "/", and then enter the file name, for example:

XML/HTML CodeCopy content to clipboard
  1. <   href ="html/next.htm" >   
  2. < img   src ="images/bg.jpg" />   

If you want to connect to the upper-level directory, you need to enter "../" first, and then enter the directory name and file name, for example:

XML/HTML CodeCopy content to clipboard
  1. <   href = "../www/index.htm" >   

Example summary
Now there are 4 HTML files: aaa.html bbb.html ccc.html index.html
The path of aaa.html is: D:/www/adminwang/html/aaa.html
The path of bbb.html is: D:/www/adminwang/bbb.html
The path of ccc.html is: D:/www/ccc.html
The path of index.html is D:/www/index.html

1. Link files in the same directory
For example, the code for the ccc.html file to link to index.html is as follows:

XML/HTML CodeCopy content to clipboard
  1. <   href = "index.html" > Link to index page </ a >   


2. Link to the file in the previous directory
For example, the code for bbb.html to link ccc.html is as follows:

XML/HTML CodeCopy content to clipboard
  1. <   href = "../ccc.html" > Link to ccc webpage </ a >   


3. Link to files in the upper 2 directories
For example, the code for linking aaa.html to ccc.html is as follows:

XML/HTML CodeCopy content to clipboard
  1. <   href = "../../ccc.html" > Link to ccc webpage </ a >   


4. Link files in lower-level directories
For example, the code for linking ccc.html to bbb.html is as follows:

XML/HTML CodeCopy content to clipboard
  1. <   href = "adminwang/bbb.html" > Link to bbb webpage </ a >   


5. Link to files in the next 2 directories
For example, the code for ccc.html to link aaa.html is as follows:

XML/HTML CodeCopy content to clipboard
  1. <   href = "adminwang/html/aaa.html" > Link to aaa webpage </ a >   

HTML absolute path
Provide the full path to the file, including the protocol or drive letter where applicable. That is, the actual complete path of the file or directory on your homepage on the hard disk. For example:
http://www.adminwang.com/index.htm
d:/ www /html/images/bg.jpg
There is nothing much to say about absolute paths. Once you have mastered relative paths, absolute paths will be very simple.

<<:  Sample code for implementing horizontal infinite scrolling with pure CSS3

>>:  25 Ways and Tips to Increase Web Page Loading Speed

Recommend

The difference between div and table in speed, loading, web application, etc.

1: Differences in speed and loading methods The di...

vue+ts realizes the effect of element mouse drag

This article example shares the specific code of ...

How to use lazy loading in react to reduce the first screen loading time

Table of contents use Install How to use it in ro...

CSS transparent border background-clip magic

This article mainly introduces the wonderful use ...

Summary of common Linux distribution mirror source configuration

I have been researching Linux recently and tried ...

MySQL SHOW PROCESSLIST assists in the entire process of troubleshooting

1. SHOW PROCESSLIST command SHOW PROCESSLIST show...

How to detect if the current browser is a headless browser with JavaScript

Table of contents What is a headless browser? Why...

VUE+Canvas realizes the whole process of a simple Gobang game

Preface In terms of layout, Gobang is much simple...

MySQL Order By Multi-Field Sorting Rules Code Example

Say it in advance On a whim, I want to know what ...

A brief discussion on the design and optimization of MySQL tree structure tables

Preface In many management and office systems, tr...

js uses the reduce method to make your code more elegant

Preface In actual projects, the most common proce...

Detailed explanation of Javascript string methods

Table of contents String length: length charAt() ...