css中position:relative的真正理解
  其实话说⼀直以来也没真正去理解好position:relative的⽤法的真实意义。
  我想很多⼈实实在在⽤的多都是position:relative和position:absolute结合起来⼀起⽤的。
  position属性是⽤四种定位。默认的是static。
  position:absolute(绝对定位) ——是脱离⽂档流,相对于⽗级元素(包含这个position:relative)定位,当然如果没有,那就是相对于body定位的。
  position:relative(相对定位) ——单独使⽤,我不知道很多⼈是不是也跟我⼀样忽略过它,relative 也是不脱离⽂档流,“这个元素会偏移某个距离。但是该元素仍保持其未
css固定定位定位前的形状,它原本所占的空间仍保留。”,它是相对于⾃⼰来定位的,例如:#main{position:relative;top:-50px;},这时#main会在相对于它原来的位置上移50px。
  position:fixed(固定定位)  —— 跟绝对定位有点类似,只是它的⽗级元素永远都是视窗本⾝。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="/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;
}
position:relative;
left:400px;
top:-20px;
}
div.static,div.fixed,div.lative{
width:300px;
}
div.static{
background-color:#bbb;
position:static;
}
div.fixed{
background-color:#ffc0cb;
}
div.absolute{
background-color:#b0c4de;
}
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对        <div class="fixed">fixed:未⽀持。对象定位遵从绝对(absolute)⽅式。但是要遵守⼀些规范 </div>
<div class="relative">relative:对象不可层叠,但将依据 left,right,top,bottom 等属性在正常⽂档流中
偏移位置 </div>
</div>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。