HTML 5
HTML 5
Example
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
What is HTML5?
HTML5 will be the new standard for HTML.
The previous version of HTML, HTML 4.01, came in 1999. The web has changed a lot since
then.
HTML5 is still a work in progress. However, the major browsers support many of the new
HTML5 elements and APIs.
WHATWG was working with web forms and applications, and W3C was working with
XHTML 2.0. In 2006, they decided to cooperate and create a new version of HTML.
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>
But all major browsers (Safari, Chrome, Firefox, Opera, Internet Explorer) continue to add
new HTML5 features to their latest versions.
Today, some elements in HTML 4.01 are obsolete, never used, or not used the way they were
intended to. These elements are removed or re-written in HTML5.
To better handle today's internet use, HTML5 includes new elements for better structure,
better form handling, drawing, and for media content.
Tag Description
<article> Defines an article
<aside> Defines content aside from the page content
Isolates a part of text that might be formatted in a different direction from
<bdi>
other text outside it
<command> Defines a command button that a user can invoke
<details> Defines additional details that the user can view or hide
<summary> Defines a visible heading for a <details> element
Specifies self-contained content, like illustrations, diagrams, photos, code
<figure>
listings, etc.
<figcaption> Defines a caption for a <figure> element
<footer> Defines a footer for a document or section
<header> Defines a header for a document or section
<hgroup> Groups a set of <h1> to <h6> elements when a heading has multiple levels
<mark> Defines marked/highlighted text
<meter> Defines a scalar measurement within a known range (a gauge)
<nav> Defines navigation links
<progress> Represents the progress of a task
<ruby> Defines a ruby annotation (for East Asian typography)
Defines an explanation/pronunciation of characters (for East Asian
<rt>
typography)
<rp> Defines what to show in browsers that do not support ruby annotations
<section> Defines a section in a document
<time> Defines a date/time
<wbr> Defines a possible line-break
Tag Description
<audio> Defines sound content
<video> Defines a video or movie
<source> Defines multiple media resources for <video> and <audio>
Defines a container for an external application or interactive content (a plug-
<embed>
in)
<track> Defines text tracks for <video> and <audio>
Tag Description
<datalist> Specifies a list of pre-defined options for input controls
<keygen> Defines a key-pair generator field (for forms)
<output> Defines the result of a calculation
Removed Elements
The following HTML 4.01 elements are removed from HTML5:
<acronym>
<applet>
<basefont>
<big>
<center>
<dir>
<font>
<frame>
<frameset>
<noframes>
<strike>
<tt>
<u>
Today, most videos are shown through a plug-in (like flash). However, different browsers
may have different plug-ins.
HTML5 defines a new element which specifies a standard way to embed a video/movie on a
web page: the <video> element.
Example
<video width="320" height="240" controls="controls">
<source src="movie.mp4" type="video/mp4" />
<source src="movie.ogg" type="video/ogg" />
Your browser does not support the video tag.
</video>
Try it yourself »
The control attribute adds video controls, like play, pause, and volume.
It is also a good idea to always include width and height attributes. If height and width are set,
the space required for the video is reserved when the page is loaded. However, without these
attributes, the browser does not know the size of the video, and cannot reserve the appropriate
space to it. The effect will be that the page layout will change during loading (while the video
loads).
You should also insert text content between the <video> and </video> tags for browsers that
do not support the <video> element.
The <video> element allows multiple <source> elements. <source> elements can link to
different video files. The browser will use the first recognized format.
MP4 = MPEG 4 files with H264 video codec and AAC audio codec
WebM = WebM files with VP8 video codec and Vorbis audio codec
Ogg = Ogg files with Theora video codec and Vorbis audio codec
There are methods for playing, pausing, and loading, for example. There are properties (e.g.
duration, volume, seeking) that you can read or set. There are also DOM events that can
notify you, for example, when the <video> element begins to play, is paused, is ended, etc.
The examples below illustrate, in a simple way, how to address a <video> element, read and
set properties, and call methods.
<!DOCTYPE html>
<html>
<body>
<div style="text-align:center">
<button onclick="playPause()">Play/Pause</button>
<button onclick="makeBig()">Big</button>
<button onclick="makeSmall()">Small</button>
<button onclick="makeNormal()">Normal</button>
<br />
<video id="video1" width="420">
<source src="mov_bbb.mp4" type="video/mp4" />
<source src="mov_bbb.ogg" type="video/ogg" />
Your browser does not support HTML5 video.
</video>
</div>
<script type="text/javascript">
var myVideo=document.getElementById("video1");
function playPause()
{
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
function makeBig()
{
myVideo.width=560;
}
function makeSmall()
{
myVideo.width=320;
}
function makeNormal()
{
myVideo.width=420;
}
</script>
Note: Of the video properties, only videoWidth and videoHeight are immediately available.
The other properties are available after the video's meta data has loaded.
Today, most audio files are played through a plug-in (like flash). However, different browsers
may have different plug-ins.
HTML5 defines a new element which specifies a standard way to embed an audio file on a
web page: the <audio> element.
Example
<audio controls="controls">
<source src="song.ogg" type="audio/ogg" />
<source src="song.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
Try it yourself »
The control attribute adds audio controls, like play, pause, and volume.
You should also insert text content between the <audio> and </audio> tags for browsers that
do not support the <audio> element.
The <audio> element allows multiple <source> elements. <source> elements can link to
different audio files. The browser will use the first recognized format.
In HTML5, drag and drop is part of the standard, and any element can be draggable.
Browser Support
Internet Explorer 9, Firefox, Chrome, and Safari 5 support drag and drop.
Example
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev)
{
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
ev.preventDefault();
}
</script>
</head>
<body>
</body>
</html>
Try it yourself »
It might seem complicated, but lets go through all the different parts of a drag and drop event.
In the example above, the ondragstart attribute calls a function, drag(event), that specifies
what data to be dragged.
The dataTransfer.setData() method sets the data type and the value of the dragged data:
function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
In this case, the data type is "Text" and the value is the id of the draggable element ("drag1").
This is done by calling the event.preventDefault() method for the ondragover event:
event.preventDefault()
function drop(ev)
{
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
ev.preventDefault();
}
Code explained:
Get the dragged data with the dataTransfer.getData("Text") method. This method will
return any data that was set to the same type in the setData() method
The dragged data is the id of the dragged element ("drag1")
Append the dragged element into the drop element
Call preventDefault() to prevent the browser default handling of the data
(default is open as link on drop)