一下内容摘选于:http://www./tech/web/2007/4601.asp 有时间自己去详细看看,详细你会明白的.
1.定位的专业解释 相对定位(relative):对象不可层叠,依据left,right,top,bottom等属性在正常文档流中偏移自身位置。同样可以用z-index分层设计。
2.定位的形象解释 自私的相对定位(relative)
相对定位一个最大特点是:自己通过定位跑开了还占用着原来的位置,不会让给他周围的诸如文本流之类的对象。相对定位也比较独立,做什么事它自己说了算,要定位的时候,它是以自己本身所在位置偏移的(相对对象本身偏移)。再拿前边作比如来解,那么此时西瓜似乎是有魔法的,如果西瓜通过相对定位在水桶中偏移了你会看到一个现实生活中不存在的现象:水中有一个地方水凹下去了,周围的水不能填补它,西瓜看起来在旁边,如果搅动一下桶中的水,那个凹的位置会发现改变(文本流对相对定位对象还存在影响),但是凹处到西瓜出现的距离始终保持一致。可见文本流与它之间还会互相影响,因为对象并没有真正脱离文本流,就像有两个人在同一层楼水平移动的过程中会有碰头的机会。
实例强化对定位属性的理解 相对定位对象会占据它原来位置,在下面实例中,没有设定相对定位前,绿色小盒子是在蓝色盒子左上角的,之前也设定了绿色小盒子的浮动方式为左浮动,可以看到文本环绕在它右边,但是后来用相对定位方法把绿色小盒子重定位到外面去了,它还占着自己原来位置,因为你还可以看到文本流没有发生自动填补流动。因此这种直接的相对定位方法较少用,因为重定位对象后原来位置空了一块。相对定位常与绝对定位结合用,一般是给父级设定相对定位方式,子级元素就可以相对它进行方便的绝对定位了(请注意看后面的实例)。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./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>
相对定位的对象不可以重叠。这一点也许比较难理解,先来这样理解吧,因为相对定位的对像没有脱离文本流,就像两个还是住在同一层楼的人,任何时候,他们脚下踩的地方不可能被另一个人踩着的,如果可以就意味着两个人共享一块地方了。在下面的实例中,两个小盒子都刚好排在大盒子上方,所以在大盒子中输入的文字被挤到了下面。上面两个盒子我再用相对定位的方法对调了它们的位置,当前它们下方的空间其实不是自己的,也正因为它们没有重叠,所以盒子上方还是有两个盒子占用的空间,下面文字无法向上流动(我已对盒子设定了浮动属性的了,如果不设定,即使有空间文本也不会往上流,上面盒子是块级元素,会独立占据一行)。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./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>
给包含块一个高度,或者说给定位对象的父级设定一个高度。不然也许显示的结果并不是你想要的。在下面的例子中,如果你本意要实现下面效果:把小盒子在父盒子中的左上角垂直往下偏移,上方留出一块区域,如果你删除了box1的高度会怎么样呢?自己测试一下吧。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./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;
- width: 200px;
- height: 200px;/*如果不设定这个高度,也许显示的结果并不是你想要的*/
-
- }
- .box2 {
- background-color: #CC99FF;
- height: 100px;
- width: 100px;
- position:relative;
- bottom:-100px;
- }
- -->
- </style>
- </head>
-
- <body>
- <div class="box1">
- <div class="box2"></div>
- </div>
- </body>
- </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./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;
width: 200px;
height: 200px;/*如果不设定这个高度,也许显示的结果并不是你想要的*/
}
.box2 {
background-color: #CC99FF;
height: 100px;
width: 100px;
position:relative;
bottom:-100px;
}
-->
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
放置相对定位对像到可视区外不会出现滚动条。此现象很好理解,因为相对定位对象原来位置没有变,原来位置没有出去滚动条就不会出现。在下面实例中,如果你缩小浏览器窗口,相对定位的对象还会出现完全消失的情况呢。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./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:13pt;
- }
- .box{
- background-color: #996699;
- height: 200px;
- width: 270px;
- background-image: url(http://farm1.static./48/172522117_410a1e87c1_m.jpg);
- background-repeat: no-repeat;
- background-position: center center;
- position: relative;
- left: 900px;
- }
- .box2{
- height: 200px;
- width: 270px;
- position:absolute;
- border: 1px dashed #996699;
- }
- -->
- </style>
- </head>
-
- <body>
- <div class="box2">虚线框是右边盒子原来的位置,即用了相对定位后,把对象重新定位到了右边,但对象的位置其实还在这里,或者说对象实际上并没有越出浏览器可视区域,所以不会出现滚动条。<br />
- 我说过相对定位的的盒子原有位置是不会被占用的,但我为什么可以在这里写说明呢,这是因为我用了绝对定位,我只是为了方便说明才这样做的,请不要误解了。</div>
- <div class="box"></div>
- </body>
- </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./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:13pt;
}
.box{
background-color: #996699;
height: 200px;
width: 270px;
background-image: url(http://farm1.static./48/172522117_410a1e87c1_m.jpg);
background-repeat: no-repeat;
background-position: center center;
position: relative;
left: 900px;
}
.box2{
height: 200px;
width: 270px;
position:absolute;
border: 1px dashed #996699;
}
-->
</style>
</head>
<body>
<div class="box2">虚线框是右边盒子原来的位置,即用了相对定位后,把对象重新定位到了右边,但对象的位置其实还在这里,或者说对象实际上并没有越出浏览器可视区域,所以不会出现滚动条。<br />
我说过相对定位的的盒子原有位置是不会被占用的,但我为什么可以在这里写说明呢,这是因为我用了绝对定位,我只是为了方便说明才这样做的,请不要误解了。</div>
<div class="box"></div>
</body>
</html>
定位的特殊值情况。如果定位设置是这样的:position:relative ,right:200px, left:10px会出现什么情况呢?一边叫对象向右偏移10px,另一边又叫对象向左偏移200px,到底是听那边的呢,还是那个先,那个后呢。针对这种矛盾情况,CSS规定只听左边的left,而右边怎么设置都被重定为-left,即-left =right。上下值top与bottom矛盾的话,就以top为准,所以在下面的实例中,你如何改变right的值,对象位置不会发生改变的。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www./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;
- width: 200px;
- height: 200px;
- }
- .box2 {
- background-color: #CC99FF;
- height: 100px;
- width: 100px;
- position:relative;
- right:200px;/*此值总被认为是-left=-10px,所以你怎么改它都不会影响到布局*/
- left:10px;/*这里设定了两个定位值,但是它们是矛盾的,所以此时将以左边为准,如果是上下类型错误,则以上边定位数值为准*/
-
- }
- -->
- </style>
- </head>
-
- <body>
- <div class="box1">
- <div class="box2"></div>
- </div>
- </body>
- </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./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;
width: 200px;
height: 200px;
}
.box2 {
background-color: #CC99FF;
height: 100px;
width: 100px;
position:relative;
right:200px;/*此值总被认为是-left=-10px,所以你怎么改它都不会影响到布局*/
left:10px;/*这里设定了两个定位值,但是它们是矛盾的,所以此时将以左边为准,如果是上下类型错误,则以上边定位数值为准*/
}
-->
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
综合实例见证定位的魅力
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>相对定位和绝对定位实例--作者:唐国辉</title>
<style type="text/css">
<!--
*{
margin:0px;
padding:0px;
}
body {
margin:10px;
font-size: 13px;
}
a:link {
color: #666;
text-decoration: none;/*去除链接下划线*/
}
a:visited {
color: #666;
text-decoration: none;
}
a:hover {
color: #F90;
}
h3 {
color: #FFF;
background-color: #F90;
width: 100px;
padding-top:3px;
text-align:center;
}
ul {
width: 300px;
border-top: 1px solid #F60;/*使其上边有一线条,与标题h3吻合*/
}
ul li {
padding:5px;
border-bottom: 1px solid #CCC;
list-style:none;/*去除列表样式,这对于标准浏览很重要*/
}
a {
position: relative;/*设置其定位方法为相对定位,等一下内部信息面板就可以相对它定位*/
display:block;/*让链接以块状呈现,这样不用点中链接文字就可以响应链接*/
}
a div {
display: none;/*初始化信息面板不显示*/
}
a:hover {background:#fff;}/*IE7以下版本A状态伪类bug*/
a:hover div {
position: absolute;
padding:5px;
display:block;
width: 245px;/*只给出宽度,高让它随内容多少自动调整*/
left:150px;/*这是相对父级A的定位*/
top: 20px;
border: 1px solid rgb(91,185,233);
background-color: rgb(228,246,255);
z-index:999;/*把信息面板提到一个较高的位置,使链接文字过长时不会与面板重叠,但这只对FF有效*/
}
a img {
width:80px;
height:80px;
border:none;/*去除图片边框,默认情况下,链接内的图片在标准浏览器会出现边框*/
display:block;
position: absolute;/*用绝对定位抽离正常文本流,不然在设计的时候考虑到不同浏览器正常显示会更麻烦*/
top:5px;/*这里的5px是与信息面板大盒子的填充一样的*/
left:5px;
}
dl {
width: 160px;
float:right;
color: #999;
line-height:20px;
}
dl dd span {
font-weight: bold;
color: #639;
}
-->
</style>
</head>
<body>
<h3>最新单曲</h3>
<ul>
<li><a href="#">01 爱的文身 黄圣依<div><img src="http://imgcache.qq.com/music/photo/singer/54/singerpic_5554_0.jpg" alt="" />
<dl>
<dd><span>歌名:</span>爱的文身</dd>
<dd><span>歌手:</span>黄圣依</dd>
<dd><span>介绍:</span>黄圣依唱片主打歌的确是她个人的内心写照,《爱的文身》由香港音乐大师金培达作曲,制作人陈少琪亲自填词。</dd>
</dl></div></a></li>
<li><a href="#">02 累了 阿信<div><img src="http://imgcache.qq.com/music/photo/singer/47/singerpic_6547_0.jpg" alt="" />
<dl>
<dd><span>歌名:</span>累了</dd>
<dd><span>歌手:</span>阿信</dd>
<dd><span>介绍:</span>青春校园偶像剧----【熱情仲夏】片尾曲</dd>
</dl></div></a></li>
<li><a href="#">03 漫漫 慢慢 阿朵<div><img src="http://imgcache.qq.com/music/photo/singer/61/singerpic_6361_0.jpg" alt="" />
<dl>
<dd><span>歌名:</span>漫漫 慢慢</dd>
<dd><span>歌手:</span>阿朵</dd>
<dd><span>介绍:</span>阿朵抢听版最新单曲《漫漫 慢慢》让你认识阿朵柔情的一面,展现阿朵百变的风格。</dd>
</dl></div></a></li>
<li><a href="#">04 我怀念的 孙燕姿<div><img src="http://imgcache.qq.com/music/photo/singer/9/singerpic_109_0.jpg" alt="" />
<dl>
<dd><span>歌名:</span>我怀念的</dd>
<dd><span>歌手:</span>孙燕姿</dd>
<dd><span>介绍:</span>令人感同身受的抒情歌,在故事性的架构中,有着平凡但又能扣人心弦的情感,是一首高度共鸣的抒情歌。</dd>
</dl></div></a></li>
<li><a href="#">05 听,花期越来越近 后弦<div><img src="http://imgcache.qq.com/music/photo/singer/33/singerpic_4733_0.jpg" alt="" />
<dl>
<dd><span>歌名:</span>花期越来越近</dd>
<dd><span>歌手:</span>后弦</dd>
<dd><span>介绍:</span>后弦参与《花开的声音》这个舞台剧里的一部分,邀请了后弦去演唱这首歌,此歌就是为舞台剧《花开的声音》而创作。</dd>
</dl></div></a></li>
</ul>
</body>
</html>