日期:2014-05-17 浏览次数:20797 次
(1)静态定位不讲了,前面讲的都是静态定位。



这里我们可以看出,所谓相对定位是指,相对该元素应当显示的左上角重新定位,虽然它脱离了标准流,但是它的空间,不能被占用。
relative.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <link rel="stylesheet" type="text/css" href="relative.css" /> <title>relative 相对定位</title> </head> <body> <div class="div1">内容1</div> <div class="div1" id="spe">内容2</div> <div class="div1">内容3</div> <div class="div1">内容4</div> </body> </html>
.div1{
width: 70px;
height: 30px;
background: silver;
float: left;
margin-left: 5px;
}
#spe{
position: relative;
left: 40px;
top: 100px;
}


绝对定位,究竟是对哪个点定位。
absolute.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <link rel="stylesheet" type="text/css" href="absolute.css" /> <title>绝对定位</title> </head> <body> <div class="div1">内容1</div> <div class="div1" id="spe">内容2</div> <div class="div1">内容3</div> <div class="div1">内容4</div> </body> </html>
*{
margin: 0px;
padding: 0px;
}
.div1{
width: 70px;
height: 30px;
background: silver;
float: left;
margin-left: 5px;
}
#spe{
position: absolute; /*绝对定位(究竟对谁)*/
left: 40px; /*left为正,则向右移动*/
top: 100px; /*top为正,则向下移动*/
}用一个div把内容2包起来,重点讲解“对该元素最近的那个脱离了标准流的元素定位”
absolute2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="absolute2.css" />
<title>绝对定位2</title>
</head>
<body>
<