DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS進階教程
CSS 教程:三欄固定寬度版面配置實作
編輯:CSS進階教程     

本篇動手做一個最常見的版型:固定寬度的三欄式頁面。我們會把 logo 區、頂部導覽列、主要內容欄、分類側欄,以及一個用來放 120×600 廣告的右欄全部排出來,程式碼可以直接複用到你的前端專案裡。

版面結構規劃

下圖說明我在頁面中安排的元素與其對應關係。

這個骨架本身已經可以直接跑起來。你接下來只要重新定義字型、配色、各區塊的間距,再補上自己的元件樣式即可。

步驟一:HTML 結構

新增頁面,把以下程式碼貼進 <body>:

<div id=”container”>
<div id=”topbar”>頂欄/Logo層</div>
<div id=”navbar”>
    <a href=“index.html?home”>Home</a>
    <a href=“index.html?about”>About</a>
    <a href=“mailto:myemailaddres@email.com”>Contact me</a>
</div>
<div id=”main”>
<div id=”column_left”>
    <h1>文章標題</h1>
    <h2>2008年12月5日</h2>
    <p>在這裡添加您的文本內容</p>
</div>
<div id=”column_right”>
    <h3>分類</h3>
     右側內容添加分類或widget (twitter、 我博客的讀者等…)
</div>
<div id=”column_right_adsense”>
    <h3>AdSense</h3>
    Adsense 120 X 600
</div>
<!– Don’t remove spacer div. Solve an issue about container height –>
<div class=”spacer”></div>
</div>
<div id=”footer”>? 2008 Information about your site</div>
</div>

這裡有個關鍵細節:.spacer 這個清除浮動的空 div 不能刪,它負責把 #main 的高度撐回正常值,否則後面的頁尾會跑到側欄中間去。

步驟二:CSS 樣式

建立 CSS 檔並連結到頁面:

/* ——————————
HTML重定義標簽
—————————— */
body{font-family:Arial, Helvetica, sans-serif; font-size:12px; margin:20px; padding:0;}

input, form, textarea
h1, h2, h3, h4, h5, h6{margin:0; padding:0;}
h1{font-size:18px;}
h2{font-size:14px; color:#999999;}
h3{font-size:13px; border-bottom:solid 1px #DEDEDE; padding:4px 0; margin-bottom:10px;}

a:link, a:visited{color:#0033CC;}
a:hover{text-decoration:none;}

/* ——————————
頁面結構
—————————— */

/* #container has an absolute width (780 pixel) */

#container{width:780px; margin:0 auto;}
    #topbar{width:auto; display:block; height:60px;}
    #navbar{width:auto; display:block; height:28px;}
        #navbar a{heigth:28px; line-height:28px; padding:0 8px; display:inline;}

#main{width:auto; display:block; padding:10px 0;}
    #column_left{width:460px; margin-right:20px; float:left;}
    #column_right{width:160px; margin-right:20px; float:left;}
    #column_right_adsense{width:120px; float:left;}
    div.spacer{clear:both; height:10px; display:block;}

#footer{width:auto; display:block; padding:10px 0; font-size:11px; color:#666666;}

/* ——————————
自定義類
—————————— */

/* 在這裡添加您的自定義類 … */

存檔後就能直接開啟預覽。

幾個實務提醒:三欄寬度相加(460 + 160 + 120)加上兩段 20px 間距,再加上 container 未扣的內容寬度,總和必須小於 780px,否則在部分瀏覽器中會發生換行錯位——這正是固定寬度版型最常見的翻车點。另外範例中 #navbar a 的 heigth 是拼字錯誤,正確應為 height,實際上該行由 line-height 撐開高度,所以問題不明顯,但複用時建議修正。最後,若專案已不需支援 IE6/7,可把 .spacer 換成在 #main 上套用 clearfix,結構會更乾淨。

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