1.div+CSS(目前流行的開發模式)
a.簡潔,減少大量的<table><tr><td>標簽,減少帶寬;
b.內容和樣工分離,方便維護;
c.符合搜索引擎的喜好;
d.流行大型網站都使用此技術;
e.利于分工合作。
2.HTML設置樣式有三種方法
(1).行內設置樣式(inline)
<bodystyle="background-color:#FF0000">
Pagecontent.
</body>
(2).嵌入式設置樣式(embedded)
<head>
<style>
body{
background-color:#FF0000;
}
</style>
</head>
<body>
Pagecontent.
</body>
<!--設置Firstdivision與Seconddivisioncolor-->
<head>
<style>
.redDiv{
color:#FF0000;
}
</style>
</head>
<body>
<divclass="redDiv">Firstdivision.</div>
<divclass="redDiv">Seconddivision.</div>
<div>Thirddivision.</div>
</body>
(3).外部設置樣式(引用外部CSS文件設置,匹配的element元素將生效)
<head>
<linkrel="stylesheet"type="text/css"href="style.css"/>
</head>
<body>
Pagecontent.
</body>
3.CSS選擇器(8種)
(1).標簽選擇器(元素選擇器)
(2).類(class)選擇器
(3).id選擇器
(4).父子選擇器(關聯選擇器)
(5).組合選擇器
(6).偽標簽、偽類選擇器
(7).屬性選擇器
(8).相鄰兄弟選擇器。
/*1.標簽選擇器(元素選擇器)*/
span{
color:blue;
}
/*2.類(class)選擇器*/
.stwo{
color:#383838;
font-weight:bold;
}
/*3.id選擇器*/
#div11{
color:#600000;
}
#p3{
color:red;
border:solid;
border-color:yellow;
}
/*4.父子選擇器(關聯選擇器)*/
#div33p{
border:solid;
border-color:yellow;
}
/*5.組合選擇器*/
h1,h2{
color:#CCFF66;
}
/*6.偽標簽、偽類選擇器*/
a:link{
color:#CCFF66;
}
a:visited{
color:#CC0033;
}
/*選擇div父標簽下第一個p子標簽*/
div>p:first-child{
border:3pxsolidred;
}
/*先后順序:行內CSS>id>class>標簽(父子先)*/
/*1.标签选择器*/
body{background-color:#F0F0F0;}
p{margin:20px30px30px20px;}
/*2.类选择器(.)
语法:.class名称{属性名:属性值;}
设置多个属性:
.class选择器名{
属性名:属性值;
......
}
*/
.news{
background-color:#D0D0D0;
font-size:20px;
color:#FFFF00;
}
/*3.id选择器(#)
语法:#id{属性名:属性值;}
设置多个属性:
#id{
属性名:属性值;
......
}
*/
#one{
background-color:#FFFF00;
font-size:30px;
color:green;
}
/*4.父子(关联)选择器*/
#region_centerp{
color:orange;
}
/*5.组合选择器*/
#region_north,#region_south{
background-color:#BDE1F7;
}
/*6.伪选择器
css本身已经定义好样式的选择器.
*/
#letter:first-letter{
color:red;
font-size:30px;
font-weight:bold;
}
/*style>id>class>tag*/
/*擴展:屬性選擇器、兄弟選擇器、父子選擇器*/
<!DOCTYPEhtml>
<html>
<head>
<metacharset="UTF-8">
<title>attributevalue</title>
<styletype="text/css">
/*1.屬性選擇器:改變有設置屬性title的color屬性.*/
span[title]{
color:red;
}
span[title=two]{
color:yellow;
}
input[type=reset]{
color:blue;
}
/*屬性值title包含hello的標簽(只要包含hello就OK).*/
p[title*="hello"]{
color:green;
}
/*屬性值title包含word的標簽(word要有空格分開,或等于word,否則選不到).*/
p[title~="word"]{
color:red;
}
/*屬性值title包含的標簽(用-分開,否則選不到).*/
p[title|="word"]{
color:blue;
}
/*屬性值title值以begin開頭.*/
p[title^="begin"]{
color:orange;
}
/*屬性值title值以end結束.*/
p[title$="begin"]{
color:blue;
}
/*2.兄弟選擇器:div標簽緊接在其后的第一個b標簽,中間不能隔其它的標簽*/
div+b{
color:orange;
}
h1+h2{
border:2pxsolidgreen;
color:orange;
}
/*div標簽在其后的所有p標簽*/
div~p{
color:red;
}
html>bodyh1~h3{
color:blue;
}
/*3.父子:div標簽的所有span后裔標簽*/
divspan{
color:red;
}
/*div標簽在其后的所有直系i標簽(兒子)*/
div>i{
color:orange;
}
/*偽選擇器:first-child匹配的是某父元素的第一个子元素,可以说是结构上的第一个子元素。
:first-of-type匹配的是某父元素下相同类型子元素中的第一个,比如p:first-of-type,
就是指所有类型为p的子元素中的第一个。
这里不再限制是第一个子元素了,只要是该类型元素的第一个就行了。
同样类型的选择器:last-child和:last-of-type、:nth-child(n)和:nth-of-type(n)也可以这样去理解。
如#child標簽id下的第一個em標簽。
*/
#child>em:first-child{
font-weight:bold;
color:red;
}
</style>
</head>
<body>
<spantitle="attibute">Thisisred.</span><br/>
<spantitle="two">spanwithtwointitle.</span><br/>
<spantitle="three">spanwith"hello"intitle.</span><br/>
<span>spanfour.</span>
<ptitle="valhello">pwith"wei"intitle"hello".</p>
<ptitle="hello">pwith"hello"intitle.</p>
<div>div+b</div>
<p>divfirstp.</p>
<b>afterb.</b><br/>
<p>divsecondp.</p>
<div>seconddiv+b</div>
<b>secondafterb.</b><br/>
<inputtype="reset"value="Reset"/>
<br/>
<b>third.</b>
<ptitle="word-friend">word</p>
<ptitle="wordname">wordname</p>
<ptitle="beginptitle">pwith"begin"startintitle</p>
<ptitle="end-begin">end-begin.</p>
<ptitle="valhello">pwith"wei"intitle"hello".</p>
<ptitle="hello">pwith"hello"intitle.</p>
<ptitle="beginptitle">pwith"begin"startsecondintitle</p>
<ptitle="pbegin">pwith"begin"endintitle</p>
<div>
<span>divdirectchildspan.</span><br/>
<i>directi兒子</i><br/>
<b>
<i>
<span>bidescendantspan.</span><br/>
</i>
<i>descendanti孫子</i>
</b>
</div>
<h1>Titledescription</h1>
<h2>h2firstdescription</h2>
<h2>h2seconddescription</h2>
<h3>h3threefirstdescription</h3>
<h2>h2seconddescription</h2>
<h3>h3threeseconddescription</h3>
<h3>h3threethirddescription</h3>
<spanid="child">
<em>Iamastrongman.</em><br/>
Iama
<em>strong</em>man.
</span>
</body>
</html>