BEST SITE FOR WEB DEVELOPERS
HTML5 Lessons for beginners

Ua Es De Fr Ru

HTML5 Migrating from HTML4


Transition from HTML4 to HTML5

This section is entirely about how to migrate from HTML4 to HTML5.

This section shows how to convert an HTML4 page into an HTML5 page without breaking any of the original content or page structure.

You can also migrate from XHTML to HTML5 using the same method.

Typical HTML4 Typical HTML5
<div id="header"> <header>
<div id="menu"> <nav>
<div id="content"> <section>
<div class="article"> <article>
<div id="footer"> <footer>

Typical HTML4 Page

Example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>HTML4</title>
<style>
body {
  font-family: Verdana,sans-serif;
  font-size: 0.9em;
}

div#header, div#footer {
  padding: 10px;
  color: white;
  background-color: black;
}

div#content {
  margin: 5px;
  padding: 10px;
  background-color: lightgrey;
}

div.article {
  margin: 5px;
  padding: 10px;
  background-color: white;
}

div#menu ul {
  padding: 0;
}

div#menu ul li {
  display: inline;
  margin: 5px;
}
</style>
</head>
<body>

<div id="header">
  <h1>Monday Times</h1>
</div>

<div id="menu">
  <ul>
   <li>News</li>
    <li>Sports</li>
   <li>Weather</li>
  </ul>
</div>

<div id="content">
 <h2>News Section</h2>
  <div class="article">
    <h2>News Article</h2>
   <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat at.</p>
  </div>
  <div class="article">
    <h2>News Article</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat at.</p>
  </div>
</div>

<div id="footer">
 <p>&amp;copy; 2024 Monday Times. All rights reserved.</p>
</div>

</body>
</html>
Try it Yourself »

Change to HTML5 Doctype

Change the doctype:

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

to the HTML5 doctype:

Example

<!DOCTYPE html>
Try it Yourself »

Switch to HTML5 Encoding

Change the encoding information:

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

to HTML5 encoding:

Example

<meta charset="utf-8">
Try it Yourself »

Add HTML5Shiv

New HTML5 semantic elements are supported in all modern browsers. In addition, you can “teach” older browsers to work with “unknown elements”.

However, IE8 and earlier versions do not allow styling of unknown elements. Therefore, HTML5Shiv is a JavaScript workaround that enables styling of HTML5 elements in Internet Explorer versions prior to version 9.

Add HTML5Shiv:

Example

<!--[if lt IE 9]>
 <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<![endif]-->
Try it Yourself »

Learn more about HTML5Shiv in the HTML5 Browser Support section.


Switch to HTML5 Semantic Elements

The existing CSS contains IDs and classes used to style elements:

body {
  font-family: Verdana,sans-serif;
  font-size: 0.9em;
}

div#header, div#footer {
  padding: 10px;
  color: white;
  background-color: black;
}

div#content {
  margin: 5px;
  padding: 10px;
  background-color: lightgrey;
}

div.article {
  margin: 5px;
  padding: 10px;
  background-color: white;
}

div#menu ul {
  padding: 0;
}

div#menu ul li {
  display: inline;
  margin: 5px;
}

Replace them with equivalent CSS styles for HTML5 semantic elements:

body {
  font-family: Verdana,sans-serif;
  font-size: 0.9em;
}

header, footer {
  padding: 10px;
  color: white;
  background-color: black;
}

section {
  margin: 5px;
  padding: 10px;
  background-color: lightgrey;
}

article {
  margin: 5px;
  padding: 10px;
  background-color: white;
}

nav ul {
  padding: 0;
}

nav ul li {
  display: inline;
  margin: 5px;
}

Finally, replace the elements with HTML5 semantic elements:

Examples

<body>

<header>
  <h1>Monday Times</h1>
</header>

<nav>
  <ul>
    <li>News</li>
    <li>Sports</li>
    <li>Weather</li>
  </ul>
</nav>

<section>
  <h2>News Section</h2>
  <article>
    <h2>News Article</h2>
    <p>Lorem ipsum dolor sit amet..</p>
  </article>
  <article>
    <h2>News Article</h2>
   <p>Lorem ipsum dolor sit amet..</p>
  </article>
</section>

<footer>
  <p>&copy; 2014 Monday Times. All rights reserved.</p>
</footer>

</body>
Try it Yourself »

The Difference Between <article>, <section> and <div>

In the HTML5 standard, there is no strict difference between <article>, <section>, and <div>.

In the HTML5 standard, the <section> element is defined as a block of related content.

The <article> element is defined as a complete, self‑contained block of related content.

The <div> element is defined as a block of child elements.

How to interpret this?

In the example above, we used <section> as a container for related <article> elements.

But we could also have used <article> as the container for the articles.

Here are several different examples:

<article> inside <article>:

<article>

<h2>Famous Cities</h2>

<article>
  <h2>London</h2>
  <p>London is the capital city of England.</p>
</article>

<article>
  <h2>Paris</h2>
  <p>Paris is the capital and most populous city of France.</p>
</article>

<article>
  <h2>Tokyo</h2>
  <p>Tokyo is the capital of Japan.</p>
</article>

</article>
Try it Yourself »

<div> inside <article>:

<article>

<h2>Famous Cities</h2>

<div class="city">
  <h2>London</h2>
  <p>London is the capital city of England.</p>
</div>

<div class="city">
  <h2>Paris</h2>
  <p>Paris is the capital and most populous city of France.</p>
</div>

<div class="city">
  <h2>Tokyo</h2>
  <p>Tokyo is the capital of Japan.</p>
</div>

</article>
Try it Yourself »

<div> inside <section> inside <article>:

<article>

<section>
  <h2>Famous Cities</h2>

  <div class="city">
   <h2>London</h2>
   <p>London is the capital city of England.</p>
  </div>

  <div class="city">
   <h2>Paris</h2>
   <p>Paris is the capital and most populous city of France.</p>
  </div>

  <div class="city">
   <h2>Tokyo</h2>
   <p>Tokyo is the capital of Japan.</p>
  </div>
</section>

<section>
  <h2>Famous Countries</h2>

  <div class="country">
   <h2>England</h2>
   <p>London is the capital city of England.</p>
</div>

  <div class="country">
   <h2>France</h2>
   <p>Paris is the capital and most populous city of France.</p>
</div>

  <div class="country">
   <h2>Japan</h2>
   <p>Tokyo is the capital of Japan.</p>
  </div>
</section>

</article>
Try it Yourself »