css:cascading style sheets层叠样式表
css版本号:2.0
2.css的作用:
快速维护页面元素(如文本、图像、链接等)的外观
(如字体、字号、颜色等)
3.html属性与css样式的使用原则(p59)
W3C建议尽量使用css样式取代HTML标记属性
例如:src属性只能用html实现,
但是宽高属性尽量使用css样式实现
4.css应用方式
4.1链接到外部的css文件
扩展名: .css
<link type="text/css" rel="stylesheet"
media="all|screen|print" href="CSS文件地址">
media:
all:浏览器支持
打印:支持 针对所有
screen:浏览器支持,打印不支持,针对屏幕
print:浏览器不支持,打印支持,针对打印机
4.2书写在文档的头部(<head>标签里)
<style type="text/css">
...
....
</style>
css样式同时使用外部和内部样式的时候,
采用书写顺序放在下面的
4.3书写在标记内部(优先级最高,重复定义以最后一次为准)
<标记名 style="css样式"/>
<标记名 style="css样式">...</标记名称>
5、css语法结构
选择器{
属性:值;
属性:值;
。。。
}
二、选择器(哪些对象使用css样式)
1.通配选择器--*(能够自动应用于所有元素)
2.元素选择器--标记名称(自动应用于指定的元素)
也叫类型选择器
例如(标题为h2的):
h2{
color:red;
}
3.类选择器 -- .类名称
1)在使用css类时需要添加class="类名"属性
2)css类在声明时需要加点".",但是在使用时无需“点”
例如:
<style type="text/css">
.a1{
color:red;
}
</style>
<h1 class="a1">标题1(A)</h1>
3)多个css类之间以空格分隔
<h1 class="a1 a2">标题1(A)</h1>
4)变形的用法
元素名.类名称(只能用于指定元素)
例如:
td.a3{
color:blue;
}代表的意思是a3只能用于单元格对象。
.类名称.类名称:指只能同时应用这2个类
例如:.a4.a5{
font-size:80px;
}表示的意思是只能同时使用a4和a5这两个类
p.a1.a2:只用于段落,同时用于a1和a2
4.ID选择器--#id(主要是为以后的java脚本用)
1)只能手动的应用给文档的唯一对象。
2)使用时添加id="ID"属性
3)对象的ID除了css可以使用外,还可以被当前
JavaScript或jQuery使用到。
JavaScript:
document.getElementById(String id)
jQuery:
$('#id')
<style type="text/css">
#a1{
color:yellow;
font-size:80px
}
</style>
...
<h2 id="a1">我是标题2</h2>
5.群组选择器(实际上是具有相同属性的选择器的简捷方式)
选择器,选择器,...
h1,.red{
color:red;
}表示元素选择器跟类选择器具有相同的属性
6.派生选择器(后代选择器)
选择器 选择器 ...
说明:前面的选择器至少是后面选择器的父节点
7.伪类选择器(主要是针对链接)
:link,正常显示的状态
:hover,鼠标放上的状态
:active,鼠标按下的状态
:visited,访问过的状态
三、css单位
1.长度
px:像素
%:百分比
em:字号的倍数
2.颜色
1)英文名称,如red,green,blue等
2)完成的16位进制数字,如#ff0000(2位为单位,分别表示红绿蓝)
#ff0000红,#00ff00l绿,#0000ff蓝
3)简写的十六位进制,(#aabbcc的形态可以简写为#abc)如#f00
4)RGB模式,如rgb(0,0,255)
5)百分比的RGB模式,如rgb(0%,100%,0%)
3.css选择器优先级
例1:
inline,IDs,classes,tags
内联样式其优先级代码是1,0,0,0
例2:
#a2{
}
id选择器的优先级是0,1,0,0
类选择器的优先级是0,0,1,0
例4
p{
}
元素选择器的优先级是:0,0,0,1
例5
#a1 a2 .a3 .a4 p{
}
0,2,2,1
-----------------
一、布局属性:
width:value;
height:value;
二、边框属性
上边框的宽度:
border-top-width:value;
上边框的线形:
border-top-style:
none|solid(实线)|dotted(简化线);
上边框的颜色:
border-top-color
border-top(补p68[2])
设置上边框的属性
语法:border-top:width style color;
四条边框线都一样:
border:width style color;
overflow
溢出处理
语法:
overflow:visible(可见)|hidden(隐藏)|scroll(滚动)
hidden用的最多
三、内边距与外边距
3.1 外边距(框线与外部的边距)
顶部的外边距:
margin-top:value;
margin-right:value;
margin-bottom:value;
margin-left:value;
设置外边距:
margin:value;(是个方向相同)
margin:value value (上下,左右)
margin:value value value value(上,左右 ,下)
margin:value value value value value(上,右,下,左)
内边距
padding-top
padding-right
padding-left
padding-bottom
padding
padding
四、盒子模型(BoxModel)
对象的宽度=左侧外边距+左侧边框+左侧内边距+宽度
+右侧内边距+右侧边框+右侧外边距
#container
#logo
#navi导航
#main 主要内容
#copy 版权
Dreamweaver(html编辑工具)
common.css
p{
color:green;
font-size:20px;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>css作用</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet"
media="all" href="common.css">
<style type="text/css">
p{
color:red;
font-size:20px;
}
</style>
</head>
<body>
<p>文本样式</p>
<p style="color:blue">文本样式</p>
<p>文本样式</p>
<p>文本样式</p>
</body>
</html>
颜色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>color</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>文本样式</p>
<p style="color:red">文本样式</p>
<p style="color:#ff0000">文本样式</p>
<p style="color:#f00">文本样式</p>
<p style="color:rgb(255,0,0)">文本样式</p>
<p style="color:rgb(100%,0,0)">文本样式</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>css选择器优先级</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style type="text/css">
*{
color:red;
}
p{
color:green;
}
.p1{
color:blue;
}
#p2{
color:yellow;
}
.a4{/*0,0,1,0*/
color:green;
}
body a5{/*0,0,1,1*/
color:red;
}
</style>
</head>
<body>
<p style="color:black" id="p2" class="p1">文本样式</p>
<p>文本样式2</p>
<h1 class="a4 a5">标题1</h1>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>01通配选择器.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
*{
color:red;
}
</style>
</head>
<body>
<h1>我是标题1</h1>
<h2>我是标题2</h2>
<p>我是段落</p>
<table width="300" cellpadding="10" cellspacing="0" border="1">
<tr>
<td>我是单元格1</td>
<td>我是单元格2</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>02元素选择器.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
h2{
color:red;
}
</style>
</head>
<body>
<h1>我是标题1</h1>
<h1>我是标题1</h1>
<h1>我是标题1</h1>
<h2>我是标题2</h2>
<h2>我是标题2</h2>
<h2>我是标题2</h2>
<p>我是段落</p>
<table width="300" cellpadding="10" cellspacing="0" border="1">
<tr>
<td>我是单元格1</td>
<td>我是单元格2</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>03类选择器.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
.a1{
color:red;
}
.a2{
font-size:40px;
}
td.a3{
color:blue;
}
.a4.a5{
font-size:80px;
}
</style>
</head>
<body>
<h1 class="a1">标题1(A)</h1>
<h1 class="a3">我是标题1</h1>
<h1>我是标题1</h1>
<h2>我是标题2</h2>
<h2 class="a1 a2">我是标题2(AB)</h2>
<h2 class="a1">我是标题2(A)</h2>
<p class="a4 a5">我是段落(A)</p>
<table width="300" cellpadding="10" cellspacing="0" border="1">
<tr>
<td class="a3">我是单元格1</td>
<td>我是单元格2</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>04ID选择器.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
#a1{
color:yellow;
font-size:80px
}
</style>
</head>
<body>
<h1>标题1</h1>
<h1>我是标题1</h1>
<h1>我是标题1</h1>
<h2>我是标题2</h2>
<h2>我是标题2</h2>
<h2>我是标题2</h2>
<p id="a1">我是段落</p>
<table width="300" cellpadding="10" cellspacing="0" border="1">
<tr>
<td>我是单元格1</td>
<td>我是单元格2</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>05群组选择器.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
h1,.red{
color:red;
}
</style>
</head>
<body>
<h1>我是标题1</h1>
<h1>我是标题1</h1>
<h1>我是标题1</h1>
<h2>我是标题2</h2>
<h2>我是标题2</h2>
<h2 class="red">我是标题2</h2>
<p>我是段落</p>
<p class="red">我是段落</p>
<p>我是段落</p>
<table width="300" cellpadding="10" cellspacing="0" border="1">
<tr>
<td>我是单元格1</td>
<td>我是单元格2</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>06后代选择器(派生选择器).html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
strong em{
color:red;
}
</style>
</head>
<body>
<p>A1<strong><em>加粗倾斜</em></strong></p>
<p>A2<strong><u><em>加粗倾斜</em></u></strong></p>
<p>B<em><strong>加粗倾斜</strong></em></p>
<p>C<strong>加粗</strong></p>
<p>D<em>倾斜</strong></p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>07伪类选择器.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--text-decoration:划线-->
<style type="text/css">
a{
color:red;
text-decoration:none;
}
a:hover{
color:black;
text-decoration:underline;
}
</style>
</head>
<body>
<p><a href="#">链接1</a></p>
<p><a href="#">链接2</a></p>
<p><a href="#">链接3</a></p>
<p><a href="#">链接4</a></p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>宽度高度.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
#wrapper{
width:500px;
height:300px;
border-top-width:2px;
border-top-style:dotted;
border-top-color:red;
border-right-width:2px;
border-right-style:solid;
border-right-color:red;
border-left-width:2px;
border-left-style:solid;
border-left-color:red;
border-bottom-width:2px;
border-bottom-style:none;
border-bottom-color:red;
}
</style>
</head>
<body>
<!--div是个容器-->
<div id="wrapper">12345</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>宽度高度.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
body{
/*
margin-top:0;
margin-left:0;
margin-bottom:0;
*/
margin:0;
}
#wrapper{
width:500px;
height:100px;
/*
二、边框属性
上边框的宽度:
border-top-width:value;
上边框的线形:
border-top-style:
none|solid(实线)|dotted(简化线);
上边框的颜色:
border-top-color
border-top(补p68[2])
设置上边框的属性
语法:border-top:width style color;
四条边框线都一样:
border:width style color;
*/
/* css注释:
border-top-width:2px;
border-top-style:solid;
border-top-color:red;
*/
/*
border-top:2px solid red;本行等价于上面3行
border-left:2px solid red;
border-right:2px solid red;
border-bottom:2px solid red;
*/
border:2px solid red;/*本行等价于上面4行*/
overflow:visible;
/*
overflow
溢出处理
语法:
overflow:visible(可见)|hidden(隐藏)|scroll (滚动)
hidden用的最多
*/
}
/*
三、内边距与外边距
3.1 外边距(框线与外部的边距)
顶部的外边距:
margin-top:value;
margin-right:value;
margin-bottom:value;
margin-left:value;
设置外边距:
margin:value;(是个方向相同)
margin:value value (上下,左右)
margin:value value value value(上,左右 ,下)
margin:value value value value value(上,右,下,左)
*/
#container{
width:500px;
height:1000px;
border:2px solid blue;
margin-top:20px;
padding:20px;/*内边距*/
}
</style>
</head>
<body>
<!--div是个容器-->
<div id="wrapper">
<p>12345</p>
<p>12345</p>
<!--
<p>12345</p>
<p>12345</p>
<p>12345</p>
<p>12345</p>
<p>12345</p>
<p>12345</p>
<p>12345</p>
<p>12345</p>
-->
</div>
<div id="container">
<p>ABCDE</p>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>盒子模型(BoxModel)</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
body{
margin:0;
}
#wrapper{
width:500px;
height:100px;
border:2px solid red;
overflow:visible;
margin:0 auto;/*上下外边距0,左右居中*/
}
#container{
width:500px;
height:1000px;
border:2px solid blue;
/*margin-top:20px;*/
margin:0 auto;
padding:20px;/*内边距*/
}
</style>
</head>
<body>
<!--div是个容器-->
<div id="wrapper">
<p>12345</p>
<p>12345</p>
</div>
<div id="container">
<p>ABCDE</p>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>盒子模型(BoxModel)</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
body{
margin:0;
}
img{
border:none;
}
#container{
width:960px;
margin:0 auto;
border:1px solid #f00;
}
#logo{
height:62px;
}
</style>
</head>
<body>
<div id="container">
<!-- Logo start -->
<div id="logo"><a href="#"><img src="images/pic.bmp" alt=""></img></a></div>
<!-- Logo End -->
<!-- Navi start -->
<div id="navi"></div>
<!-- Navi End -->
<!-- Main start -->
<div id="main"></div>
<!-- Main End -->
<!-- copy start -->
<div id="copy"></div>
<!-- copy End -->
</div>
</body>
</html>