DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
DIV+CSS 常用佈局程式碼速查:一到三欄一次收齊
編輯:布局實例     

以下是一份可直接複製的佈局片段清單,涵蓋單欄到三欄、絕對定位與浮動定位兩大流派。

單行一列

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; }

float 定位寫法一

<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; }

float 定位寫法二(中間通欄)

<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%,否則最後一欄會被擠到下一行。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved