Using CSS in HTML
2015-06-09 19:58
537 查看
Table of Contents
CSS in style AttributesCSS in style Elements
CSS in External Files
The link Element
The @import Instruction
CSS is intended to be used with HTML (or SVG). There are three ways you can include the CSS in your HTML file:
Embed CSS inside the
styleattribute of HTML elements.
Embed CSS inside
styleHTML elements
Link to external CSS files.
Each of these options will be described below.
CSS in style Attributes
The simplest way to include CSS in an HTML page is to embed the CSS inside thestyleattribute of an HTML element. CSS properties embedded inside a
styleattribute are only applied to the HTML element into which they are embedded. Here is how that looks:
<div style="border: 1px solid black;"> Style This! </div>
This example inserts the CSS property
borderinto the
styleattribute of the
divelement. The value of the
borderproperty is
1px solid blackwhich sets the border of the
divelement to a one pixel wide, black border.
If you need to set more than one CSS property inside the
styleelement, separate the CSS properties with a semicolon, like this:
<div style="border: 1px solid black; font-size: 18px;"> Style This! </div>
This example sets both the
borderCSS property and the
font-sizeCSS property.
CSS in style Elements
Another option of using CSS in HTML is to embed your CSS insidestyleHTML elements inside your HTML page. If you need to apply the same styles to multiple HTML elements, this is much easier than setting the styles inside each HTML element in their
styleattribute. Here is a CSS example using the
styleelement:
<style> div { border: 1px solid black; } </style> <div> Style This! </div> <div> Style This Too! </div>
This example shows a single
styleelement which defines a CSS rule which is applied to all
divelements. Thus, the CSS property inside the
styleelement (the
borderproperty) is applied to both of the
divelements in the example.
You can define more than one CSS rule inside the same
styleelement. Here is an example:
<style> div { border: 1px solid black; } span { border: 1px solid blue; }
</style>
This example defines two CSS rules. The first CSS rule applied to all
divelements, and the second CSS rule is applied to all
spanelements.
You can also embed more than one
styleelement within the same HTML page. Here is an example:
<style> div { border: 1px solid black; } </style> <style> span { border: 1px solid blue; } </style>
This example shows the CSS rules from the previous example embedded in their own
styleelement.
The
styleelements can be embedded either inside the HTML
headelement or the
bodyelement. Here is an example:
<!DOCTYPE html> <html> <head> <style> div { border: 1px solid black; } </style> </head> <body> <style> span { border: 1px solid blue; } </style> <div> Style This! </div> <span> Style This Too! </span> </body> </html>
In this example the CSS rule for
divelements is embedded inside its own
styleelement inside the
headHTML element, and the CSS rule for
spanelements is embdded inside its own
styleelement inside the
bodyHTML element.
CSS in External Files
If you need to apply the same CSS styling to multiple HTML pages, it is easier to move your CSS rules to a CSS file, and then link to that file from your HTML pages. You can reference an external style sheet in two ways:Via a
linkelement inside the
headelement.
Via an
@importinstruction inside a
styleelement.
Both of these mechanisms are explained in the following sections.
The link Element
You reference an external CSS file using thelinkelement (inside the
headelement). Here is an example:
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="my-css-file.css"> </head> <body> <div> Style This! </div> <span> Style This Too! </span> </body> </html>
This example links to the external CSS file named
my-css-file.css. This file has to be accessible online, and in this case it has to be located in the same directory as the HTML file. Thus, if the HTML file is located at
http://jenkov.com/my-website/my-html-file.html, then the CSS file should be available at
http://jenkov.com/my-website/my-css-file.css.
Actually, the
hrefattribute of the
linkelement can contain an absolute or relative URL. For more information about absolute and relative URLs, see HTML and URLs in my HTML4 tutorial.
If we move the CSS rules from the earlier examples to the
my-cess-file.cssfile, then the contents would look like this:
div { border: 1px solid black; } span { border: 1px solid blue; }
Notice that the CSS file has no
styleelement. It only has the CSS rules themselves.
The @import Instruction
You can also import an external CSS file from inside astyleelement using the
@importinstruction. Here is a CSS
@importexample:
<style> @import url('http://jenkov.com/my-website/my-css-file.css'); </style>
This example will include the CSS file
http://jenkov.com/my-website/my-css-file.cssinto the HTML page which contains that
styleelement.
相关文章推荐
- css3处理sprite背景图压缩来解决H5网页在手机浏览器下图标模糊的问题
- Qt 自定义 滚动条 样式
- 关于Css选择器优先级
- CSS3选择器
- iOS之设置导航控制器样式以及AttributeText, NSMutableAttributedString
- AUTOCAD ActiveX 设置默认点样式
- label、span标签小解
- CSS 清除浮动的几种方式
- CSS里的单位
- CSS定位阴影
- CSS多种方法实现分隔线
- pre 随变化的样式
- CSS3 选择器——伪类选择器
- 16款纯CSS3实现的loading加载动画
- table中tr间距的设定
- CSS样式表层叠(cascade)处理冲突
- css font-weight原理
- css @media认识
- Notice: Constant CSS_URL already defined in C:\wamp\www\PHP\shop\index.php
- layer 1.8.5 引用样式失效