本文內容:
1.利用HTML5及float布局
2.利用inline block布局
3.利用CSS表格(table)布局
示例代碼 -1 (利用HTML5標簽及浮動布局)
<style>header nav ul {margin:15px;list-style: none;height:50px;}header nav ul li {font-size:1.5em;color: coral;margin:10px;float: left;}header nav ul li a {text-decoration: none;}aside {margin-right:50px;float: left;width:200px;}aside ul {list-style: none;}article {float: left;}footer {font-family:Arial;clear: both;text-align: center;}</style><!--定義頁眉--><header><nav><ul><li><ahref="#">主鏈接一</a></li><li><ahref="JavaScript:void (0)">主鏈接二</a></li><li><aonclick="return false"href="index.html">主鏈接三</a></li></ul></nav></header><hr/><!--定義左邊側欄導航--><aside><ul><li>左側導航欄鏈接1</li><li>左側導航欄鏈接2</li></ul></aside><!--定義右邊內容顯示區--><article><section><h2>主題內容標題1</h2><p>......</p></section><section><h2>主題內容標題2</h2><p>.......</p></section><section><h2>主題內容標題3</h2><p>.......</p></section></article><!--定義頁腳--><footer><hr/><!--footer的樣式設置為clear:both,以便使其始終居於底部,並占滿整行。-->©Jener_Yan <spanid="DateSpan"></span></footer><script>/*獲取當前年份*/var d = document.getElementById("DateSpan");varNowYear=newDate();d.innerHTML =NowYear.getFullYear();</script>
網頁效果圖
注意點:浮動的設置
******************************************************************************************************
示例代碼 -2 (利用inline block布局)
<style>body {text-align: center;}div {display:inline-block;min-height:200px;padding:5px;margin:0;}header, footer {background-color: aquamarine;padding:5px;margin:5px;}main {width:100%;margin:0;}#main-l {background-color: chartreuse;width:20%;}#main-m {background-color: bisque;width:50%;/*此處的寬度占比不取60%是因為,還得留些空間給內邊距(Padding)和外邊距(Margin)*/}#main-r {background-color: coral;width:20%;}</style><h2>inline block布局</h2><header>header</header><main><divid="main-l">左側欄寬 20%</div><divid="main-m">中部欄寬 50%</div><divid="main-r">右側欄寬 20%</div></main><footer>©Jener_Yan <spanid="DateSpan"></span></footer><script>/*獲取當前年份*/var d = document.getElementById("DateSpan");varNowYear=newDate();d.innerHTML =NowYear.getFullYear();</script>
網頁效果圖
注意點:width中對margin的預留空間。
******************************************************************************************************
示例代碼 -3 (利用CSS表格式布局)
示例代碼
<style>/*全局設置*/div {min-height:100px;margin:0auto;padding:6px;}header, footer {padding:3px;background-color: darkgray;margin:4px0;width:100%;}.container {text-align: center;}.content {display: table;/*將div轉化為表格模式顯示*/width:100%;}/*上部三個欄目*/.upContent {display: table-row;/*將div轉換為行顯示*/}.upContentL {background-color:#fffefe;width:20%;display: table-cell;/*將div轉化為單元格顯示*/}.upContentM {background-color: aqua;width:60%;display: table-cell;}.upContentR {display: table-cell;background-color: skyblue;width:20%;}/*下部兩個欄目*/.downContent {display: table-row;width:100%;}.downContentL {background-color: aquamarine;display: table-cell;width:30%;}.downContentR {background-color: lightcyan;width:70%;display: table-cell;}</style><divclass="container"><h2>CSS表格式布局</h2><header>header</header><divclass="content"><divclass="upContent"><divclass="upContentL">上部左欄 20%</div><divclass="upContentM">上部中欄 60%</div><divclass="upContentR">上部右欄 20%</div></div></div><divclass="content"><divclass="downContent"><divclass="downContentL">下部左欄 20%</div><divclass="downContentR">下部右欄 80%</div></div></div><footer>©Jener_Yan <spanid="DateSpan"></span></footer></div><script>var date =newDate();document.getElementById("DateSpan").innerHTML = date.getFullYear();</script>
網頁效果圖
注意點:采用表格式布局注意顯示(display)屬性值的設置,table → table-row → table-cell
