聊聊三欄佈局----左右定寬,中間自適應。
效果圖:html
聖盃佈局
<!DOCTYPE html> <html> <head lang="en"> <title>聖盃</title> <style> .container{ padding:0 200px 0 180px; height:100px; } .left{ float:left; width:180px; height:100px; margin-left:-100%; background:red; position:relative; left:-180px; } .main{ float:left; width:100%; height:100px; background:blue; } .right{ float:left; width:200px; height:100px; margin-left:-200px; background:green; position:relative; right:-200px; } </style> </head> <body> <div class="container"> <div class="main">middle</div> <div class="left">left</div> <div class="right">right</div> </div> </body> </html>
雙飛翼佈局
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>雙飛翼</title> <style> .main{ float:left; width:100%; height:100px; background:blue; } .left{ float:left; width:180px; height:100px; margin-left:-100%; background:red; } .right{ float:left; width:200px; height:100px; margin-left:-200px; background:green; } .inline{ margin:0 200px 0 180px; } </style> </head> <body> <div class="main"> <div class="inline">middle</div> </div> <div class="left">left</div> <div class="right">right</div> </body> </html>
注意
:必定要在要在main中再包裹一個<div>並設置它的margin:0 180px 0 200px。佈局
Flex佈局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Flex</title> <style> .flex { display: flex; flex-flow: row; } .left{ width: 180px; height: 100px; background-color: red; } .main{ flex: 1; height: 100px; background-color: blue; } .right { width: 200px; height: 100px; background-color: green; } </style> </head> <body> <div class="flex"> <div class="left">left</div> <div class="main">middle</div> <div class="right">right</div> </div> </body> </html>
最重要的仍是要理解浮動和負margin技術以及width:100%。flex