天天看點

HTML頁面的布局方案

作者:程式設計實踐
HTML頁面的布局方案

當我們剛開始學習Web前端時,就對HTML頁面的布局特别感興趣,上一講《CSS初步介紹》中,講解了CSS的入門知識,現在我們介紹一下HTML布局的知識。

HTML布局的方案有Table布局、最流行的DIV布局、以及HTML5建議的DIV布局的替代方案。

當HTML内容被浏覽器顯示時,整個HTML頁面對使用者來說,就是一個顯示資訊與進行操作的界面。我們常常見到和下面類似的界面:

HTML頁面的布局方案

在這個界面中,整個HTML網頁被分為标題區、導航區、内容去、狀态欄區,下面我們分别用Table布局、DIV布局和HTML5建議的布局方案來實作該界面。

1、Table布局方案

使用Table布局方案,将整個浏覽器的展示部分就是一個表格,然後我們設定好表格的單元格合并、大小即可。下面是使用Table布局方案的HTML頁面:

<!DOCTYPE html>
<html>
<head>
<title>layout001</title>
<meta charset="utf-8" />
<style type="text/css">
html,body, table{
width:100%;
height:100%;
}
#first_row{
height:6%;
}
#second_row{
height:91%;
}
#third_row{
height:3%;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
#header{
border:1px black solid;
color:white;
text-align:center;
background:green;
}
#navigator{
border:1px black solid;
color:white;
text-align:center;
background:blue;
}
#content{
border:1px black solid;
color:white;
text-align:center;
background:gray;
}
#footer{
border:1px black solid;
color:white;
text-align:center;
background:orange;
}
</style>
</head>
<body>
<table>
<tr id="first_row">
<td id="header" colspan="2">标題區</td>
</tr>

<tr id="second_row">
<td id="navigator" width="15%">導航區</td>
<td id="content" width="85%">内容區</td>
</tr>

<tr id="third_row">
<td id="footer" colspan="2">狀态欄區</td>
</tr>
</table>
</body>
</html>           

使用浏覽器打開這個HTML文檔,展示效果如下:

HTML頁面的布局方案

這個架構建立後,我們就可以在各個<td>内進行具體的開發了。

2、DIV布局方案

使用DIV布局方案,将整個浏覽器的展示部分由各個DIV來配置設定。下面是使用DIV布局方案的HTML頁面:

<!DOCTYPE html>
<html>
<head>
<title>layout002</title>
<meta charset="utf-8" />
<style type="text/css">
html,body{
width:100%;
height:100%;
}
body,#header,#second_row,#navigator,#content,#footer{
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
#header{
height:6%;
width:100%;
color:white;
text-align:center;
background:green;
}
#second_row{
height:91%;
width:100%;
}
#navigator{
height:100%;
width:15%;
float:left;
color:white;
text-align:center;
background:blue;
}
#content{
height:100%;
width:85%;
float:right;
color:white;
text-align:center;
background:gray;
}
#footer{
height:3%;
width:100%;
color:white;
text-align:center;
background:orange;
}
</style>
</head>
<body>
<div id="header">
标題區
</div>
<div id="second_row">
<div id="navigator">
導航區
</div>
<div id="content">
内容區
</div>
</div>
<div id="footer">
狀态欄區
</div>
</body>
</html>           

使用浏覽器打開這個HTML文檔,展示效果如下:

HTML頁面的布局方案

這個架構建立後,我們就可以在各個<div>内進行具體的開發了。

可以發現,使用DIV元素,我們對頁面進行布局時更友善。

3、HTML5推薦的布局方案

使用DIV布局方案,使用起來特别友善,基本上是前端開發者的首選。不過在HTML5标準來看,各個DIV的含義不明确,是以建議使用專門的元素來替代DIV。這是按照HTML5推薦的布局方案的頁面:

<!DOCTYPE html>
<html>
<head>
<title>layout003</title>
<meta charset="utf-8" />
<style type="text/css">
html,body{
width:100%;
height:100%;
}
body,header,#second_row,nav,main,footer{
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
header{
height:6%;
width:100%;
color:white;
text-align:center;
background:green;
}
#second_row{
height:91%;
width:100%;
}
nav{
height:100%;
width:15%;
float:left;
color:white;
text-align:center;
background:blue;
}
main{
height:100%;
width:85%;
float:right;
color:white;
text-align:center;
background:gray;
}
footer{
height:3%;
width:100%;
color:white;
text-align:center;
background:orange;
}
</style>
</head>
<body>
<header>
标題區
</header>
<div id="second_row">
<nav>
導航區
</nav>
<main>
内容區
</main>
</div>
<footer>
狀态欄區
</footer>
</body>
</html>           

使用浏覽器打開這個HTML文檔,展示效果和上面的一模一樣:

HTML頁面的布局方案

使用這種方案,除了使用了含義明确的<header>、<nav>、<main>、<footer>元素外,和DIV方案沒有差別。

這種做法貌似HTML更清晰,但實際上增加了元素的種類,增加了開發人員的記憶負擔,容易出錯。是以,前端程式員基本上都不喜歡這種方案。