定义和用法
position 属性把元素放置到一个静态的、相对的、绝对的、或固定的位置中。
语法:
Object.style.position=static|relative|absolute|fixed
可能的值
值 | 描述 |
---|---|
static | 默认。位置设置为 static 的元素,它始终会处于页面流给予的位置(static 元素会忽略任何 top、bottom、left 或 right 声明)。 |
relative | 位置被设置为 relative 的元素,可将其移至相对于其正常位置的地方,因此 "left:20" 会将元素移至元素正常位置左边 20 个像素的位置。 |
absolute | 位置设置为 absolute 的元素,可定位于相对于包含它的元素的指定坐标。此元素的位置可通过 "left"、"top"、"right" 以及 "bottom" 属性来规定。 |
fixed | 位置被设置为 fixed 的元素,可定位于相对于浏览器窗口的指定坐标。此元素的位置可通过 "left"、"top"、"right" 以及"bottom" 属性来规定。不论窗口滚动与否,元素都会留在那个位置。工作于 IE7(strict 模式)。 |
示例:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css" >
.relative{clear:both;width:100%;}
div{float:left;width:200px ; height:100px ;}
.div1{ background:#999; position:relative;left:100px;}
.div2{background:#06F}
.absolute{clear:both;width:100%;position:relative;left:20px;}
.div3{ background:#999;position:absolute;right:0px;}
.div4{background:#06F}
.fixed{clear:both;width:100%;position: fixed;left:400px; z-index:1;}
.div5{ background:#990;}
</style>
</head>
<body>
<div class="relative">
<div class="div1">div1</div>
<div class="div2">div2</div>
</div>
<div class="absolute">
<div class="div3">div3</div>
<div class="div4">div4</div>
</div>
<div class="fixed">
<div class="div5">div5</div>
<div class="div6">div6</div>
</div>
</body>
</html>