Differences in declaring your root directory in HTML

懵懂的女人 提交于 2019-12-03 16:28:11

To reference files relative to the current page, you can either write the path plainly without a prefix, or you can be explicit about it by prefixing with ./. To reach files with paths relative to the root of your site, you prefix the path with /.

Summary

/ absolute path (full path to resource from root directory)
./ relative path from current directory (equal to not specifying any folder prefix)
../ relative path from parent directory
../../ relative path from parent of parent

Examples

Current URL               Resource path          Resolves to

/pages/home.html          ./picture.jpg          /pages/picture.jpg
/pages/home.html          ../img/picture.jpg     /img/picture.jpg
/pages/about/home.html    /img/picture.jpg       /img/picture.jpg
/pages/about/home.html    img/picture.jpg        /pages/about/img/picture.jpg
/home.html                img/picture.jpg        /img/picture.jpg

This is the same only if the source page (the one containing this HTML) is itself at the root of the domain.

In the general case, ./images is equivalent to images.

This has nothing to do with HTML, apart from the fact that URLs are used in HTML, too. This is defined in URL specifications. Briefly, ./ is relative to current base URL, and / is relative to the server root (technically, it refers to the server part of the base URL).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!