三种格式:(CSS 语法由三部分构成:选择器、属性和值) 例:body {color: blue}
1、派生选择器 例: Css部分: strong { color: red; } h2 {
color: red; } h2 strong { color: blue; } Html文件部分:<p>The strongly emphasized word in this paragraph is<strong>red</strong>.</p> <h2>This subhead is also red.</h2>
<h2>The strongly emphasized word in this subhead is<strong>blue</strong>.</h2>
2、id 选择器(选择器以 "#" 来定义): 例: Css部分: #red {color:red;} #green {color:green;} Html 文本:
<p id="red">这个段落是红色。</p> <p id="green">这个段落是绿色。</p>
3、类选择器 例: Css部分:
.center {text-align: center} Html文本:
如何插入样式表
1、外部样式表
当样式需要应用于很多页面时,外部样式表将是理想的选择。在使用外部样式表的情况下,你可以通过改变一个文件来改变整个站点的外观。每个页面使用 <link> 标签链接到样式表。
<link> 标签在(文档的)头部: <head>
<link rel="stylesheet" type="text/css" href="mystyle.css" /> </head>
浏览器会从文件 mystyle.css 中读到样式声明,并根据它来格式文档。
2、内部样式表
当单个文档需要特殊的样式时,就应该使用内部样式表。你可以使用 <style> 标签在文档头部定义内部样式表,就像这样: <head>
<style type="text/css"> hr {color: sienna;} p {margin-left: 20px;}
body {background-image: url("images/back40.gif");} </style> </head>
3、内联样式
要使用内联样式,你需要在相关的标签内使用样式(style)属性。Style 属性可以包含任何 CSS 属性。本例展示如何改变段落的颜色和左外边距: <p style="color: sienna; margin-left: 20px"> This is a paragraph </p>