對比圖:css
下面直接上代碼:瀏覽器
聖盃佈局:ide
<body> <div id="hd">header</div> <div id="bd"> <div id="middle">middle</div> <div id="left">left</div> <div id="right">right</div> </div> <div id="footer">footer</div> </body> <style> #hd{ height:50px; background: #666; text-align: center; } #bd{ /*左右欄經過添加負的margin放到正確的位置了,此段代碼是爲了擺正中間欄的位置*/ padding:0 200px 0 180px; height:100px; } #middle{ float:left; width:100%;/*左欄上去到第一行*/ height:100px; background:blue; } #left{ float:left; width:180px; height:100px; margin-left:-100%; background:#0c9; /*中間欄的位置擺正以後,左欄的位置也相應右移,經過相對定位的left恢復到正確位置*/ position:relative; left:-180px; } #right{ float:left; width:200px; height:100px; margin-left:-200px; background:#0c9; /*中間欄的位置擺正以後,右欄的位置也相應左移,經過相對定位的right恢復到正確位置*/ position:relative; right:-200px; } #footer{ height:50px; background: #666; text-align: center; } </style>
雙飛翼佈局:佈局
<body> <div id="hd">header</div> <div id="middle"> <div id="inside">middle</div> </div> <div id="left">left</div> <div id="right">right</div> <div id="footer">footer</div> </body> <style> #hd{ height:50px; background: #666; text-align: center; } #middle{ float:left; width:100%;/*左欄上去到第一行*/ height:100px; background:blue; } #left{ float:left; width:180px; height:100px; margin-left:-100%; background:#0c9; } #right{ float:left; width:200px; height:100px; margin-left:-200px; background:#0c9; } /*給內部div添加margin,把內容放到中間欄,其實整個背景仍是100%*/ #inside{ margin:0 200px 0 180px; height:100px; } #footer{ clear:both; /*記得清楚浮動*/ height:50px; background: #666; text-align: center; } </style>
<body> <style type="text/css"> body{margin:0; padding:0;} .boxA{width:180px;background: #CD0000 } .boxB{width:600px;background: #9ACD32} .boxC{width:180px;background: #87CEFF} .box{ height: 300px; float: left;} /*ABC*/ /*.boxA{position: relative;left:-960px;} .boxB{margin-left:180px;}*/ /*CBA*/ .container{ padding: 0 180px;} .boxB{width: 100%; text-align: right;} .boxC{position: relative;margin-left: -180px;left:-100%;} .boxA{position: relative;margin-left: -180px;right:-180px;} /*BAC*/ /*.boxC{float:right;} .container{width: 960px;}*/ </style> <p>現有並列的三列布局結構,從左至右依次爲 A, B, C, 寬度分別爲180px, 600px, 180px。要求在不改變 Html 結構的狀況下用CSS實現:ABC,CBA,BAC 三種佈局及在CBA排列下使B寬度自適應(三列總寬度100%),不能使用針對瀏覽器的CSS Hack.</p> <div class="container"> <div class="box boxB">B</div> <div class="box boxC">C</div> <div class="box boxA">A</div> </div> </body>