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

Detailed explanation of html-webpack-plugin usage

Recently, I used html-webapck-plugin plug-in for ...

setup+ref+reactive implements vue3 responsiveness

Setup is used to write combined APIs. The interna...

JavaScript implementation of classic snake game

This article shares the specific code of JavaScri...

Introduction to Vue3 Composition API

Table of contents Overview Example Why is it need...

How MySQL uses transactions

Basics A transaction is an atomic operation on a ...

js canvas realizes random particle effects

This article example shares the specific code of ...

mysql method to view the currently used configuration file my.cnf (recommended)

my.cnf is the configuration file loaded when MySQ...

Detailed steps to install MYSQL8.0 on CentOS7.6

1. Generally, mariadb is installed by default in ...

How to use Nginx to handle cross-domain Vue development environment

1. Demand The local test domain name is the same ...

Linux kernel device driver Linux kernel basic notes summary

1. Linux kernel driver module mechanism Static lo...

Pure CSS custom multi-line ellipsis problem (from principle to implementation)

How to display text overflow? What are your needs...

Specific use of MySQL window functions

Table of contents 1. What is a window function? 1...

A brief discussion on MySQL event planning tasks

1. Check whether event is enabled show variables ...