Relative path ※ Absolute path

xiaoxiao2021-03-06  43

If you have just started touching web design, do you often happen this problem? Do a good job in your machine, you can browse it normally, and pass the page to the server, you will always appear to see the picture, the CSS style table fails. In this case, mostly because you use the wrong path, an absolute path should be used where the relative path should be used, causing the browser to open the specified file in the specified location.

Let's take a look at the difference between the relative path to the absolute path of the headaches of beginners.

what is

Absolute path: Everyone knows that when we usually use a computer, you must know the files of the file must know the location of the file, and the way to indicate the location of the file is the path, for example, as long as you see this path: C: / Website / IMG / Photo .jpg We know the photo.jpg file is in the IMG subdirectory in the Website directory of the C drive. A path similar to such a complete description file is an absolute path. We don't need to know any other information, you can determine the location of the file according to the absolute path. The way to determine the file location in the website is also an absolute path.

In addition, in the application of the website, usually we use "/" to represent the root directory, / img / photo.jpg means a photo.jpg file in the IMG directory on the root directory of this website. But this use is a risky for beginners, because we must know that the root directory referred to here is not the root of your website, but the root directory of the server where your website is located, so

An error occurs when the root directory of the site is not at the root directory.

What is a relative path: Let's first analyze why the picture does not display properly. For example, there is now a page index.htm, and there is a picture photo.jpg in this page. Their absolute paths are as follows: c: /website/index.htm c: /website/img/photo.jpg If you use the absolute path C: /Website/img/photo.jpg, then everything is normal on your computer, Because it is indeed possible to find a photo.jpg file on the specified location, you can find a photo.jpg file, but when you transfer the page to the website, it is likely to be wrong because your website may be on the server. C disk, may be on the D disk, or in the AA directory, it is more likely to be in the BB directory, there is no reason to have C: /Website/img/photo.jpg such a path. So, what kind of path you want to use in the index.htm file is positioned with photo.jpg files? Yes, it should be used with relative paths, so-called relative paths, as the name suggests is their relative and target position. In the above example, the photo.jpg in Index.htm can use img / photo.jpg to locate files, so regardless of these files, as long as their relative relationship is not changed, it will not be wrong. In addition, we use ".." to represent the previous directory, "../../" indicate the directory of the upper level, and so on. (Friends who have learned DOS may be more likely to understand)

Look at a few examples, pay attention to all examples of the INDEX.htm file in the INDEX.htm file. There is a picture photo.jpg. Example: C: /Website/web/index.htm c: /website/img/photo.jpg How should PHOTO.JPG in Index.htm in this example? Error Write: img / photo.jpg This method is incorrect, in this case, the absolute path represented by IMG / photo.jpg for index.htm files is: C: / Website / Web / IMG / Photo .jpg, obviously does not meet the requirements. Correct writing: Use the relative path of ../img/photo.jpg to locate the file: c: /website/web/xz/index.htm c: /website/img/images/photo.jpg In this example INDEX. What should the photo.jpg in HTM? Error Write: ../ img / images / photo.jpg This method is incorrect, in this case for Index.htm files ./Img/Images/photo.jpg The absolute path represented by: C : /website/web/img/images/photo.jpg. Correct writing: You can use ../../img/images/photo.jpg's relative path to position files

Example: C: /Website/web/xz/index.htm c: /website/web/img/photo.jpg How should the photo.jpg in Index.htm in this example? Error Write: ../../ img / photo.jpg This method is incorrect, in this case for index.htm files ../../img/photo.jpg The absolute path represented by : C: /website/img/photo.jpg. Write correctly: You can use ./img/photo.jpg's relative path to locate files

Summary: Through the above example, it can be found that the same portions in the absolute path of the two files can be ignored when the absolute path is converted into a relative path. Just consider that they are different.

How to modify the path to the style sheet: Use the text editor to open the HTM file, view the source code, find ... < tag "Test.css" type = "text / css">. The content behind "HREF =" is the path to the CSS, and we can perform a relative path conversion according to the above knowledge. Example: c: /website/web/xz/index.htm c: /website/css/test.css In this case INDEX. HTM, you can use the Test.css file, you can use ../../css/test. The relative path of CSS is positioned, and the complete code tag is: Error Write method: .. /..../css/test.css This method is incorrect, in this case for Index.htm files ../..../css/test.css represented the absolute path Yes: c: /css/test.css

转载请注明原文地址:https://www.9cbs.com/read-117737.html

New Post(0)