這裏的demo我只是舉一下absolute的例子,由於fixed和relative都比較好理解,就沒必要多說。css
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> <style type="text/css"> div{border:2px red solid;} body{background-color: #999;} #box1{ width: 400px; height: 400px; padding: 20px; background-color: #444; margin-left: 100px; margin-top: 100px; } #box2{ width: 300px; height: 300px; background-color: #777; position: absolute; } </style> </head> <body> <div id="box1"> <div id="box2"> </div> </div> </body> </html>
效果
html
這個就是未設置TRBL時效果。接着咱們設置一下TBRL來看看,更改一下代碼瀏覽器
#box2{ width: 300px; height: 300px; background-color: #777; position: absolute; top:50px; left:50px; }
效果:
佈局
這個box2就是相對於根元素來定位的。由於它的祖輩容器中沒有一個設置了postion的。接着咱們在box1中添加position: relative;post
#box1{ width: 400px; height: 400px; padding: 20px; background-color: #444; margin-left: 100px; margin-top: 100px; position: relative; }
效果:
以上三種狀況就簡明解釋了absolute的一些特性。優化
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title></title> <style type="text/css"> div{border:2px red solid;} body{background-color: #999;} #box1{ width: 400px; height: 400px; padding: 20px; background-color: #444; margin-left: 100px; margin-top: 100px; position: relative; } #box2{ width: 300px; height: 300px; background-color: #777; position: absolute; top:50px; left:50px; } </style> </head> <body> <div id="box1"> <div id="box2"> </div> <p>這是一段文字,若是看不見我就說明我被遮住了,或者被遮住一部分。</p> </div> </body> </html>