position:static | relative | absolute | fixed | center | page | stickycss
默認值:statichtml
static:對象遵循常規流。top,right,bottom,left等屬性不會被應用。css3
relative:對象遵循常規流,而且參照自身在常規流中的位置經過top,right,bottom,left屬性進行偏移時不影響常規流中的任何元素。瀏覽器
absolute:對象脫離常規流,使用top,right,bottom,left等屬性進行絕對定位,盒子的偏移位置不影響常規流中的任何元素,其margin不與其餘任何margin摺疊。spa
fixed:對象脫離常規流,使用top,right,bottom,left等屬性以窗口爲參考點進行定位,當出現滾動條時,對象不會隨着滾動。code
center:對象脫離常規流,使用top,right,bottom,left等屬性指定盒子的位置或尺寸大小。盒子在其包含容器垂直水平居中。盒子的偏移位置不影響常規流中的任何元素,其margin不與其餘任何margin摺疊。(CSS3)htm
page:盒子的位置計算參照absolute。盒子在分頁媒體或者區域塊內,盒子的包含塊始終是初始包含塊,不然取決於每一個absolute模式。(CSS3)對象
sticky:對象在常態時遵循常規流。它就像是 relative 和 fixed 的合體,當在屏幕中時按常規流排版,當捲動到屏幕外時則表現如fixed。該屬性的表現是現實中你見到的吸附效果。(CSS3)blog
示例:utf-8
<!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>