Absolute path and relative path detailed

Source: Internet
Author: User

For example, your C-disk has two files 1 and 2 under the ABC folder.

If you want file 1 to say the location of file 2 (that is, the path), then there are two ways to represent it:
C:\ABC\2
This is the absolute path, indicating that the 2 file is represented from the largest directory, C disk, under the C disk ABC file.
2
This is the relative path, because 1 files and 2 files are under the C:\ABC, so their path in front of the "C:\ABC" is the same, you do not have to show it.


and upload the page to the server will always appear not see the picture, CSS stylesheet failure and other errors. This is mostly because you use the wrong path and use an absolute path where the relative path should be used, causing the browser to not open the specified file in the specified location.

Let's talk about the difference between the relative path and the absolute path that make the beginner headache.

What is an absolute path:

As we all know, when we use the computer to find the necessary files must know the location of the file, and the way to represent the location of the file is the path, such as 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 http://www.pckings.net/img/photo.jpg to determine the location of a file.

In addition, 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 directory of the 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.
What is a relative path:

Let's first 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
How should the photo.jpg in the index.htm in this example 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 relative path to locate the file


Cases:
C:/website/web/xz/index.htm
C:/website/img/images/photo.jpg
How should the photo.jpg in the index.htm in this example 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
How should the photo.jpg in the index.htm in this example 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.
Finally, to avoid a path error when making a Web page, we can use the Dreamweaver site management feature to manage the site. As long as you use the menu command site-new site to create new sites and define the site directory, it will automatically turn the absolute path into a relative path, and when you move files in the site, the connection path associated with these files will automatically change, it is very convenient.

Absolute path, from the root directory to your directory;
Relative paths, from a directory to the starting point to your directory.

For example:
┍a folder
C-|
┕b folder

Absolute path: C:\a folder

Relative path (if you are in folder B):.. \b Folder ('.. \ ' Up-level meaning '


Everyone is interested can add group, discuss study together, group two-dimensional code:


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.