Position 属性设置或返回用于元素的定位方法的类型(静态、相对、绝对或固定)。
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
返回位置属性:
object.style.position
设置位置属性:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
Value | Description |
---|---|
static | Elements renders in order, as they appear in the document flow. This is default. |
absolute | The element is positioned relative to its first positioned (not static) ancestor element |
fixed | The element is positioned relative to the browser window |
relative | The element is positioned relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position |
sticky | The element is positioned based on the user's scroll position A sticky element toggles between |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
默认值: | 静止的 |
---|---|
返回值: | String,表示元素的位置类型 |
CSS版本 | CSS2 |
使用不同的位置类型:
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 参考:位置属性
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!