Style position 属性

Style 对象参考手册 Style 对象

定义和用法

position 属性设置或返回用于元素定位方法的类型(static(静态的)、relative(相对的)、absolute(绝对的)或 fixed(固定的))。

语法

设置 position 属性:

Object.style.position="static|absolute|fixed|relative|inherit"

返回 position 属性:

Object.style.position

描述
static 默认。位置设置为 static 的元素,它始终会处于页面流给予的位置(static 元素会忽略任何 top、bottom、left 或 right 声明)。
absolute 位置设置为 absolute 的元素,可定位于相对于第一个已定位(非静态的)的包含它的元素的指定坐标。此元素的位置可通过 "left"、"top"、"right" 以及 "bottom" 属性来规定。
fixed 位置被设置为 fixed 的元素,可定位于相对于浏览器窗口的指定坐标。此元素的位置可通过 "left"、"top"、"right" 以及 "bottom" 属性来规定。不论窗口滚动与否,元素都会留在那个位置。工作于 IE7(strict 模式)。
relative 位置被设置为 relative 的元素,可将其定位于相对于其正常位置的地方,因此 "left:20" 会将元素移至元素正常位置左边 20 个像素的位置。
inherit position 属性的值从父元素继承。


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 position 属性。

注意:IE7 及更早的版本不支持 "inherit" 值。IE8 只有规定了 !DOCTYPE 才支持 "inherit"。IE9 支持 "inherit"。


实例

实例

把元素的 position(位置)从 static(静态,默认)改为 absolute(绝对):

<html>
<head>
<script>
function displayResult()
{
document.getElementById("b1").style.position="absolute";
document.getElementById("b1").style.top="100px";
document.getElementById("b1").style.left="100px";
}
</script>
</head>
<body>

<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<input type="button" id="b1" onclick="displayResult()" value="Position me">

</body>
</html>

尝试一下 »


Style 对象参考手册 Style 对象