On this page, we'll show how you can migrate from HTML4 to HTML5. Let's learn it step by step.
In HTML4, we used id and class attributes which are now replaced with new elements in HTML5:<div id="header"> - <header>
<div id="menu"> - <nav>
<div id="content"> - <section>
<div class="article"> - <article>
<div id="footer"> - <footer>
Step 1: Changing the Doctype
We change the HTML4 doctype to HTML5 doctype.
HTML4
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
HTML5
<!DOCTYPE html>
Step 2: Changing the Encoding
Here, we change the HTML4 encoding information to HTML5 encoding.
HTML4
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
HTML5
<meta charset="utf-8">
Step 3: Adding the HTML5Shiv
All modern browsers support the new HTML5 semantic elements. Moreover, you can "teach" older browsers handling "unknown elements". The HTML5Shiv is used to enable styling of HTML5 elements in such browsers.
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<![endif]-->
Step 4: Changing to HTML5 Semantic Elements
Fisrt, see an example where HTML4 semantic elements are used.
Example of HTML4 elements:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<meta charset="utf-8">
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<style>
body {
font-size: 0.9em;
}
#header,
#footer {
padding: 10px;
color: white;
background-color: black;
text-align: center;
}
h2 {
text-align: center
}
h3 {
text-align: right;
padding-right: 20px;
}
div.content {
margin: 5px;
padding: 20px;
background-color: lightgrey;
}
.article {
margin: 20px;
padding: 10px;
background-color: white;
}
#header-menu ul {
padding: 0;
}
#header-menu ul li {
display: inline;
margin: 5px;
}
</style>
</head>
<body>
<div id="header">
<h1>LaravelSoft</h1>
</div>
<div id="header-menu">
<ul>
<li>
<a href="https://www.w3docs.com/learn-html.html">Books</a>
</li>
<li>
<a href="https://www.w3docs.com/quiz/">Quizzes</a>
</li>
<li>
<a href="https://www.w3docs.com/snippets">Snippets</a>
</li>
<li>
<a href="https://www.w3docs.com/tool/">Tools</a>
</li>
<li>
<a href="https://www.w3docs.com/string-functions/">String Functions</a>
</li>
</ul>
</div>
<div class="content">
<h2>Article Section</h2>
<div class="article">
<h3>Article Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat at. 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">
<h3>News Article</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat at.</p>
<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>© 2020 All rights reserved.</p>
</div>
</body>
</html>
Now, see the migration from HTML4 elements to HTML5 semantic elements.
Example of HTML5 semantic elements:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<meta charset="utf-8">
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<style>
body {
font-size: 0.9em;
}
header,
footer {
padding: 10px;
color: white;
background-color: black;
text-align: center;
}
h2 {
text-align: center
}
h3 {
text-align: right;
padding-right: 20px;
}
section {
margin: 5px;
padding: 20px;
background-color: lightgrey;
}
article {
margin: 20px;
padding: 10px;
background-color: white;
}
nav ul {
padding: 0;
}
nav ul li {
display: inline;
margin: 5px;
}
</style>
</head>
<body>
<header>
<h1>LaravelSoft</h1>
</header>
<nav>
<ul>
<li>
<a href="https://www.w3docs.com/learn-html.html">Books</a>
</li>
<li>
<a href="https://www.w3docs.com/quiz/">Quizzes</a>
</li>
<li>
<a href="https://www.w3docs.com/snippets">Snippets</a>
</li>
<li>
<a href="https://www.w3docs.com/tool/">Tools</a>
</li>
<li>
<a href="https://www.w3docs.com/string-functions/">String Functions</a>
</li>
</ul>
</nav>
<section>
<h2>Article Section</h2>
<article>
<h3>Article Title</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat at. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat at.
</p>
</article>
<article>
<h3>News Article</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque in porta lorem. Morbi condimentum est nibh, et consectetur tortor feugiat at.
</p>
<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>© 2020 All rights reserved.</p>
</footer>
</body>
</html>
The Difference Between <article>,<section> and <div> Elements
In HTML5, there are some differences between the <section>, <article>, and <div> elements. Particularly:
- The <section> element is specified as a block of related elements.
- The <article> element is specified as an independent and complete block of related elements.
- The <div> element is specified as a block of children elements.
Example of the <section>, <article>, and <div> tags:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<section>
<h1>Articles about flowers</h1>
<article>
<h2>Roses</h2>
<p>
Rose – the queen of flowers - is the object of worship and ardent love. Since time immemorial, the rose has been the object of worship and admiration.
</p>
</article>
<div>
<h2>Lilies</h2>
<p>
Lily - an amazing beauty flower, one of the most ancient among a variety of bulbous plants.
</p>
</div>
</section>
</body>
</html>
Practice Your Knowledge
Quiz Time: Test Your Skills!
Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.