<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>绝对定位对象可以层叠,层叠顺序可用z-index控制,但最好不要用负值,因为FF不支持</title>
<style type="text/css">
<!--
body {
margin:0px;
font-size: 9pt;
margin-top: 150px;
margin-left: 150px;
}
.box1 {
background-color: #33CCFF;
height: 200px;
width: 270px;
background-image: url(http://farm1.static.flickr.com/80/251133988_e0b8174060_m.jpg);
background-repeat: no-repeat;
background-position: center center;
position: absolute;
left: 10px;
top: 10px;
z-index:-1;/*这里用了负值,在标准浏览器,如FF中是不能正常显示的*/
}
.box2 {
background-color: #66CC33;
height: 200px;
width: 270px;
background-image: url(http://farm1.static.flickr.com/6/76318014_e50414fe42_m.jpg);
background-repeat: no-repeat;
background-position: center center;
position: absolute;
left: 50px;
top: 50px;
}
.box3 {
background-color: #996699;
height: 200px;
width: 270px;
background-image: url(http://farm1.static.flickr.com/48/172522117_410a1e87c1_m.jpg);
background-repeat: no-repeat;
background-position: center center;
position: absolute;
left: 100px;
top: 100px;
}
-->
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
(4)相对定位对象会占据它原来位置,在下面实例中,没有设定相对定位前,绿色小盒子是在蓝色盒子左上角的,之前也设定了绿色小盒子的浮动方式为左浮动,可以看到文本环绕在它右边,但是后来用相对定位方法把绿色小盒子重定位到外面去了,它还占着自己原来位置,因为你还可以看到文本流没有发生自动填补流动。因此这种直接的相对定位方法较少用,因为重定位对象后原来位置空了一块。相对定位常与绝对定位结合用,一般是给父级设定相对定位方式,子级元素就可以相对它进行方便的绝对定位了(请注意看后面的实例)。
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>相对定位对象会占据原有位置</title>
<style type="text/css">
<!--
body {
margin:0px;
font-size: 9pt;
line-height:12pt;
margin-top: 150px;
margin-left: 150px;
}
.box1 {
background-color: #3CF;
height: 200px;
width: 200px;
}
.box2 {
background-color: #6C6;
height: 100px;
width: 100px;
position: relative;
float: left;
top:-120px;
}
-->
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
[相对定位对象会占据原有位置]现在绿色小盒子是以子盒子形式存在蓝色大盒子中,并设定了浮动方式为左浮动,所以这些文字能环绕在它右边,当绿色小盒子用相对定位方法重定位到外边去了,文字还是不能流入它的区域,即左上角空白区域,那是因为绿色盒子还占用着它原来位置。</div>
</body>
</html>