Migration from HTML4 to HTML5
This chapter is covering the entire procedure about how to migrate from HTML4 page to HTML5 page.
This chapter will demonstrate the convertion of an HTML4 page into an HTML5 page, without changing or altering anything of the original content or structure.
A Typical HTML4 Page
Example
<!DOCTYPE HTML PUBLIC "-//NGO//DTD HTML 4.01 Transitional//EN"
"http://www.ngo.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.8em;}
div#header,div#footer,div#content,div#post
{border:1px solid
grey;margin:5px;margin-bottom:15px;padding:8px;background-color:white;}
div#header,div#footer {color:white;background-color:#444;margin-bottom:5px;}
div#content {background-color:#ddd;}
div#menu ul
{margin:0;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 id="post">
<h2>News Article</h2>
<p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum
ipsum
lurum hurum turum.</p>
<p>Ipsum lurum hurum turum
ipsum lurum hurum turum ipsum lurum hurum turum ipsum
lurum hurum
turum.</p>
</div>
<div id="post">
<h2>News
Article</h2>
<p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum
lurum hurum turum ipsum
lurum hurum turum.</p>
<p>Ipsum
lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
lurum hurum turum.</p>
</div>
</div>
<div id="footer">
<p>&copy; 2014 Monday Times. All rights reserved.</p>
</div>
</body>
</html>
Try it Yourself
Change to HTML5 Doctype
Change the doctype, from the HTML4 doctype:
<!DOCTYPE HTML PUBLIC "-//NGO//DTD HTML 4.01 Transitional//EN"
"http://www.ngo.org/TR/html4/loose.dtd">
to the HTML5 doctype:
Change to HTML5 Encoding
Changing the encoded information, from HTML4:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
to HTML5:
Add The HTML Shiv
HTML5 semantic elements are getting supported by all modern browsers.
Additionally, you can also "teach" older browsers how to handle "unknown elements".
Add the shiv for Internet Explorer support:
Example
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
Try it Yourself
Read more about the shiv in HTML5 Browser Support.
Change to HTML5 Semantic Elements
The existing CSS contains id's and classes inorder to style the 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;
}
Duplicating using equal 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, changing the elements to HTML5 semantic elements:
Example
<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, consectetur adipiscing elit. Pellentesque in
porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat
at.</p>
</article>
<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>
</article>
</section>
<footer>
<p>© 2014 Monday Times. All rights reserved.</p>
</footer>
</body>
Try it Yourself
The Difference Between <article> <section> and <div>
There is a difference which is confusing (lack of) in the HTML5 standard, between <article> <section> and <div>.
In the HTML5 standard, the <section> element is found to be defined as a block of related elements.
The <article> element is found to be defined as a complete, self-contained block of related elements.
The <div> element is found to be defined as a block of children elements.
How can we interpret that?
In the example given above, we are using <section> as a container for related <articles>.
But, we also could have used <article> as a container for articles as well.
Here are few other different examples:
<article> in <article>:
<article>
<h2>Famous
Cities</h2>
<article>
<h2>London</h2>
<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>
</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, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</article>
</article>
Try it Yourself
<div> in <article>:
<article>
<h2>Famous
Cities</h2>
<div class="city">
<h2>London</h2>
<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>
</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, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</div>
</article>
Try it Yourself
<div> in <section> in <article>:
<article>
<section>
<h2>Famous Cities</h2>
<div
class="city">
<h2>London</h2>
<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>
</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, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</div>
</section>
<section>
<h2>Famous Countries</h2>
<div
class="country">
<h2>England</h2>
<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>
</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, the center of the
Greater Tokyo Area,
and the most populous metropolitan area in the
world.</p>
</div>
</section>
</article>
Try it Yourself