DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS特效代碼
拋磚引玉:div+css 與 table 的寬度自適應布局
編輯:CSS特效代碼     

所謂寬度自適應布局,就是當瀏覽器視窗大小改變時,頁面元素的寬度隨之調整,從而達到自適應效果。常見形式有:兩欄(左固定、右自適應)、三欄(左右固定、中間自適應)、三欄(左右自適應、中間固定)。

一、利用 div+css 實現

(1)兩欄:左固定、右自適應

利用 div + float + margin:

<!DOCTYPE html>
<head>
<title>width_layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<style type="text/css">
.content { min-width:300px; }
.div1 { width:200px; height:300px; background:green; float:left; }
.div2 { height:300px; background:pink; margin-left:200px; }
</style>
</head>
<body>
<div class="content">
<div class="div1"></div>
<div class="div2"></div>
</div>
</body>
</html>

(2)三欄:左右固定、中間自適應

思路:左右兩欄分別左浮動與右浮動,中間欄不管寬度,將它的 margin-left/right 設為與左右兩欄的固定寬一致。

<!DOCTYPE html>
<head>
<title>layout2</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<style>
* { margin:0; padding:0; }
.main { height:300px; width:100%; min-width:400px; }
.main_left { height:300px; width:200px; float:left; background-color:green; text-align:center; }
.main_center { height:300px; margin-left:200px; margin-right:100px; text-align:center; background-color:pink; }
.main_right { height:300px; width:100px; float:right; text-align:center; background-color:blue; }
</style>
</head>
<body>
<div class="main">
<div class="main_left">我是左邊部分,寬度不變</div>
<div class="main_right">我是右邊部分,寬度不變</div>
<div class="main_center">我是中間部分,寬度自適應</div>
</div>
</body>
</html>

(3)三欄:左右自適應、中間固定

思路:若左右兩欄寬度一樣,各設為父元素的 50%,再將左右兩欄的 margin-left 設為中間欄固定寬度的一半,三欄都左浮動即可。

<!DOCTYPE html>
<head>
<title>layout3</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<style>
body { min-width:500px; }
#left, #right { float: left; margin: 0 0 0 -101px; width: 50%; height:58px; *width: 49.9%; }
#main { width: 200px; height:58px; float: left; background: green; }
.inner { height: 100%; }
#left .inner, #right .inner { margin: 0 0 0 101px; background: orange; }
</style>
</head>
<body>
<div id="left"><div class="inner">left</div></div>
<div id="main"><div class="inner">中間 width 不變,兩邊自適應</div></div>
<div id="right"><div class="inner">right</div></div>
</body>
</html>

二、利用 table+css 實現

由於 table 自帶一些特性(如文字自動垂直居中、未設 td 寬度時瀏覽器自動渲染),實現上述布局相對容易。

(1)兩欄:左固定、右自適應

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>table_layout</title>
<style>
.m_table { width:100%; height:100px; text-align:center; }
.left_td { width:300px; background-color:#98FF1A; }
.right_td { background-color:#7CC0FF; }
</style>
</head>
<body>
<table class="m_table">
<tr>
<td class="left_td">這個是左邊部分,寬度確定</td>
<td class="right_td">這個是右邊部分,寬度自動擴展</td>
</tr>
</table>
</body>
</html>

(2)三欄:左右固定、中間自適應

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>table_layout2</title>
<style>
.m_table { width:100%; height:400px; text-align:center; }
.left_td { width:200px; height:300px; min-width:200px; background-color:green; }
.center_td { height:300px; background-color:pink; }
.right_td { width:200px; height:300px; min-width:200px; background-color:blue; }
</style>
</head>
<body>
<table class="m_table">
<tr>
<td class="left_td">我是左邊部分,寬度不變</td>
<td class="center_td">我是中間部分,寬度自適應</td>
<td class="right_td">我是右邊部分,寬度不變</td>
</tr>
</table>
</body>
</html>

(3)左右自適應、中間固定

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>table_layout3</title>
<style>
.m_table { width:100%; min-width:500px; height:58px; text-align:center; }
.left_td { height:58px; background-color:orange; }
.center_td { height:58px; width:200px; background-color:green; }
.right_td { height:58px; background-color:orange; }
</style>
</head>
<body>
<table class="m_table">
<tr>
<td class="left_td">我是左邊部分,寬度自適應</td>
<td class="center_td">我是中間部分,寬度不變</td>
<td class="right_td">我是右邊部分,寬度自適應</td>
</tr>
</table>
</body>
</html>

三、div+css 與 table+css 布局的比較

1、table 結構的網站逐格渲染,打開速度較慢;DIV+CSS 結構打開速度快。

2、DIV+CSS 更利於搜尋引擎蜘蛛爬取,table 結構則爬取較費力。

3、table 結構架構單調,方方正正,想做圓潤或流線效果需繪製大量邊框圖片;DIV+CSS 樣式豐富,可用結構做出多變效果。

4、table 結構樣式寫在頁面程式碼裡,冗餘、可讀性與二次開發性差,修改麻煩;DIV+CSS 結構與樣式分離,可讀性與二次修改都更方便。

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