您的位置:首页 > Web前端 > CSS

position:relative 的深入理解,欢迎探讨

2012-05-25 20:39 435 查看
 

对于css中的position:relative;我始终搞不明白它是什么意思,有什么作用?

对于position的其它几个属性,我都搞懂了

引用

static :  无特殊定位,对象遵循HTML定位规则

absolute :  将对象从文档流中拖出,使用left,right,top,bottom等属性进行绝对定位。而其层叠通过z-index属性定义。此时对象不具有边距,但仍有补白和边框

relative :  对象不可层叠,但将依据left,right,top,bottom等属性在正常文档流中偏移位置

fixed :  IE5.5及NS6尚不支持此属性

对于文档中所说的几个属性,除了relative,其它的一试,就效果出来了,对于个relative,真是难理解。

要说是相对定位嘛,也不是。

对于页面中一个static的div#demo,我想让这个#demo里的一个div#sub相对于#demo定位在右上角的某个地方,应该是用这个position:relative这个吧,然后再加上top,right给限制一下。

我理解得没有错吧?

不过在现实开发中,我却常常 得不到预期的效果。我觉得应该是我对这个属性还没有理解透。现在我想完全掌握这个,就像position:absolute那样地掌握它,想怎么弄html元素就怎么弄HTML元素。

请理解这一点的朋友们给讲讲,或者给点资料,链接之类 的也可以。

案例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>position</title>
<style type="text/css">
<!--
body{
font-size:12px;
margin:0 auto;
}

div#demo{
position:relative;
border:1px solid #000;
margin:50px;
top:-50px;
line-height:18px;
overflow:hidden;
clear:both;
height:1%;
}

div#sub{
position:absolute;
right:10px;
top:10px;
}

div.relative{
position:relative;
left:400px;
top:-20px;
}

div.static,div.fixed,div.absolute,div.relative{
width:300px;
}

div.static{
background-color:#bbb;
position:static;
}

div.fixed{
background-color:#ffc0cb;
}

div.absolute{
background-color:#b0c4de;
}

div.relative{
background-color:#ffe4e1;
}
-->
</style>
</head>
<body>
<div id="demo">
<div class="static">static: 默认值。无特殊定位,对象遵循HTML定位规则 </div>
<div id="sub" class="absolute">absolute: 将对象从文档流中拖出,使用left,right,top,bottom 等属性相对于其最接近的一个最有定位设置的父对象进行绝对定位。如果不存在这样的父对象,则依据body对象。而其层叠通过z-index属性定义 </div>
<div class="fixed">fixed:未支持。对象定位遵从绝对(absolute)方式。但是要遵守一些规范 </div>
<div class="relative">relative:对象不可层叠,但将依据 left,right,top,bottom 等属性在正常文档流中偏移位置 </div>
</div>
</body>
</html>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html 文档 class xhtml css ie