以下是一份可直接複製的佈局片段清單,涵蓋單欄到三欄、絕對定位與浮動定位兩大流派。
body { margin: 0px; padding: 0px; text-align: center; }
#content { margin-left: auto; margin-right: auto; width: 400px; }
body { margin: 0px; padding: 0px; text-align: center; }
#content-top { margin-left: auto; margin-right: auto; width: 400px; }
#content-end { margin-left: auto; margin-right: auto; width: 400px; }
body { margin: 0px; padding: 0px; text-align: center; }
#content-top { margin-left: auto; margin-right: auto; width: 400px; }
#content-mid { margin-left: auto; margin-right: auto; width: 400px; }
#content-end { margin-left: auto; margin-right: auto; width: 400px; }
#bodycenter { width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
#bodycenter #dv1 { float: left; width: 280px; }
#bodycenter #dv2 { float: right; width: 410px; }
#header { width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
#bodycenter { width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
#bodycenter #dv1 { float: left; width: 280px; }
#bodycenter #dv2 { float: right; width: 410px; }
#header { width: 700px; margin-right: auto; margin-left: auto; }
#bodycenter { width: 700px; margin-right: auto; margin-left: auto; }
#bodycenter #dv1 { float: left; width: 280px; }
#bodycenter #dv2 { float: right; width: 410px; }
#footer { width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
#left { position: absolute; top: 0px; left: 0px; width: 120px; }
#middle { margin: 20px 190px; }
#right { position: absolute; top: 0px; right: 0px; width: 120px; }
<div id="wrap">
<div id="column">
<div id="column1">這裡是第一列</div>
<div id="column2">這裡是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這裡是第三列</div>
<div class="clear"></div>
</div>
#wrap { width: 100%; height: auto; }
#column { float: left; width: 60%; }
#column1 { float: left; width: 30%; }
#column2 { float: right; width: 30%; }
#column3 { float: right; width: 40%; }
.clear { clear: both; }
<div id="center" class="column"><h1>This is the main content.</h1></div>
<div id="left" class="column"><h2>This is the left sidebar.</h2></div>
<div id="right" class="column"><h2>This is the right sidebar.</h2></div>
body {
margin: 0;
padding-left: 200px;
padding-right: 190px;
min-width: 240px;
}
.column { position: relative; float: left; }
#center { width: 100%; }
#left { width: 180px; right: 240px; margin-left: -100%; }
#right { width: 130px; margin-right: -100%; }
<div id="header">這裡是頂行</div>
<div id="wrap">
<div id="column">
<div id="column1">這裡是第一列</div>
<div id="column2">這裡是第二列</div>
<div class="clear"></div>
</div>
<div id="column3">這裡是第三列</div>
<div class="clear"></div>
</div>
#header { width: 100%; height: auto; }
#wrap { width: 100%; height: auto; }
#column { float: left; width: 60%; }
#column1 { float: left; width: 30%; }
#column2 { float: right; width: 30%; }
#column3 { float: right; width: 40%; }
.clear { clear: both; }
在上面兩行三列的結構前後各補上 header 與 footer 即可:
#header { width: 100%; height: auto; }
#wrap { width: 100%; height: auto; }
#column { float: left; width: 60%; }
#column1 { float: left; width: 30%; }
#column2 { float: right; width: 30%; }
#column3 { float: right; width: 40%; }
.clear { clear: both; }
#footer { width: 100%; height: auto; clear: both; }
使用這批模板時有兩點要留意:一是所有浮動容器記得補 overflow: auto 或清除元素,避免高度塌陷;二是百分比寬度相加不要超過 100%,否則最後一欄會被擠到下一行。