使用 HTTP:// 或本地路径链接到外部样式表
各自的优点和缺点是什么?有区别吗?
HTML 中的 CSS 样式表。
What are the pros and cons of each? Is there a difference?
CSS Stylesheets in HTML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您问应该使用其中哪一个:
两者之间的区别在于前者称为相对路径,后者称为绝对路径。
如果相关 HTML 页面是
http://example.com/page.html
,那么实际上没有区别。但是,如果页面是https://example.com/page.html
(受 SSL 保护),您会发现现在是一个非常重要的区别。在安全页面上,许多浏览器不会加载未安全提供的内容。如果您使用绝对路径链接 CSS 文件,则无法安全地提供该文件,因此您的样式表可能无法加载。除非您有非常具体的原因要绝对链接 CSS,否则您需要使用相对路径。
I assume you're asking which of these you should use:
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 ishttps://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.
当使用 http://... (绝对路径)时,当您将页面移动到新域(如果您不使用 PHP 动态生成它)时,您需要更改它,
最佳方式
使用相对路径是使用本地路径的 不可能,因为 CSS 文件是由主机/用户浏览器请求的,而不是在服务器上请求的
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
开始使用内联和嵌入式
style=""
或在标题中放置一些样式定义是很好的,但是一旦项目超过两页,这只是因为最终需要做更多的工作。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.