标签:
position:static | relative | absolute | fixed | center | page | sticky
默认值:static
static:对象遵循常规流。top,right,bottom,left等属性不会被应用。
relative:对象遵循常规流,并且参照自身在常规流中的位置通过top,right,bottom,left属性进行偏移时不影响常规流中的任何元素。
absolute:对象脱离常规流,使用top,right,bottom,left等属性进行绝对定位,盒子的偏移位置不影响常规流中的任何元素,其margin不与其他任何margin折叠。
fixed:对象脱离常规流,使用top,right,bottom,left等属性以窗口为参考点进行定位,当出现滚动条时,对象不会随着滚动。
center:对象脱离常规流,使用top,right,bottom,left等属性指定盒子的位置或尺寸大小。盒子在其包含容器垂直水平居中。盒子的偏移位置不影响常规流中的任何元素,其margin不与其他任何margin折叠。(CSS3)
page:盒子的位置计算参照absolute。盒子在分页媒体或者区域块内,盒子的包含块始终是初始包含块,否则取决于每个absolute模式。(CSS3)
sticky:对象在常态时遵循常规流。它就像是 relative 和 fixed 的合体,当在屏幕中时按常规流排版,当卷动到屏幕外时则表现如fixed。该属性的表现是现实中你见到的吸附效果。(CSS3)
示例:
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="utf-8" /> <style> #position { position: absolute; top: 50%; left: 50%; width: 150px; height: 40px; margin: -20px 0 0 -75px; padding: 0 10px; background: #eee; line-height: 2.4; } </style> </head> <body> <div id="position">水平垂直居中的对象</div> </body> </html>
z-index: auto | <integer>
默认值:auto
适用于:定位元素。即定义了position为 relative | absolute | fixed | center | page | sticky 的元素
示例:
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="utf-8" /> <style> .z1,.z2,.z3 { position: absolute; width: 200px; height: 100px; padding: 5px 10px; color: #fff; text-align: right; } .z1:hover,.z2:hover,.z3:hover { background:#0000FF } .z1 { z-index: 1; background: #000; } .z2 { z-index: 2; top: 30px; left: 30px; background: #C00; } .z3 { z-index: 3; top: 60px; left: 60px; background: #999; } </style> </head> <body> <div class="z1">z-index:1</div> <div class="z2">z-index:2</div> <div class="z3">z-index:3</div> </body> </html>
参数:auto | <length> | <percentage>
默认值:auto
适用于:定位元素。即定义了position为 relative | absolute | fixed | center | page 的元素,static时候无效
示例:
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="utf-8" /> <style> .test { position: absolute; bottom: 0; left:50%; width: 200px; height: 100px; margin: -50px 0 0 -100px; padding: 0px 25px; background: #c00; color: #fff; line-height: 5; } </style> </head> <body> <div class="test">我将出现在浏览器底部</div> </body> </html>
标签:
原文地址:http://www.cnblogs.com/xiepeixing/p/4334380.html