您现在的位置: 365建站网 > 建站教程 > html教程 > HTML 布局

HTML 布局

此节有 370 人学习过     参与评论

网站常常以多列显示内容(就像杂志和报纸)。

使用 <div> 元素的 HTML 布局

注释:<div> 元素常用作布局工具,因为能够轻松地通过 CSS 对其进行定位。

这个例子使用了四个 <div> 元素来创建多列布局:

实例

</>code

  1. <body>
  2. <div id="header">
  3. <h1>City Gallery</h1>
  4. </div>
  5. <div id="nav">
  6. London<br>
  7. Paris<br>
  8. Tokyo<br>
  9. </div>
  10. <div id="section">
  11. <h1>London</h1>
  12. <p>
  13. London is the capital city of England. It is the most populous city in the United Kingdom,
  14. with a metropolitan area of over 13 million inhabitants.
  15. </p>
  16. <p>
  17. Standing on the River Thames, London has been a major settlement for two millennia,
  18. its history going back to its founding by the Romans, who named it Londinium.
  19. </p>
  20. </div>
  21. <div id="footer">
  22. Copyright 365jz.com.cn
  23. </div>
  24. </body>

亲自试一试

CSS:

</>code

  1. <style>
  2. #header {
  3. background-color:black;
  4. color:white;
  5. text-align:center;
  6. padding:5px;
  7. }
  8. #nav {
  9. line-height:30px;
  10. background-color:#eeeeee;
  11. height:300px;
  12. width:100px;
  13. float:left;
  14. padding:5px;
  15. }
  16. #section {
  17. width:350px;
  18. float:left;
  19. padding:10px;
  20. }
  21. #footer {
  22. background-color:black;
  23. color:white;
  24. clear:both;
  25. text-align:center;
  26. padding:5px;
  27. }
  28. </style>

使用 HTML5 的网站布局

HTML5 提供的新语义元素定义了网页的不同部分:

HTML5 语义元素

header 定义文档或节的页眉
nav 定义导航链接的容器
section 定义文档中的节
article 定义独立的自包含文章
aside 定义内容之外的内容(比如侧栏)
footer 定义文档或节的页脚
details 定义额外的细节
summary 定义 details 元素的标题

这个例子使用 <header>, <nav>, <section>, 以及 <footer> 来创建多列布局:

实例

</>code

  1. <body>
  2. <header>
  3. <h1>City Gallery</h1>
  4. </header>
  5. <nav>
  6. London<br>
  7. Paris<br>
  8. Tokyo<br>
  9. </nav>
  10. <section>
  11. <h1>London</h1>
  12. <p>
  13. London is the capital city of England. It is the most populous city in the United Kingdom,
  14. with a metropolitan area of over 13 million inhabitants.
  15. </p>
  16. <p>
  17. Standing on the River Thames, London has been a major settlement for two millennia,
  18. its history going back to its founding by the Romans, who named it Londinium.
  19. </p>
  20. </section>
  21. <footer>
  22. Copyright 365jz.com.cn
  23. </footer>
  24. </body>

亲自试一试

CSS

</>code

  1. <style>
  2. header {
  3. background-color:black;
  4. color:white;
  5. text-align:center;
  6. padding:5px;
  7. }
  8. nav {
  9. line-height:30px;
  10. background-color:#eeeeee;
  11. height:300px;
  12. width:100px;
  13. float:left;
  14. padding:5px;
  15. }
  16. section {
  17. width:350px;
  18. float:left;
  19. padding:10px;
  20. }
  21. footer {
  22. background-color:black;
  23. color:white;
  24. clear:both;
  25. text-align:center;
  26. padding:5px;
  27. }

使用表格的 HTML 布局

注释:<table> 元素不是作为布局工具而设计的。

<table> 元素的作用是显示表格化的数据。

使用 <table> 元素能够取得布局效果,因为能够通过 CSS 设置表格元素的样式:

实例

</>code

  1. <body>
  2. <table class="lamp">
  3. <tr>
  4. <th>
  5. <img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px">
  6. </th>
  7. <td>
  8. The table element was not designed to be a layout tool.
  9. </td>
  10. </tr>
  11. </table>
  12. </body>

亲自试一试

CSS

</>code

  1. <style>
  2. table.lamp {
  3. width:100%;
  4. border:1px solid #d4d4d4;
  5. }
  6. table.lamp th, td {
  7. padding:10px;
  8. }
  9. table.lamp td {
  10. width:40px;
  11. }
  12. </style>

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (370人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------
html教程目录

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 建站教程 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号