What is a relative path? What is an absolute path?

Source: Internet
Author: User
Tags code tag join joins relative root directory
Found that the problem has been a long time, do the site often encounter such problems, especially in CSS and some of the code JS to see others write some code, chain station files, really let me understand. To now on the relative path and absolute path, although read related, the teacher's words are simply mentioned, the link problem is still a little vague. Think up today to Baidu search a bit, find a more comprehensive answer to the description reproduced down. Found that the problem has been a long time, do the site often encounter such problems, especially in CSS and some of the code JS to see others write some code, chain station files, really let me understand. To now on the relative path and absolute path, although read related, the teacher's words are simply mentioned, the link problem is still a little vague. Think up today to Baidu search a bit, find a more comprehensive answer to the description reproduced down.

The following articles are reproduced in this question:

If you have just started to contact web design, is this often the case? Good web pages on their own machine can be normal browsing, and the page uploaded to the server will always appear to see no pictures, 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 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
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.

How to modify the path of a style sheet:

Open the HTM file using a text editor, view the source code, find <link rel= "stylesheet" href= "Test.css" type in the beginning of the source code Error writing examples:.. /.. /.. /css/test.css
This writing is not correct, in this case for the index.htm file. /.. /.. The absolute path represented by/CSS/TEST.CSS is: c:/css/test.css

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.

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.