CSS position 属性
更多“自己尝试”的例子见下文。
定义和用法
The position
property specifies the type of positioning method used for an element (static, relative, absolute, fixed, or sticky).
浏览器支持
表中的数字指定了完全支持该属性的第一个浏览器版本。
属性 | |||||
---|---|---|---|---|---|
position | 1.0 | 7.0 | 1.0 | 1.0 | 4.0 |
注意: sticky
值在 Edge 15 及更早版本中不受支持。
CSS 语法
position: static|absolute|fixed|relative|sticky|initial|inherit;
属性值
值 | 描述 | 演示 |
---|---|---|
static | 默认值。元素按照它们在文档流中的顺序进行渲染 | 试一试 » |
absolute | 元素相对于其第一个定位(非 static)的祖先元素进行定位 | 试一试 » |
fixed | 元素相对于浏览器窗口进行定位 | 试一试 » |
relative | 元素相对于其正常位置进行定位,因此 "left:20px" 会在元素的左侧位置增加 20 像素 | 试一试 » |
sticky | 元素根据用户的滚动位置进行定位 粘性元素在 |
试一试 » |
initial | 将此属性设置为其默认值。阅读关于initial | |
inherit | 从其父元素继承此属性。阅读关于inherit |
更多示例
示例
如何相对于其正常位置定位元素
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
自己动手试一试 »
示例
更多定位
#parent1 {
position: static;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child1 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent2 {
position: relative;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child2 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
自己动手试一试 »
相关页面
CSS 教程:CSS 定位
HTML DOM 参考:position 属性