The difference between relative path and absolute path _css/html

Source: Internet
Author: User
Absolute path:

We need to know the location of the file when we use the computer, and the way to represent the location of the file is the path, for example, just see the path: c:/website/img/ Photo.jpg We know the photo.jpg file is in an IMG subdirectory under the website directory in C disk. A path similar to the complete description file location is an absolute path. We do not need to know any other information to determine the location of the file according to the absolute path. In a Web site, it is also an absolute path to yun_qi_img/photo.jpg to determine the location of a file.

In the application of the website, we usually use "/" to represent the root directory,/img/photo.jpg means the photo.jpg file is in the IMG directory on the root of this website. But this is risky for beginners, knowing that the root directory is not the root of your site, but the root of the server where your Web site resides, so there will be an error when the site's root directory is not the same as the server root.
Relative path:

Analyze why the picture does not display correctly. For example, there is now a page index.htm that joins a picture photo.jpg in this page. Their absolute path is as follows:
C:/website/index.htm
C:/website/img/photo.jpg

If you use an absolute path c:/website/img/photo.jpg, then everything is fine on your own computer, because you can do it in the specified location, c:/website/img/ Photo.jpg found on the photo.jpg file, but when you upload the page to the site is likely to go wrong, because your site may be in the server C, may be in D, or in the AA directory, more likely in the BB directory, in short, there is no reason to have c:/website/ Img/photo.jpg such a path. So, what path do you want to use to locate the Photo.jpg file in the index.htm file? Yes, it should be a relative path, the so-called relative path, as the name suggests is their relative and target position. Photo.jpg that are joined in index.htm in the previous example can use Img/photo.jpg to locate files, so no matter where they are placed, they do not go wrong as long as their relative relationship does not change.

In addition we use ".. /"to represent the previous level of the directory,".. /.. /"represents the directory on the ancestor, and so on. (Friends who have studied dos may be easier to understand)

Take a look at a few more examples and note that all of the examples are index.htm with a picture photo.jpg in the join file.

Cases:
C:/website/web/index.htm
C:/website/img/photo.jpg
In this case, how should the photo.jpg of the index.htm join be represented?
Wrong wording: img/photo.jpg
This writing is not correct, in this case, for the index.htm file img/photo.jpg represents the absolute path is: c:/website/web/img/photo.jpg, obviously does not meet the requirements.
Use the correct wording: /img/photo.jpg the relative path to locate the file


Cases:
C:/website/web/xz/index.htm
C:/website/img/images/photo.jpg
In this case, how should the photo.jpg of the index.htm join be represented?
Wrong wording:. /img/images/photo.jpg
This writing is not correct, in this case for the index.htm file. The absolute path represented by/img/images/photo.jpg is: c:/website/web/img/images/photo.jpg.
Correct writing: You can use ... /.. /img/images/photo.jpg the relative path to locate the file


Cases:
C:/website/web/xz/index.htm
C:/website/web/img/photo.jpg
In this case, how should the photo.jpg of the index.htm join be represented?
Wrong wording:. /.. /img/photo.jpg
This writing is not correct, in this case for the index.htm file. /.. The absolute path represented by/img/photo.jpg is: c:/website/img/photo.jpg.
Correct writing: You can use ... /img/photo.jpg the relative path to locate the file


Summary: Through the above examples can be found, in the absolute path to the relative path, two files absolute path of the same part can be ignored, do not consider. Just think about what they are different.

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.