在 HTML 文件中添加外部 CSS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1947878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding external CSS in an HTML file
提问by Programer
I know I can include CSS on my page like this:
我知道我可以像这样在我的页面上包含 CSS:
<style>
.style{
..
}
</style>
How do I add an external stylesheet file in an HTML page?
如何在 HTML 页面中添加外部样式表文件?
回答by Pekka
In your HEAD, add:
在您的 HEAD 中,添加:
<link rel="stylesheet" type="text/css" href="your_css_file.css">
the css file itself does not contain <style>
tags.
css 文件本身不包含<style>
标签。
回答by Victor Nicollet
The simplest way to do so is to add a stylesheet link to your document HEAD section:
最简单的方法是将样式表链接添加到文档 HEAD 部分:
<link rel="stylesheet" href="style.css" type="text/css">
Doing so will reduce the performance on the first load (since the system must request two files instead of one) but improve subsequent performance because the style sheet remains in cache for a while.
这样做会降低第一次加载时的性能(因为系统必须请求两个文件而不是一个)但会提高后续性能,因为样式表会在缓存中保留一段时间。
回答by rui
From StackOverflow's page:
从 StackOverflow 的页面:
<link rel="stylesheet" href="http://sstatic.net/so/all.css?v=5885" type="text/css">
I don't think it will improve speed much unless the same CSS file is shared across multiple webpages in your website (so the browser can cache it). Otherwise there's the penalty of creating an extra HTTP connection to retrieve the CSS.
我认为除非在您网站的多个网页之间共享相同的 CSS 文件(以便浏览器可以缓存它),否则它不会提高速度。否则,创建一个额外的 HTTP 连接来检索 CSS 会受到惩罚。
回答by G.Ashok Kumar
Add the following line in the head of your html file
在 html 文件的头部添加以下行
<link rel="stylesheet" type="text/css" href="path/to/your/style.css">
Then you can add styles in style.css like,
然后你可以在 style.css 中添加样式,例如,
.classname{
...
}
And there is also inline style sheet, you can add it in the html line itself, for example
并且还有内联样式表,你可以将它添加到 html 行本身,例如
<a style="color:red;" href="#"></a>
回答by Ashok Chhetri
You need to add this tag in head section of your html file
您需要在 html 文件的 head 部分添加此标签
<link rel="stylesheet" type="text/css" href="css_file.css">
<link rel="stylesheet" type="text/css" href="css_file.css">