(4)相对定位对象会占据它原来位置,在下面实例中,没有设定相对定位前,绿色小盒子是在蓝色盒子左上角的,之前也设定了绿色小盒子的浮动方式为左浮动,可以看到文本环绕在它右边,但是后来用相对定位方法把绿色小盒子重定位到外面去了,它还占着自己原来位置,因为你还可以看到文本流没有发生自动填补流动。因此这种直接的相对定位方法较少用,因为重定位对象后原来位置空了一块。相对定位常与绝对定位结合用,一般是给父级设定相对定位方式,子级元素就可以相对它进行方便的绝对定位了(请注意看后面的实例)。
< 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 >
本文转中国秀未来设计 http://www.showweb.cn ,转载请注明出处。
(5)相对定位的对象不可以重叠。这一点也许比较难理解,先来这样理解吧,因为相对定位的对像没有脱离文本流,就像两个还是住在同一层楼的人,任何时候,他们脚下踩的地方不可能被另一个人踩着的,如果可以就意味着两个人共享一块地方了。在下面的实例中,两个小盒子都刚好排在大盒子上方,所以在大盒子中输入的文字被挤到了下面。上面两个盒子我再用相对定位的方法对调了它们的位置,当前它们下方的空间其实不是自己的,也正因为它们没有重叠,所以盒子上方还是有两个盒子占用的空间,下面文字无法向上流动(我已对盒子设定了浮动属性的了,如果不设定,即使有空间文本也不会往上流,上面盒子是块级元素,会独立占据一行)。
< 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: #33CCFF;
height: 200px;
width: 210px;
padding:5px;
}
.box2 {...}{
background-color: #66CC66;
height: 100px;
width: 100px;
position: relative;
float: left;
left:100px;
}
.box3 {...}{
background-color: #CC99FF;
height: 100px;
width: 100px;
position: relative;
float: left;
right:100px;
}
-->
</ style >
</ head >
< body >
< div class ="box1" >
< div class ="box2" > 我家在左边 </ div >
< div class ="box3" > 我家在右边 </ div >
相对定位对象是在正常文本流中移动的,所以它的存在还是会影响文本流的布局, 如果是绝对定位,这些文本应向上流入上方两个盒子的底部了。
</ div >
</ body >
</ html >
本文转中国秀未来设计 http://www.showweb.cn ,转载请注明出处。
(6)高度自适应的妙用。在下面实例中,右边的紫色小盒子高度是没有设定的,它的高度是随着里面内容的增加而增高的,但我们又可以通过绝对定位方法把它始终定位在父盒子的右上角。同理,我们也可以只设定高度而让宽度自动随内容增加而变宽。
< 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:5px;
font-size: 9pt;
}
.box1 {...}{
background-color: #33CCFF;
height: 500px;
width: 500px;
position: relative;
}
.box2 {...}{
background-color: #CC99FF;
width: 100px;
position: absolute;
right:3px;
top:3px;
text-align: center;
line-height:15pt;
}
.box3 {...}{
width: 390px;
height:500px;
position: absolute;
left:0px;
top:0px;
line-height:15pt;
border-right: thin dashed #999;
}
-->
</ style >
</ head >
< body >
< div class ="box1" >
< div class ="box3" > 右边的小盒子高度是没有设定的,它的高度是随着里面内容的增加而增高的,但我们又可以通过绝对定位把它始终定在父盒子的右上角。 </ div >
< div class ="box2" >
< p > 蓝色理想 < br />
经典论坛 < br />
业界动态 < br />
技术文档
</ p >
</ div >
</ div >
</ body >
</ html >
本文转中国秀未来设计 http://www.showweb.cn ,转载请注明出处。