position详解

本文详细解析了CSS中元素的五种定位方式:static、relative、absolute、fixed及inherit。阐述了每种定位方式的特点及其如何影响页面布局,并特别强调了absolute和fixed定位在实际应用中的注意事项。

position: static|relative|absolute|fixed;

Value Description
static(default) Elements renders in order, as they appear in the document flow.
absolute The element is positioned relative to its first positioned (not static) ancestor element
fixed(ie6 not supported) 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
inherit The value of the position property is inherited from the parent element

position:absolute;

设置此属性值为 absolute 会将对象拖离出正常的文档流绝对定位而不考虑它周围内容的布局。假如其他具有不同 z-index 属性的对象已经占据了给定的位置,他们之间不会相互影响,而会在同一位置层叠。此时对象不具有外边距( margin ),但仍有内边距( padding )和边框( border )。
要激活对象的绝对(absolute)定位,必须指定 left , right , top , bottom 属性中的至少一个,并且设置此属性值为absolute 。否则上述属性会使用他们的默认值 auto ,这将导致对象遵从正常的HTML布局规则,在前一个对象之后立即被呈递。

TRBL属性(TOP、RIGHT、BOTTOM、LEFT)只有当设定了position属性才有效。
当设定position:absolute
如果父级(无限)默认设置position:static属性,那么当前的absolute则结合TRBL属性以浏览器左上角为原始点进行定位
如果父级(无限)设定position属性为非static值,那么当前的absolute则结合TRBL属性以父级(最近)的左上角为原始点进行定位。


position:fixed;

ie6不支持position:fixed;值,解决此问题可参考http://blog.youkuaiyun.com/bill200711022/article/details/7046286


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值