从 HTML4 迁移至 HTML5

本章讲解如何从一张典型的 HTML4 页面迁移至典型的 HTML5。

本章演示如何把一张已有的 HTML4 页面转换为 HTML5 页面,在不破坏如何原始内容和结构的情况下。

注释:您可以使用相同的技巧从 HTML4 以及 XHTML 迁移至 HTML5。

典型的 HTML4 典型的 HTML5
<div id="header"> <header>
<div id="menu"> <nav>
<div id="content"> <section>
<div id="post"> <article>
<div id="footer"> <footer>

典型的 HTML4 页面

实例

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  6. <title>HTML4</title>
  7. <style>
  8. body {font-family:Verdana,sans-serif;font-size:0.8em;}
  9. div#header,div#footer,div#content,div#post
  10. {border:1px solid grey;margin:5px;margin-bottom:15px;padding:8px;background-color:white;}
  11. div#header,div#footer {color:white;background-color:#444;margin-bottom:5px;}
  12. div#content {background-color:#ddd;}
  13. div#menu ul {margin:0;padding:0;}
  14. div#menu ul li {display:inline; margin:5px;}
  15. </style>
  16. </head>
  17. <body>
  18. <div id="header">
  19. <h1>Monday Times</h1>
  20. </div>
  21. <div id="menu">
  22. <ul>
  23. <li>News</li>
  24. <li>Sports</li>
  25. <li>Weather</li>
  26. </ul>
  27. </div>
  28. <div id="content">
  29. <h2>News Section</h2>
  30. <div id="post">
  31. <h2>News Article</h2>
  32. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  33. lurum hurum turum.</p>
  34. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  35. lurum hurum turum.</p>
  36. </div>
  37. <div id="post">
  38. <h2>News Article</h2>
  39. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  40. lurum hurum turum.</p>
  41. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  42. lurum hurum turum.</p>
  43. </div>
  44. </div>
  45. <div id="footer">
  46. <p>&copy; 2014 Monday Times. All rights reserved.</p>
  47. </div>
  48. </body>
  49. </html>

更改为 HTML5 Doctype

修改文档类型,从 HTML4 doctype:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">

修改为 HTML5 doctype:

  1. <!DOCTYPE html>

更改为 HTML5 编码

修改编码信息,从 HTML4:

  1. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

改为 HTML5:

  1. <meta charset="utf-8">

添加 shiv

所有现代浏览器都支持 HTML5 语义元素。

此外,您可以“教授”老式浏览器如何处理“未知元素”。

为 Internet Explorer 支持而添加的 shiv:

  1. <!--[if lt IE 9]>
  2. <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  3. <![endif]-->

注释:请在 HTML5 浏览器支持中阅读更多有关 shiv 的知识。

为 HTML5 语义元素添加 CSS

请看已有的 CSS 样式:

  1. div#header,div#footer,div#content,div#post {
  2. border:1px solid grey;margin:5px;margin-bottom:15px;padding:8px;background-color:white;
  3. }
  4. div#header,div#footer {
  5. color:white;background-color:#444;margin-bottom:5px;
  6. }
  7. div#content {
  8. background-color:#ddd;
  9. }
  10. div#menu ul {
  11. margin:0;padding:0;
  12. }
  13. div#menu ul li {
  14. display:inline; margin:5px;
  15. }
  16. Duplicate with equal CSS styles for HTML5 semantic elements:
  17. header,footer,section,article {
  18. border:1px solid grey;margin:5px;margin-bottom:15px;padding:8px;background-color:white;
  19. }
  20. header,footer {
  21. color:white;background-color:#444;margin-bottom:5px;
  22. }
  23. section {
  24. background-color:#ddd;
  25. }
  26. nav ul {
  27. margin:0;padding:0;
  28. }
  29. nav ul li {
  30. display:inline; margin:5px;
  31. }

更改为 HTML5 <header><footer>

把 id=”header” 和 id=”footer” 的 <div> 元素:

  1. <div id="header">
  2. <h1>Monday Times</h1>
  3. </div>
  4. .
  5. .
  6. .
  7. <div id="footer">
  8. <p>&copy; 2021 CtrlASD. All rights reserved.</p>
  9. </div>

修改为 HTML5 语义元素 <header><footer>

  1. <header>
  2. <h1>Monday Times</h1>
  3. </header>
  4. .
  5. .
  6. .
  7. <footer>
  8. <p>© 2021 CtrlASD. All rights reserved.</p>
  9. </footer>

更改为 HTML5 <nav>

把 id=”menu” 的 <div> 元素:

  1. <div id="menu">
  2. <ul>
  3. <li>News</li>
  4. <li>Sports</a></li>
  5. <li>Weather</li>
  6. </ul>
  7. </div>

修改为 HTML5 语义元素 <nav>

  1. <nav>
  2. <ul>
  3. <li>News</li>
  4. <li>Sports</a></li>
  5. <li>Weather</li>
  6. </ul>
  7. </nav>

更改为 HTML5 <section>

把 id=”content” 的 the <div> 元素:

  1. <div id="content">
  2. .
  3. .
  4. .
  5. </div>

修改为 HTML5 语义元素 <section>

  1. <section>
  2. .
  3. .
  4. .
  5. </section>

更改为 HTML5 <article>

把 class=”post” 的所有 <div> 元素:

  1. <div class="post">
  2. <h2>News Article</h2>
  3. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  4. lurum hurum turum.</p>
  5. </div>

修改为 HTML5 语义元素 <article>

  1. <article>
  2. <h2>News Article</h2>
  3. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  4. lurum hurum turum.</p>
  5. </article>

删除这些“不再需要的”样式:

  1. div#header,div#footer,div#content,div#post {
  2. border:1px solid grey;margin:5px;margin-bottom:15px;padding:8px;background-color:white;
  3. }
  4. div#header,div#footer {
  5. color:white;background-color:#444;margin-bottom:5px;
  6. }
  7. div#content {
  8. background-color:#ddd;
  9. }
  10. div#menu ul {
  11. margin:0;padding:0;
  12. }
  13. div#menu ul li {
  14. display:inline; margin:5px;
  15. }

典型的 HTML5 页面

最后您可以删除 <head> 标签。HTML5 中不再需要它们:

实例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <title>HTML5</title>
  4. <meta charset="utf-8">
  5. <!--[if lt IE 9]>
  6. <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
  7. </script>
  8. <![endif]-->
  9. <style>
  10. body {
  11. font-family:Verdana,sans-serif;font-size:0.8em;
  12. }
  13. header,footer,section,article {
  14. border:1px solid grey;
  15. margin:5px;margin-bottom:15px;padding:8px;
  16. background-color:white;
  17. }
  18. header,footer {
  19. color:white;background-color:#444;margin-bottom:5px;
  20. }
  21. section {
  22. background-color:#ddd;
  23. }
  24. nav ul {
  25. margin:0;padding:0;
  26. }
  27. nav ul li {
  28. display:inline; margin:5px;
  29. }
  30. </style>
  31. <body>
  32. <header>
  33. <h1>Monday Times</h1>
  34. </header>
  35. <nav>
  36. <ul>
  37. <li>News</li>
  38. <li>Sports</li>
  39. <li>Weather</li>
  40. </ul>
  41. </nav>
  42. <section>
  43. <h2>News Section</h2>
  44. <div id="post">
  45. <h2>News Article</h2>
  46. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  47. lurum hurum turum.</p>
  48. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  49. lurum hurum turum.</p>
  50. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  51. lurum hurum turum.</p>
  52. </div>
  53. <div id="post">
  54. <h2>News Article</h2>
  55. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  56. lurum hurum turum.</p>
  57. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  58. lurum hurum turum.</p>
  59. <p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
  60. lurum hurum turum.</p>
  61. </div>
  62. </section>
  63. <footer>
  64. <p>&copy; 2014 Monday Times. All rights reserved.</p>
  65. </footer>
  66. </body>
  67. </html>

<article> <section><div> 之间的差异

在 HTML5 标准中,<article> <section><div> 之间的差异很小,令人困惑。

在 HTML5 标准中,<section> 元素被定位为相关元素的块。

<article>元素被定义为相关元素的完整的自包含块。

<div> 元素被定义为子元素的块。

如何理解呢?

在上面的例子中,我们曾使用 <section> 作为相关 <articles> 的容器。

但是,我们也能够把 <article> 用作文章的容器。

下面是一些不同的例子:

  1. <article> 中的 <article>
  2. <article>
  3. <h2>Famous Cities</h2>
  4. <article>
  5. <h2>London</h2>
  6. <p>London is the capital city of England. It is the most populous city in the United Kingdom,
  7. with a metropolitan area of over 13 million inhabitants.</p>
  8. </article>
  9. <article>
  10. <h2>Paris</h2>
  11. <p>Paris is the capital and most populous city of France.</p>
  12. </article>
  13. <article>
  14. <h2>Tokyo</h2>
  15. <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
  16. and the most populous metropolitan area in the world.</p>
  17. </article>
  18. </article>
  1. <article> 中的 <div>
  2. <article>
  3. <h2>Famous Cities</h2>
  4. <div class="city">
  5. <h2>London</h2>
  6. <p>London is the capital city of England. It is the most populous city in the United Kingdom,
  7. with a metropolitan area of over 13 million inhabitants.</p>
  8. </div>
  9. <div class="city">
  10. <h2>Paris</h2>
  11. <p>Paris is the capital and most populous city of France.</p>
  12. </div>
  13. <div class="city">
  14. <h2>Tokyo</h2>
  15. <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
  16. and the most populous metropolitan area in the world.</p>
  17. </div>
  18. </article>
  1. <article> 中的 <section> 中的 <div>
  2. <article>
  3. <section>
  4. <h2>Famous Cities</h2>
  5. <div class="city">
  6. <h2>London</h2>
  7. <p>London is the capital city of England. It is the most populous city in the United Kingdom,
  8. with a metropolitan area of over 13 million inhabitants.</p>
  9. </div>
  10. <div class="city">
  11. <h2>Paris</h2>
  12. <p>Paris is the capital and most populous city of France.</p>
  13. </div>
  14. <div class="city">
  15. <h2>Tokyo</h2>
  16. <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
  17. and the most populous metropolitan area in the world.</p>
  18. </div>
  19. </section>
  20. <section>
  21. <h2>Famous Countries</h2>
  22. <div class="country">
  23. <h2>England</h2>
  24. <p>London is the capital city of England. It is the most populous city in the United Kingdom,
  25. with a metropolitan area of over 13 million inhabitants.</p>
  26. </div>
  27. <div class="country">
  28. <h2>France</h2>
  29. <p>Paris is the capital and most populous city of France.</p>
  30. </div>
  31. <div class="country">
  32. <h2>Japan</h2>
  33. <p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
  34. and the most populous metropolitan area in the world.</p>
  35. </div>
  36. </section>
  37. </article>