Linking to External Stylesheets with HTTP:// or local path

99封情书 提交于 2019-12-11 09:27:52

问题


What are the pros and cons of each? Is there a difference?

CSS Stylesheets in HTML.


回答1:


I assume you're asking which of these you should use:

<link rel="stylesheet" type="text/css" href="/file.css" />
<link rel="stylesheet" type="text/css" href="http://example.com/file.css" />

The difference between the two is that the former is called a relative path and the latter is an absolute path.

If the HTML page in question is http://example.com/page.html, then there effectively is no difference. However, if the page is https://example.com/page.html (SSL secured), you'll find that there is now a very important difference. On a secured page, many browsers will not load content which is not also served securely. If you've linked your CSS file with an absolute path, it would not be served securely and therefore your stylesheet might not get loaded.

Unless you have a very specific reason to link your CSS absolutely, you want to use a relative path.




回答2:


when use http://... (absolute path) you'll need to change it when you move your page to new domain (if you dont generate it dynamically with PHP )

using relative path is the best way imho

using local path is not possible as CSS files are requested by hosts/uests browser, not on server




回答3:


It's nice to get started with inline and embedded style="" or putting some style defs in the header, but once a project is more than two pages this just because more work in the end.



来源:https://stackoverflow.com/questions/4011411/linking-to-external-stylesheets-with-http-or-local-path

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