The DOCTYPE declaration for HTML5 is very simple:
<!DOCTYPE html>
The character encoding (charset) declaration is also very simple:
<meta charset="UTF-8">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<body>
Content of the document......
</body>
</html>
The default character encoding in HTML5 is UTF-8. |
The most interesting new elements are:
New semantic elements like <header>, <footer>, <article>, and <section>.
New form controls like number, date, time, calendar, and range.
New graphic elements: <svg> and <canvas>.
New multimedia elements: <audio> and <video>.
In the chapter HTML5 Support, you will learn how to "teach" old browsers to handle HTML5 semantic. |
The most interesting new API's are:
Local storage is a powerful replacement for cookies. |
The following HTML4 elements have been removed from HTML5:
HTML5 is supported in all modern browsers.
In addition, all browsers, old and new, automatically handle unrecognized elements as inline elements.
Because of this, you can "teach" old browsers to handle "unknown" HTML elements.
You can even teach stone age IE6 (Windows XP 2001) how to handle unknown HTML elements. |
HTML5 defines 8 new semantic HTML elements. All these are block level elements.
To secure correct behavior in older browsers, you can set the CSS display property to block:
header, section, footer, aside, nav, main, article, figure {
display: block;
}
You can add any new element to HTML with a browser trick:
This example adds a new element called <myHero> to HTML, and defines a display style for it:
<!DOCTYPE html>
<html>
<head>
<title>Creating an HTML Element</title>
<script>document.createElement("myHero")</script>
<style>
myHero {
display: block;
background-color: #ddd;
padding: 50px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<myHero>My First Hero</myHero>
</body>
</html>
The JavaScript statement document.createElement("myHero") is added, only to satisfy IE.
You could use the solution described above, for all new HTML5 elements, but:
Internet Explorer 8 and earlier, does not allow styling of unknown elements. |
Thankfully, Sjoerd Visscher created the "HTML5 Enabling JavaScript", "the shiv":
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
The code above is a comment, but early versions of IE9 will read it (and understand it).
<!DOCTYPE html>
<html>
<head>
<title>Styling HTML5</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<h1>My First Article</h1>
<article>
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.
</article>
</body>
</html>
The link to the shiv code must be placed in the <head> element, because Internet Explorer needs to know about all new elements before reading them.
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Skeleton</title>
<meta charset="utf-8">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<style>
body {font-family: Verdana, sans-serif; font-size:0.8em;}
header,nav, section,article,footer
{border:1px solid grey; margin:5px; padding:8px;}
nav ul {margin:0; padding:0;}
nav ul li {display:inline; margin:5px;}
</style>
</head>
<body>
<header>
<h1>HTML5 SKeleton</h1>
</header>
<nav>
<ul>
<li><a href="html5_semantic_elements.asp">HTML5 Semantic</a></li>
<li><a href="html5_form_elements.asp">HTML5 Forms</a></li>
<li><a href="html5_canvas.asp">HTML5 Graphics</a></li>
</ul>
</nav>
<section>
<h1>Famous Cities</h1>
<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>
</section>
<footer>
<p>© 2014 W3Schools. All rights reserved.</p>
</footer>
</body>
</html>
Below is a list of the new HTML5 elements, and a description of what they are used for.
HTML5 offers new elements for better document structure:
Tag |
Description |
<article> |
Defines an article in the document |
<aside> |
Defines content aside from the page content |
<bdi> |
Defines a part of text that might be formatted in a different direction from other text |
<details> |
Defines additional details that the user can view or hide |
<dialog> |
Defines a dialog box or window |
<figcaption> |
Defines a caption for a <figure> element |
<figure> |
Defines self-contained content, like illustrations, diagrams, photos, code listings, etc. |
<footer> |
Defines a footer for the document or a section |
<header> |
Defines a header for the document or a section |
<main> |
Defines the main content of a document |
<mark> |
Defines marked or highlighted text |
<menuitem> |
Defines a command/menu item that the user can invoke from a popup menu |
<meter> |
Defines a scalar measurement within a known range (a gauge) |
<nav> |
Defines navigation links in the document |
<progress> |
Defines the progress of a task |
<rp> |
Defines what to show in browsers that do not support ruby annotations |
<rt> |
Defines an explanation/pronunciation of characters (for East Asian typography) |
<ruby> |
Defines a ruby annotation (for East Asian typography) |
<section> |
Defines a section in the document |
<summary> |
Defines a visible heading for a <details> element |
<time> |
Defines a date/time |
<wbr> |
Defines a possible line-break |
Read more about HTML5 Semantics.
Tag |
Description |
<datalist> |
Defines pre-defined options for input controls |
<keygen> |
Defines a key-pair generator field (for forms) |
<output> |
Defines the result of a calculation |
Read more about HTML5 Form Elements.
New Input Types |
New Input Attributes |
|
Read more about HTML5 Input Types.
Read more about HTML5 Input Attributes.
HTML5 allows 4 different syntaxes for attributes.
This example demonstrates the different syntaxes used in an <input> tag:
Type |
Example |
Empty |
<input type="text" value="John Doe" disabled> |
Unquoted |
<input type="text" value=John> |
Double-quoted |
<input type="text" value="John Doe"> |
Single-quoted |
<input type="text" value='John Doe'> |
In HTML5, all 4 syntaxes may be used, depending on what is needed for the attribute.
Tag |
Description |
<canvas> |
Defines graphic drawing using JavaScript |
<svg> |
Defines graphic drawing using SVG |
Read more about HTML5 Canvas.
Read more about HTML5 SVG.
Tag |
Description |
<audio> |
Defines sound or music content |
<embed> |
Defines containers for external applications (like plug-ins) |
<source> |
Defines sources for <video> and <audio> |
<track> |
Defines tracks for <video> and <audio> |
<video> |
Defines video or movie content |
Semantics means (from Ancient Greek), is the study of meaning.
Semantic elements are elements with a meaning.
A semantic element clearly describes its meaning to both the browser and the developer.
Examples of non-semantic elements: <div> and <span> - Tells nothing about its content.
Examples of semantic elements: <form>, <table>, and <img> - Clearly defines its content.
HTML5 semantic elements are supported in all modern browsers.
In addition, you can "teach" older browsers how to handle "unknown elements".
Read about it in HTML5 Browser Support.
Many web sites contain HTML code like: <div id="nav"> <div class="header"> <div id="footer">
to indicate navigation, header, and footer.
HTML5 offers new semantic elements to define different parts of a web page:
The <section> element defines a section in a document.
According to W3C's HTML5 documentation: "A section is a thematic grouping of content, typically with a heading."
A Web site's home page could be split into sections for introduction, content, and contact information.
<section>
<h1>WWF</h1>
<p>The World Wide Fund for Nature (WWF) is....</p>
</section>
The <article> element specifies independent, self-contained content.
An article should make sense on its own, and it should be possible to read it independently from the rest of the web site.
Examples of where an <article> element can be used:
<article>
<h1>Internet Explorer 9</h1>
<p>Windows Internet Explorer 9 (abbreviated as IE9) was released to
the public on March 14, 2011 at 21:00 PDT.</p>
</article>
In the HTML5 standard, the <article> element defines a complete, self-contained block of related elements.
The <section> element is defined as a block of related elements.
Can we use the definitions to decide how to nest elements? No, we cannot!
On the Internet, you will find HTML pages with <section> elements containing <article> elements, and <article> elements containing <sections> elements.
You will also find pages with <section> elements containing <section> elements, and <article> elements containing <article> elements.
Newspaper: The sports articles in the sports section, have a technical section in each article. |
The <header> element specifies a header for a document or section.
The <header> element should be used as a container for introductory content.
You can have several <header> elements in one document.
The following example defines a header for an article:
<article>
<header>
<h1>Internet Explorer 9</h1>
<p><time pubdate datetime="2011-03-15"></time></p>
</header>
<p>Windows Internet Explorer 9 (abbreviated as IE9) was released to
the public on March 14, 2011 at 21:00 PDT.</p>
</article>
The <footer> element specifies a footer for a document or section.
A <footer> element should contain information about its containing element.
A footer typically contains the author of the document, copyright information, links to terms of use, contact information, etc.
You can have several <footer> elements in one document.
<footer>
<p>Posted by: Hege Refsnes</p>
<p><time pubdate datetime="2012-03-01"></time></p>
</footer>
The <nav> element defines a set of navigation links.
The <nav> element is intended for large blocks of navigation links. However, not all links in a document should be inside a <nav> element!
<nav>
<a href="/html/">HTML</a> |
<a href="/css/">CSS</a> |
<a href="/js/">JavaScript</a> |
<a href="/jquery/">jQuery</a>
</nav>
The <aside> element defines some content aside from the content it is placed in (like a sidebar).
The aside content should be related to the surrounding content.
<p>My family and I visited The Epcot center this summer.</p>
<aside>
<h4>Epcot Center</h4>
<p>The Epcot Center is a theme park in Disney World, Florida.</p>
</aside>
In books and newspapers, it is common to have captions with images.
The purpose of a caption is to add a visual explanation to an image.
With HTML5, images and captions can be grouped together in <figure> elements:
<figure>
<img src="pic_mountain.jpg" alt="The Pulpit Rock" width="304" height="228">
<figcaption>Fig1. - The Pulpit Pock, Norway.</figcaption>
</figure>
The <img> element defines the image, the <figcaption> element defines the caption.
With HTML4, developers used their own favorite attribute names to style page elements:
header, top, bottom, footer, menu, navigation, main, container, content, article, sidebar, topnav, ...
This made it impossible for search engines to identify the correct web page content.
With HTML5 elements like: <header> <footer> <nav> <section> <article>, this will become easier.
According to the W3C, a Semantic Web:
"Allows data to be shared and reused across applications, enterprises, and communities."
Below is an alphabetical list of the new semantic elements in HTML5.
The links goes to our complete HTML5 Reference.
Tag |
Description |
Defines an article |
|
Defines content aside from the page content |
|
Defines additional details that the user can view or hide |
|
Defines a caption for a <figure> element |
|
Specifies self-contained content, like illustrations, diagrams, photos, code listings, etc. |
|
Defines a footer for a document or section |
|
Specifies a header for a document or section |
|
Specifies the main content of a document |
|
Defines marked/highlighted text |
|
Defines navigation links |
|
Defines a section in a document |
|
Defines a visible heading for a <details> element |
|
Defines a date/time |
This chapter is entirely about how to migrate from a typical HTML4 page to a typical HTML5 page.
This chapter demonstrates how to convert an existing HTML4 page into an HTML5 page, without destroying anything of the original content or structure.
You can migrate to HTML5 from HTML4, and XHTML, using the same recipe. |
Typical HTML4 |
Typical HTML5 |
<div id="header"> |
<header> |
<div id="menu"> |
<nav> |
<div id="content"> |
<section><article> |
<div id="footer"> |
<footer> |
<!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.8em;}
div#header, div#menu, div#content, div.city, div#footer
{border:1px solid grey; margin:5px; padding:8px;}
div#menu ul {margin:0; padding:0;}
div#menu ul li {display:inline; margin:5px;}
</style>
</head>
<body>
<div id="header">
<h1>HTML4 Skeleton</h1>
</div>
<div id="menu">
<ul>
<li><a href="html5_semantic_elements.asp">HTML5 Semantic</a></li>
<li><a href="html5_form_elements.asp">HTML5 Forms</a></li>
<li><a href="html5_canvas.asp">HTML5 Graphics</a></li>
</ul>
</div>
<div id="content">
<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>
</div>
<div id="footer">
<p>© 2014 W3Schools. All rights reserved.</p>
</div>
</body>
</html>
Change the doctype, from the HTML4 doctype:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
to the HTML5 doctype:
<!DOCTYPE html>
Change the encoding information, from HTML4:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
to HTML5:
<meta charset="utf-8">
HTML5 semantic elements are supported in all modern browsers.
In addition, you can "teach" older browsers how to handle "unknown elements".
Add the shiv for Internet Explorer support:
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
Read about the shiv in HTML5 Browser Support. |
Look at your existing CSS styles:
div#header, div#menu, div#content, div.article, div#footer
{border:1px solid grey; margin:5px; padding:8px;}
div#menu ul {margin:0; padding:0;}
div#menu ul li {display:inline; margin:5px;}
Duplicate with equal CSS styles for HTML5 semantic elements:
header, nav, section, article, footer
{border:1px solid grey; margin:5px; padding:8px;}
nav ul {margin:0; padding:0;}
nav ul li {display:inline; margin:5px;}
Change the <h1> element, from HTML4 Skeleton to HTML5 Skeleton:
<h1>HTML5 Skeleton</h1>
Change the <div> elements with id="header" and id="footer":
<div id="header">
<h1>HTML4 Skeleton</h1>
</div>
.
.
.
<div id="footer">
<p>© 2014 W3Schools. All rights reserved.</p>
</div>
to HTML5 semantic <header> and <footer> elements:
<header>
<h1>HTML5 Skeleton</h1>
</header>
.
.
.
<footer>
<p>© 2014 W3Schools. All rights reserved.</p>
</footer>
Change the <div> element with id="menu":
<div id="menu">
<ul>
<li><a href="html5_semantic_elements.asp">HTML5 Semantic</a></li>
<li><a href="html5_form_elements.asp">HTML5 Forms</a></li>
<li><a href="html5_canvas.asp">HTML5 Graphics</a></li>
</ul>
</div>
to an HTML5 semantic <nav> element:
<nav>
<ul>
<li><a href="html5_semantic_elements.asp">HTML5 Semantic</a></li>
<li><a href="html5_form_elements.asp">HTML5 Forms</a></li>
<li><a href="html5_canvas.asp">HTML5 Graphics</a></li>
</ul>
</nav>
Change the <div> element with id="content":
<div id="content">
.
.
.
</div>
to an HTML5 semantic <section> element:
<section>
.
.
.
</section>
Change all <div> element with class="city":
<div class="city">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>
to HTML5 semantic <article> elements:
<article>
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</article>
In this example it makes no difference, but you can keep the class="city" attribute for later styling.
Remove these "no longer needed" <style> elements:
div#header, div#menu, div#content, div.city, div#footer
{border:1px solid grey; margin:5px; padding:8px;}
div#menu ul {margin:0; padding:0;}
div#menu ul li {display:inline; margin:5px;}
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5</title>
<meta charset="utf-8">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
body {font-family: Verdana, sans-serif; font-size:0.8em;}
header, nav, section, article, footer
{border:1px solid grey; margin:5px; padding:8px;}
nav ul {margin:0; padding:0;}
nav ul li {display:inline; margin:5px;}
</style>
</head>
<body>
<header>
<h1>HTML5 Skeleton</h1>
</header>
<nav>
<ul>
<li><a href="html5_semantic_elements.asp">HTML5 Semantic</a></li>
<li><a href="html5_form_elements.asp">HTML5 Forms</a></li>
<li><a href="html5_canvas.asp">HTML5 Graphics</a></li>
</ul>
</nav>
<section>
<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>
</section>
<footer>
<p>© 2014 W3Schools. All rights reserved.</p>
</footer>
</body>
</html>
There is a confusing (lack of) difference in the HTML5 standard, between <article> <section> and <div>.
In the HTML5 standard, the <section> element is defined as a block of related elements.
The <article> element is defined as a complete, self-contained block of related elements.
The <div> element is defined as a block of children elements.
How to interpret that?
In the example above, we have used <section> as a container for related <articles>.
But, because "Famous Cities" is a complete, self-contained block of related elements, we could have used <article> as a container as well:
<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>
We could also have used <div> for each city, because each <div> is a block of children elements:
<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>
Or maybe a <section> for cities, and another <section> for countries:
<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 Counties</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>
Many developers are uncertain about the syntax style to use in HTML5.
Between 2000 and 2010, most web developers converted from HTML to XHTML.
XHTML syntax was easy. Developers where forced write valid and "well-formed" code.
HTML5 is a bit more sloppy when it comes to code validation.
With HTML5, you have to create your own best practice syntax style.
Always keep your style smart, tidy, clean, and well-formed. |
A consequent use of style, makes it easier for others to understand and use your HTML.
In the future, programs like XML readers, may want to read your HTML.
Using a well-formed "close to XHTML" syntax, can be very smart.
You can never be sure that different styles will produce the same result:
objects = getElementsByTagName("DIV")
objects = getElementsByTagName("div")
HTML5 allows mixing uppercase and lowercase letters in element names.
At W3Schools, we feel it's natural to use lowercase element names:
Looking bad:
<SECTION>
<p>This is a paragraph.</p>
</SECTION>
Looking very bad:
<Section>
<p>This is a paragraph.</p>
</SECTION>
Looking good:
<section>
<p>This is a paragraph.</p>
</section>
HTML5 allows unclosed HTML elements.
However, at W3Schools, we feel it more natural to always close HTML elements:
Looking bad:
<section>
<p>This is a paragraph.
<p>This is a paragraph.
</section>
Looking good:
<section>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</section>
HTML5 allows unclosed empty HTML elements.
This is allowed:
<meta charset="utf-8">
This is also allowed:
<meta charset="utf-8" />
The slash (/) closes the element from an XML readers point of view. It might be a good idea to keep it.
HTML5 allows mixing uppercase and lowercase letters in attribute names.
At W3Schools, we feel it's natural to use lowercase attribute names:
Looking bad:
<div CLASS="table table-striped table-bordered">
Looking good:
<div class="table table-striped table-bordered">
HTML5 allows unquoted attribute values. It saves space, and is easier to write.
However, at W3Schools, we feel it more natural to always quote attribute values:
This will not work:
<div class=table table-striped table-bordered>
This will work:
<div class="table table-striped table-bordered">