The difference between a relative path and an absolute path

Source: Internet
Author: User

in HTML, the concept of absolute and relative paths is involved whenever a file is involved (such as hyperlinks, images, etc.).  1. Absolute pathan absolute path is a path that a file really exists on the hard disk. For example "bg.jpg" This picture is stored in the hard disk "E:\book\ page layout code \ 2nd Chapter" directory, then "bg.jpg" the absolute path of this picture is "E:\book\ page cloth \ Code \ 2nd Chapter \bg.jpg". Then you should use the following statement if you want to specify a background picture of a Web page using an absolute path:<body backround= "E:\book\ page layout \ Code \ 2nd Chapter \bg.jpg" > 2. Disadvantages of using absolute pathsIn fact, in the Web page programming, rarely use absolute path, if use "E:\book\ page cloth \ Code \ 2nd Chapter \bg.jpg" To specify the location of the background picture, browsing on their own computer may be normal, However, uploading to the Web server will most likely not show the image. Since uploading to a Web server, it is possible that the entire Web site does not have an E drive on the Web server, possibly a D or H drive. Even if placed in the Web server e-disk, the Web server e-disk will not necessarily exist "E:\book\ page layout \ Code \ 2nd Chapter" This directory, so when browsing the Web page will not display the picture.  3. Relative pathIn order to avoid this situation, it is often necessary to use relative paths when specifying files in a Web page. The so-called relative path is relative to its own target file location. For example, the "s1.htm" file refers to the "bg.jpg" image, because the "bg.jpg" image is in the same directory as the "s1.htm", then use the following code in the "s1.htm" file, as long as the relative position of the two files is unchanged ( That is, in the same directory), you can display the image correctly in the browser regardless of where you upload it to the Web server. <body background= "bg.jpg" >One more example, assuming that the "s1.htm" file is in the same directory as "E:\book\ page layout \ code \ 2nd", and "bg.jpg" picture is located in the directory of "E:\book\ page layout \ code \ 2nd \img", then "bg.jpg" picture relative to " S1.htm "file, in the" IMG "subdirectory of the directory in which it is located, the statement referring to the picture should be:<body background= "img/bg.jpg" > Note: The relative path uses the "/" character as the separator character for the directory, and the absolute path can use the "\" or "/" character as the delimiter character for the directory. Since the "IMG" Directory is a subdirectory under the "2nd Chapter" directory, there is no need to add the "/" character before "IMG". often used in relative paths ". /"to represent the previous level of the directory. If there are multiple top-level catalogs, you can use more than one ". /", for example" http://www.cnblogs.com/"represents the upper parent directory. Assuming that the "s1.htm" file is in the same directory as "E:\book\ page layout \ code \ 2nd" and the "bg.jpg" picture is in the same directory as "E:\book\ page layout \ Code", then the "bg.jpg" image is relative to the "s1.htm" file, is in the parent directory of the directory in which it is located, the statement referring to the picture should be:<body background= ". /bg.jpg "> One more example, assuming that the "s1.htm" file is in the same directory as "E:\book\ page layout \ code \ 2nd", and "bg.jpg" picture is located in the directory "E:\book\ page layout \ Code \img", then "bg.jpg" picture relative to "s1.htm "The file is in the" IMG "subdirectory in the parent directory of the directory in which it is located, the statement referring to the picture should be:<body background= ". /img/bg.jpg "> 4. Relative virtual directoryThere is a more specific representation of relative paths: relative virtual directories. Take a look at the following example:<body background= "/img/bg.jpg" > in this example, the value of the Background property is "/img/bg.jpg", and note that there is a "/" character before "IMG". This "/" represents the root directory of the virtual directory. Assuming that the "E:\book\ page layout \ Code" is set to the virtual directory, then "/img/bg.jpg" The Real path is "E:\book\ page layout \ Code \img \bg.jpg", if the "E:\book\ page layout \ Code \ 2nd" Set as a virtual directory, then The true path of "/img/bg.jpg" is "E:\book\ page layout \ Code \ 2nd Chapter \img\bg.jpg"

The difference between a relative path and an absolute path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.