使用(yòng) <div> 元素的 HTML 布局
注釋:<div> 元素常用(yòng)作(zuò)布局工(gōng)具,因爲能(néng)夠輕松地通過 CSS 對(duì)其進行定位。
這(zhè)個例子使用(yòng)了(le)四個 <div> 元素來(lái)創建多列布局:
實例
<body>
<div id="header">
<h1>City Gallery</h1>
</div>
<div id="nav">
London<br>
Paris<br>
Tokyo<br>
</div>
<div id="section">
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</div>
<div id="footer">
Copyright W3School.com.cn
</div>
</body>
CSS:
<style>
#header {
background-color:black;
color:white;
text-align:center;
padding:5px;
}
#nav {
line-height:30px;
background-color:#eeeeee;
height:300px;
width:100px;
float:left;
padding:5px;
}
#section {
width:350px;
float:left;
padding:10px;
}
#footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
}
</style>
使用(yòng) HTML5 的網站(zhàn)布局
HTML5 提供的新語義元素定義了(le)網頁的不同部分:
HTML5 語義元素
header | 定義文(wén)檔或節的頁眉 |
nav | 定義導航鏈接的容器 |
section | 定義文(wén)檔中的節 |
article | 定義獨立的自(zì)包含文(wén)章 |
aside | 定義内容之外(wài)的内容(比如側欄) |
footer | 定義文(wén)檔或節的頁腳 |
details | 定義額外(wài)的細節 |
summary | 定義 details 元素的标題 |
這(zhè)個例子使用(yòng) <header>, <nav>, <section>, 以及 <footer> 來(lái)創建多列布局:
實例
<body>
<header>
<h1>City Gallery</h1>
</header>
<nav>
London<br>
Paris<br>
Tokyo<br>
</nav>
<section>
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</section>
<footer>
Copyright W3School.com.cn
</footer>
</body>
CSS
header {
background-color:black;
color:white;
text-align:center;
padding:5px;
}
nav {
line-height:30px;
background-color:#eeeeee;
height:300px;
width:100px;
float:left;
padding:5px;
}
section {
width:350px;
float:left;
padding:10px;
}
footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
}
使用(yòng)表格的 HTML 布局
注釋:<table> 元素不是作(zuò)爲布局工(gōng)具而設計(jì)的。
<table> 元素的作(zuò)用(yòng)是顯示表格化的數據。
使用(yòng) <table> 元素能(néng)夠取得布局效果,因爲能(néng)夠通過 CSS 設置表格元素的樣式:
實例
<body>
<table class="lamp">
<tr>
<th>
<img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px">
</th>
<td>
The table element was not designed to be a layout tool.
</td>
</tr>
</table>
</body>
CSS
<style>
table.lamp {
width:100%;
border:1px solid #d4d4d4;
}
table.lamp th, td {
padding:10px;
}
table.lamp td {
width:40px;
}
</style>
網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發