Style position 属性
示例
将 <div> 元素的 position 从相对改为绝对
document.getElementById("myDIV").style.position = "absolute";
自己动手试一试 »
描述
position 属性设置或返回元素使用的定位方法类型(static、relative、absolute 或 fixed)。
浏览器支持
属性 | |||||
---|---|---|---|---|---|
position | 是 | 是 | 是 | 是 | 是 |
语法
返回 position 属性
object.style.position
设置 position 属性
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
属性值
值 | 描述 |
---|---|
static | 元素按文档流中出现的顺序渲染。这是默认值。 |
absolute | 元素相对于其第一个已定位(非静态)的祖先元素定位 |
fixed | 元素相对于浏览器窗口定位 |
relative | 元素相对于其正常位置定位,因此 "left:20" 会在元素的 LEFT 位置增加 20 像素 |
sticky | 元素根据用户的滚动位置定位 粘性元素在 |
initial | 将此属性设置为其默认值。阅读关于 initial |
inherit | 从其父元素继承此属性。阅读关于 inherit |
技术详情
默认值 | static |
---|---|
返回值 | 一个字符串,表示元素的 position 类型 |
CSS 版本 | CSS2 |
更多示例
示例
使用不同的 position 类型
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
自己动手试一试 »
相关页面
CSS 教程:CSS 定位
CSS 参考:position 属性