HTML5 Features and Browser Support Guide
HTML5 Features and Browser Support Guide
❮ PreviousNext ❯
<!DOCTYPE html>
<meta charset="UTF-8">
HTML5 Example:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<body>
Content of the document......
</body>
</html>
Try it Yourself »
The default character encoding in HTML5 is UTF-8.
New attributes of form elements like number, date, time, calendar, and range.
New graphic elements: <svg> and <canvas>.
In the next chapter, HTML5 Support, you will learn how to "teach" older browsers to
handle "unknown" (new) HTML elements.
HTML Geolocation
HTML Drag and Drop
HTML Local Storage
HTML Application Cache
HTML Web Workers
HTML SSE
<acronym> <abbr>
<applet> <object>
<basefont> CSS
<big> CSS
<center> CSS
<dir> <ul>
<font> CSS
<frame>
<frameset>
<noframes>
<tt> CSS
In the chapter HTML5 Migration, you will learn how to easily migrate from HTML4 to
HTML5.
HTML History
Since the early days of the World Wide Web, there have been many versions of
HTML:
Year Version
In year 2000, the World Wide Web Consortium (W3C) recommended XHTML 1.0. The
XHTML syntax was strict, and the developers were forced to write valid and "well-
formed" code.
In 2004, W3C's decided to close down the development of HTML, in favor of XHTML.
In 2004 - 2006, the WHATWG gained support by the major browser vendors.
The WHATWG HTML5 Living Standard was published in 2012, and is continuously
updated.
In addition, all browsers, old and new, automatically handle unrecognized elements
as inline elements.
Because of this, you can "teach" older browsers to handle "unknown" HTML
elements.
You can even teach IE6 (Windows XP 2001) how to handle unknown HTML elements.
To secure correct behavior in older browsers, you can set the CSS display property
for these HTML elements to block:
This example adds a new element called <myHero> to an HTML page, and defines a
style for it:
Example
<!DOCTYPE html>
<html>
<head>
<script>[Link]("myHero")</script>
<style>
myHero {
display: block;
background-color: #dddddd;
padding: 50px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>A Heading</h1>
<myHero>My Hero Element</myHero>
</body>
</html>
Try it Yourself »
However, IE8 (and earlier) does not allow styling of unknown elements!
Thankfully, Sjoerd Visscher created the HTML5Shiv! The HTML5Shiv is a JavaScript
workaround to enable styling of HTML5 elements in versions of Internet Explorer prior
to version 9.
You will require the HTML5Shiv to provide compatibility for IE Browsers older than IE
9.
You should use the HTML5Shiv when you are using the new HTML5 elements such
as: <article>, <section>, <aside>, <nav>, <footer>.
You can download the latest version of HTML5shiv from github or reference the CDN
version at [Link]
Syntax
<head>
<!--[if lt IE 9]>
<script src="/js/[Link]"></script>
<![endif]-->
</head>
HTML5Shiv Example
If you do not want to download and store the HTML5Shiv on your site, you could
reference the version found on the CDN site.
The HTML5Shiv script must be placed in the <head> element, after any stylesheets:
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!--[if lt IE 9]>
<script
src="[Link]
>
<![endif]-->
</head>
<body>
<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>
</body>
</html>
Tag Description
<bdi> Isolates a part of text that might be formatted in a different direction from
<details> Defines additional details that the user can view or hide
<rp> Defines what to show in browsers that do not support ruby annotations
Read all about old and new form elements in HTML Form Elements.
color autocomplete
date autofocus
datetime form
datetime-local formaction
email formenctype
month formmethod
number formnovalidate
range formtarget
search height and width
tel list
time min and max
url multiple
week pattern (regexp)
placeholder
required
step
Learn all about old and new input types in HTML Input Types.
Type Example
HTML5 Graphics
Tag Description
<source> Defines multiple media resources for media elements (<video> and <aud
<track> Defines text tracks for media elements (<video> and <audio>)
Examples of non-semantic elements: <div> and <span> - Tells nothing about its
content.
HTML5 offers new semantic elements to define different parts of a web page:
<article>
<aside>
<details>
<figcaption>
<figure>
<footer>
<header>
<main>
<mark>
<nav>
<section>
<summary>
<time>
HTML5 <section> Element
The <section> element defines a section in a document.
A home page could normally be split into sections for introduction, content, and
contact information.
Example
<section>
<h1>WWF</h1>
<p>The World Wide Fund for Nature (WWF) is....</p>
</section>
An article should make sense on its own, and it should be possible to read it
independently from the rest of the web site.
Example
<article>
<h1>What Does WWF Do?</h1>
<p>WWF's mission is to stop the degradation of our planet's natural
environment,
and build a future in which humans live in harmony with nature.</p>
</article>
Can we use the definitions to decide how to nest those elements? No, we cannot!
So, on the Internet, you will find HTML pages with <section> elements
containing <article> elements, and <article> elements
containing <section> elements.
You will also find pages with <section> elements containing <section> elements,
and <article> elements containing <article> elements.
Example for a newspaper: The sport <article> in the sport section, may have a
technical section in each <article>.
Example
<article>
<header>
<h1>What Does WWF Do?</h1>
<p>WWF's mission:</p>
</header>
<p>WWF's mission is to stop the degradation of our planet's natural
environment,
and build a future in which humans live in harmony with nature.</p>
</article>
A footer typically contains the author of the document, copyright information, links to
terms of use, contact information, etc.
Example
<footer>
<p>Posted by: Hege Refsnes</p>
<p>Contact information: <a href="[Link]
someone@[Link]</a>.</p>
</footer>
Notice that NOT all links of a document should be inside a <nav> element.
The <nav> element is intended only for major block of navigation links.
Example
<nav>
<a href="/html/">HTML</a> |
<a href="/css/">CSS</a> |
<a href="/js/">JavaScript</a> |
<a href="/jquery/">jQuery</a>
</nav>
Example
<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>
Example
<figure>
<img src="pic_trulli.jpg" alt="Trulli">
<figcaption>Fig1. - Trulli, Puglia, Italy.</figcaption>
</figure>
The <img> element defines the image, the <figcaption> element defines the
caption.
This made it impossible for search engines to identify the correct web page content.
According to the W3C, a Semantic Web: "Allows data to be shared and reused across
applications, enterprises, and communities."
Tag Description
<details> Defines additional details that the user can view or hide
<figcaption> Defines a caption for a <figure> element
Last name:
Doe
Submit
<form>
.
form elements
.
</form>
Form elements are different types of input elements, like: text fields, checkboxes,
radio buttons, submit buttons, and more.
The <input> element is displayed in several ways, depending on the type attribute.
<input type="radio"> Defines a radio button (for selecting one of many choic
You will learn a lot more about input types later in this tutorial.
Text Fields
<input type="text"> defines a single-line input field for text input.
Example
A form with two text input fields:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of an input field is
20 characters.
The <label> element is useful for screen-reader users, because the screen-reader
will read out load the label when the user is focused on the input element.
The <label> element also help users who have difficulty clicking on very small
regions (such as radio buttons or checkboxes) - because when the user clicks the text
within the <label> element, it toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of the
<input> element to bind them together.
Radio Buttons
<input type="radio"> defines a radio button.
Example
A form with radio buttons:
<form>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>
</form>
This is how the HTML code above will be displayed in a browser:
Male
Female
Other
The form-handler is typically a page on the server with a script for processing input
data.
Example
A form with a submit button:
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
First name:
Last name:
Doe
Submit
The Action Attribute
The action attribute defines the action to be performed when the form is submitted.
Usually, the form data is sent to a page on the server when the user clicks on the
submit button.
In the example above, the form data is sent to a page on the server called
"/action_page.php". This page contains a server-side script that handles the form
data:
<form action="/action_page.php">
If the action attribute is omitted, the action is set to the current page.
The default value is "_self" which means the form will be submitted in the current
window.
To make the form result open in a new browser tab, use the value " _blank".
Example
Here, the submitted result will open in a new browser tab:
Other legal values are "_parent", "_top", or a name representing the name of an
iframe.
The Method Attribute
The method attribute specifies the HTTP method (GET or POST) to be used when
submitting the form data.
Example
Use the GET method when submitting the form:
or:
Example
Use the POST method when submitting the form:
However, when GET is used, the form data will be visible in the page's address
field:
/action_page.php?firstname=John&lastname=Doe
Notes on GET:
Notes on POST:
POST has no size limitations, and can be used to send large amounts of data.
Form submissions with POST cannot be bookmarked
If the name attribute is omitted, the data of that input field will not be sent at all.
Example
This example will not submit the value of the "First name" input field:
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" value="John"><br><br>
<input type="submit" value="Submit">
</form>
Attribute Description
accept-charset Specifies the charset used in the submitted form (default: the page cha
action Specifies an address (url) where to submit the form (default: the submi
autocomplete Specifies if the browser should autocomplete the form (default: on).
method Specifies the HTTP method used when submitting the form (default: GE
name Specifies a name used to identify the form (for DOM usage: document.f
novalidate Specifies that the browser should not validate the form.
target Specifies the target of the address in the action attribute (default: _self
Example
<input type="text" id="firstname" name="firstname">
The <select> Element
The <select> element defines a drop-down list:
Example
<select id="cars" name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<option value="fiat" selected>Fiat</option>
Visible Values:
Use the size attribute to specify the number of visible values:
Example
<select name="cars" size="3">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<select name="cars" size="4" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
The rows attribute specifies the visible number of lines in a text area.
You can also define the size of the text area by using CSS:
Example
<textarea name="message" style="width:200px; height:600px;">
The cat was playing in the garden.
</textarea>
Example
<button type="button" onclick="alert('Hello World!')">Click Me!
</button>
Example
<form action="/action_page.php">
<fieldset>
<legend>Personalia:</legend>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
Users will see a drop-down list of the pre-defined options as they input data.
The list attribute of the <input> element, must refer to the id attribute of
the <datalist> element.
Example
<form action="/action_page.php">
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
The <output> Element
The <output> element represents the result of a calculation (like one performed by a
script).
Example
Perform a calculation and show the result in an <output> element:
<form action="/action_page.php"
oninput="[Link]=parseInt([Link])+parseInt([Link])">
0
<input type="range" id="a" name="a" value="50">
100 +
<input type="number" id="b" name="b" value="50">
=
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Example
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
First name:
Last name:
Example
<form>
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<label for="pwd">Password:</label><br>
<input type="password" id="pwd" name="pwd">
</form>
Username:
Password:
The form-handler is typically a server page with a script for processing input data.
Example
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
First name:
John
Last name:
Doe
Submit
If you omit the submit button's value attribute, the button will get a default text:
Example
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit">
</form>
Example
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
<input type="reset">
</form>
First name:
John
Last name:
Doe
Submit Reset
If you change the input values and then click the "Reset" button, the form-data will
be reset to the default values.
Radio buttons let a user select ONLY ONE of a limited number of choices:
Example
<form>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>
</form>
Male
Female
Other
Input Type Checkbox
<input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
I have a bike
I have a car
I have a boat
Input Type Button
<input type="button"> defines a button:
Example
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
Depending on browser support, a color picker can show up in the input field.
Example
<form>
<label for="favcolor">Select your favorite color:</label>
<input type="color" id="favcolor" name="favcolor">
</form>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
</form>
You can also use the min and max attributes to add restrictions to dates:
Example
<form>
<label for="datemax">Enter a date before 1980-01-01:</label>
<input type="date" id="datemax" name="datemax" max="1979-12-
31"><br><br>
<label for="datemin">Enter a date after 2000-01-01:</label>
<input type="date" id="datemin" name="datemin" min="2000-01-02">
</form>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="birthdaytime">Birthday (date and time):</label>
<input type="datetime-local" id="birthdaytime" name="birthdaytime">
</form>
Input Type Email
The <input type="email"> is used for input fields that should contain an e-mail
address.
Some smartphones recognize the email type, and add ".com" to the keyboard to
match email input.
Example
<form>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">
</form>
Example
<form>
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
</form>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="bdaymonth">Birthday (month and year):</label>
<input type="month" id="bdaymonth" name="bdaymonth">
</form>
The following example displays a numeric input field, where you can enter a value
from 1 to 5:
Example
<form>
<label for="quantity">Quantity (between 1 and 5):</label>
<input type="number" id="quantity" name="quantity" min="1" max="5">
</form>
Input Restrictions
Here is a list of some common input restrictions:
Attribute Description
checked Specifies that an input field should be pre-selected when the page l
type="checkbox" or type="radio")
disabled Specifies that an input field should be disabled
<form action="/action_page.php">
<label for="country">Country:</label>
<input type="text" id="country" name="country" value="Norway" readonl
y><br><br>
<input type="submit" value="Submit">
</form>
A read-only input field cannot be modified (however, a user can tab to it, highlight it,
and copy the text from it).
The readonly attribute can be set to keep a user from changing the value until some
other conditions have been met (like selecting a checkbox, etc.). Then, a JavaScript
can remove the readonly value, and make the input field editable.
HTML
<input> required Attribute
Example
An HTML form with a required input field:
<form action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<input type="submit">
</form>
When present, it specifies that an input field must be filled out before submitting the
form.
HTML
<form action="/action_page.php">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname" disabled><br><br>
<input type="submit" value="Submit">
</form>
HTML Multimedia
What is Multimedia?
Multimedia comes in many different formats. It can be almost anything you can hear
or see.
Examples: Images, music, sound, videos, records, films, animations, and more.
Web pages often contain multimedia elements of different types and formats.
In this chapter you will learn about the different multimedia formats.
Browser Support
The first web browsers had support for text only, limited to a single font in a single
color.
Later came browsers with support for colors and fonts, and images!
Audio, video, and animation have been handled differently by the major browsers.
Different formats have been supported, and some formats require extra helper
programs (plug-ins) to work.
Hopefully this will become history. HTML5 multimedia promises an easier future for
multimedia.
Multimedia Formats
Multimedia elements (like audio or video) are stored in media files.
The most common way to discover the type of a file, is to look at the file extension.
MPEG .mpg MPEG. Developed by the Moving Pictures Expert Group. The first popu
.mpeg the web. Used to be supported by all browsers, but it is not supported
AVI .avi AVI (Audio Video Interleave). Developed by Microsoft. Commonly used
TV hardware. Plays well on Windows computers, but not in web brows
WMV .wmv WMV (Windows Media Video). Developed by Microsoft. Commonly use
and TV hardware. Plays well on Windows computers, but not in web b
RealVideo .rm RealVideo. Developed by Real Media to allow video streaming with lo
.ram used for online video and Internet TV, but does not play in web brows
WebM .webm WebM. Developed by the web giants, Mozilla, Opera, Adobe, and Goo
HTML5.
MPEG-4 .mp4 MP4. Developed by the Moving Pictures Expert Group. Based on Quick
or MP4 in newer video cameras and TV hardware. Supported by all HTML5 br
by YouTube.
Audio Formats
MP3 is the newest format for compressed recorded music. The term MP3 has become
synonymous with digital music.
MIDI .mid MIDI (Musical Instrument Digital Interface). Main format for all electroni
.midi synthesizers and PC sound cards. MIDI files do not contain sound, but d
played by electronics. Plays well on all computers and music hardware,
browsers.
RealAudi .rm RealAudio. Developed by Real Media to allow streaming of audio with lo
o .ram not play in web browsers.
WMA .wma WMA (Windows Media Audio). Developed by Microsoft. Commonly used
well on Windows computers, but not in web browsers.
AAC .aac AAC (Advanced Audio Coding). Developed by Apple as the default form
on Apple computers, but not in web browsers.
WAV .wav WAV. Developed by IBM and Microsoft. Plays well on Windows, Macinto
systems. Supported by HTML5.
Ogg .ogg Ogg. Developed by the [Link] Foundation. Supported by HTML5.
MP3 .mp3 MP3 files are actually the sound part of MPEG files. MP3 is the most pop
players. Combines good compression (small files) with high quality. Sup
browsers.
MP4 .mp4 MP4 is a video format, but can also be used for audio. MP4 video is the
on the internet. This leads to automatic support for MP4 audio by all bro
Video:
The HTML5 <video> element specifies a standard way to embed a video in a web
page.
Browser Support
The numbers in the table specify the first browser version that fully supports
the <video> element.
Element
Example
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="[Link]" type="video/ogg">
Your browser does not support the video tag.
</video>
How it Works
The controls attribute adds video controls, like play, pause, and volume.
It is a good idea to always include width and height attributes. If height and width
are not set, the page might flicker while the video loads.
The <source> element allows you to specify alternative video files which the browser
may choose from. The browser will use the first recognized format.
The text between the <video> and </video> tags will only be displayed in browsers
that do not support the <video> element.
Safari YES NO NO
MP4 video/mp4
WebM video/webm
Ogg video/ogg
This allows you to load, play, and pause videos, as well as setting duration and
volume.
There are also DOM events that can notify you when a video begins to play, is
paused, etc.
<source> Defines multiple media resources for media elements, such as <vid
HTML5 Audio
The HTML5 <audio> element specifies a standard way to embed audio in a web
page.
Browser Support
The numbers in the table specify the first browser version that fully supports
the <audio> element.
Element
Example
<audio controls>
<source src="[Link]" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
The <source> element allows you to specify alternative audio files which the browser
may choose from. The browser will use the first recognized format.
The text between the <audio> and </audio> tags will only be displayed in browsers
that do not support the <audio> element.
Edge/IE YES NO NO
Chrome YES YES YES
MP3 audio/mpeg
OGG audio/ogg
WAV audio/wav
This allows you to load, play, and pause audios, as well as set duration and volume.
There are also DOM events that can notify you when an audio begins to play, is
paused, etc.
<source> Defines multiple media resources for media elements, such as <video>
HTML Plug-ins
The purpose of a plug-in is to extend the functionality of a web browser.
Plug-ins can be added to web pages with the <object> tag or the <embed> tag.
Plug-ins can be used for many purposes: display maps, scan for viruses, verify your
bank id, etc.
It is used to embed plug-ins (like Java applets, PDF readers, Flash Players) in web
pages.
Example
<object width="400" height="50" data="[Link]"></object>
Example
<object width="100%" height="500px" data="[Link]"></object>
Example
<object data="[Link]"></object>
The <embed> element also defines an embedded object within an HTML document.
Web browsers have supported the <embed> element for a long time. However, it
has not been a part of the HTML specification before HTML5.
Example
<embed width="400" height="50" src="[Link]">
Example
<embed width="100%" height="500px" src="[Link]">
Example
<embed src="[Link]">
An easier solution is to let YouTube play the videos in your web page.
YouTube Video Id
YouTube will display an id (like tgbNymZ7vqY), when you save (or play) a video.
You can use this id, and refer to your video in the HTML code.
Playing a YouTube Video in HTML
To play your video on a web page, do the following:
YouTube Autoplay
You can have your video start playing automatically when a user visits that page by
adding a simple parameter to your YouTube URL.
Value 0 (default): The video will not play automatically when the player loads.
Value 1: The video will play automatically when the player loads.
YouTube - Autoplay
<iframe width="420" height="315"
src="[Link]
</iframe>
YouTube Playlist
A comma separated list of videos to play (in addition to the original URL).
YouTube Loop
Value 0 (default): The video will play only once.
YouTube - Loop
<iframe width="420" height="315"
src="[Link]
playlist=tgbNymZ7vqY&loop=1">
</iframe>
YouTube Controls
Value 0: Player controls does not display.
YouTube - Controls
<iframe width="420" height="315"
src="[Link]
</iframe>
The graphic to the left is created with <canvas>. It shows four elements: a red
rectangle, a gradient rectangle, a multicolor rectangle, and a multicolor text.
The <canvas> element is only a container for graphics. You must use JavaScript to
actually draw the graphics.
Canvas has several methods for drawing paths, boxes, circles, text, and adding
images.
Browser Support
The numbers in the table specify the first browser version that fully supports
the <canvas> element.
Element
Example
<script>
var canvas = [Link]("myCanvas");
var ctx = [Link]("2d");
[Link] = "#FF0000";
[Link](0, 0, 150, 75);
</script>
The getContext() is a built-in HTML object, with properties and methods for drawing:
Set the fill style of the drawing object to the color red:
[Link] = "#FF0000";
The fillStyle property can be a CSS color, a gradient, or a pattern. The default fillStyle
is black.
The fillRect(x,y,width,height) method draws a rectangle, filled with the fill style, on
the canvas:
Canvas Coordinates
The HTML canvas is a two-dimensional grid.
This means: Start at the upper-left corner (0,0) and draw a 150x75 pixels rectangle.
Coordinates Example
Mouse over the rectangle below to see its x and y coordinates:
Draw a Line
To draw a straight line on a canvas, use the following methods:
To actually draw the line, you must use one of the "ink" methods, like stroke().
Example
Define a starting point in position (0,0), and an ending point in position (200,100).
Then use the stroke() method to actually draw the line:
Try it Yourself »
ADVERTISEMENT
Draw a Circle
To draw a circle on a canvas, use the following methods:
Example
Define a circle with the arc() method. Then use the stroke() method to actually draw
the circle:
Once we have a gradient object, we must add two or more color stops.
The addColorStop() method specifies the color stops, and its position along the
gradient. Gradient positions can be anywhere between 0 to 1.
To use the gradient, set the fillStyle or strokeStyle property to the gradient, then
draw the shape (rectangle, text, or a line).
Using createLinearGradient()
Example
Create a linear gradient. Fill rectangle with the gradient:
JavaScript:
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
// Create gradient
var grd = [Link](0, 0, 200, 0);
[Link](0, "red");
[Link](1, "white");
Try it Yourself »
ADVERTISEMENT
Using createRadialGradient():
Example
Create a radial/circular gradient. Fill rectangle with the gradient:
JavaScript:
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
// Create gradient
var grd = [Link](75, 50, 5, 90, 60, 100);
[Link](0, "red");
[Link](1, "white");
Using fillText()
Example
Set font to 30px "Arial" and write a filled text on the canvas:
JavaScript:
Try it Yourself »
Using strokeText()
Example
Set font to 30px "Arial" and write a text, with no fill, on the canvas:
JavaScript:
Try it Yourself »
ADVERTISEMENT
JavaScript:
Example
<canvas id="myCanvas" width="200" height="100" style="border:1px solid
#000000;">
</canvas>
Draw a Line
Example
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
[Link](0, 0);
[Link](200, 100);
[Link]();
Draw a Circle
Example
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
[Link]();
[Link](95, 50, 40, 0, 2 * [Link]);
[Link]();
Draw a Text
Example
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
[Link] = "30px Arial";
[Link]("Hello World", 10, 50);
Stroke Text
Example
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
[Link] = "30px Arial";
[Link]("Hello World", 10, 50);
// Create gradient
var grd = [Link](0, 0, 200, 0);
[Link](0, "red");
[Link](1, "white");
// Create gradient
var grd = [Link](75, 50, 5, 90, 60, 100);
[Link](0, "red");
[Link](1, "white");
// Fill with gradient
[Link] = grd;
[Link](10, 10, 150, 80);
Draw Image
var c = [Link]("myCanvas");
var ctx = [Link]("2d");
var img = [Link]("scream");
[Link](img, 10, 10);
HTML5 SVG
What is SVG?
SVG stands for Scalable Vector Graphics
SVG is used to define graphics for the Web
SVG is a W3C recommendation
SVG has several methods for drawing paths, boxes, circles, text, and graphic images.
Browser Support
The numbers in the table specify the first browser version that fully supports
the <svg> element.
Element
SVG Circle
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
SVG Rectangle
Example
<svg width="400" height="100">
<rect width="400" height="100" style="fill:rgb(0,0,255);stroke-
width:10;stroke:rgb(0,0,0)" />
</svg>
SVG Star
Example
<svg width="300" height="200">
<polygon points="100,10 40,198 190,78 10,78 160,198"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg>
SVG Logo
SVG
Example
<svg height="130" width="500">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-
opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-
opacity:1" />
</linearGradient>
</defs>
<ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
<text fill="#ffffff" font-size="45" font-family="Verdana" x="50"
y="86">SVG</text>
Sorry, your browser does not support inline SVG.
</svg>
SVG is XML based, which means that every element is available within the SVG DOM.
You can attach JavaScript event handlers for an element.
Canvas SVG
HTML5 Geolocation
The HTML Geolocation API is used to locate a user's position.
Try It
Since this can compromise privacy, the position is not available unless the user approves it.
Note: Geolocation is most accurate for devices with GPS, like smartphone.
Browser Support
The numbers in the table specify the first browser version that fully supports Geolocation.
API
Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as
HTTPS. If your site is hosted on an non-secure origin (such as HTTP) the requests to get the
users location will no longer function.
The example below returns the latitude and longitude of the user's position:
Example
<script>
var x = [Link]("demo");
function getLocation() {
if ([Link]) {
[Link](showPosition);
} else {
[Link] = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
[Link] = "Latitude: " + [Link] +
"<br>Longitude: " + [Link];
}
</script>
Example explained:
The example above is a very basic Geolocation script, with no error handling.
Example
function showError(error) {
switch([Link]) {
case error.PERMISSION_DENIED:
[Link] = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
[Link] = "Location information is unavailable."
break;
case [Link]:
[Link] = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
[Link] = "An unknown error occurred."
break;
}
}
In the example below, the returned latitude and longitude is used to show the location in a
Google Map (using a static image):
Example
function showPosition(position) {
var latlon = [Link] + "," + [Link];
Location-specific Information
This page has demonstrated how to show a user's position on a map.
Property Returns
watchPosition() - Returns the current position of the user and continues to return
updated position as the user moves (like the GPS in a car).
clearWatch() - Stops the watchPosition() method.
The example below shows the watchPosition() method. You need an accurate GPS device to
test this (like smartphone):
Example
<script>
var x = [Link]("demo");
function getLocation() {
if ([Link]) {
[Link](showPosition);
} else {
[Link] = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
[Link] = "Latitude: " + [Link] +
"<br>Longitude: " + [Link];
}
</script>
In HTML5, drag and drop is part of the standard: Any element can be draggable.
Browser Support
The numbers in the table specify the first browser version that fully supports Drag
and Drop.
API
Example
<!DOCTYPE HTML>
<html>
<head>
<script>
function allowDrop(ev) {
[Link]();
}
function drag(ev) {
[Link]("text", [Link]);
}
function drop(ev) {
[Link]();
var data = [Link]("text");
[Link]([Link](data));
}
</script>
</head>
<body>
</body>
</html>
It might seem complicated, but lets go through all the different parts of a drag and
drop event.
<img draggable="true">
What to Drag - ondragstart and
setData()
Then, specify what should happen when the element is dragged.
In the example above, the ondragstart attribute calls a function, drag(event), that
specifies what data to be dragged.
The [Link]() method sets the data type and the value of the
dragged data:
function drag(ev) {
[Link]("text", [Link]);
}
In this case, the data type is "text" and the value is the id of the draggable element
("drag1").
[Link]()
function drop(ev) {
[Link]();
var data = [Link]("text");
[Link]([Link](data));
}
Code explained:
Before HTML5, application data had to be stored in cookies, included in every server
request. Web storage is more secure, and large amounts of data can be stored
locally, without affecting website performance.
Unlike cookies, the storage limit is far larger (at least 5MB) and information is never
transferred to the server.
Web storage is per origin (per domain and protocol). All pages, from one origin, can
store and access the same data.
Browser Support
The numbers in the table specify the first browser version that fully supports Web
Storage.
API
Before using web storage, check browser support for localStorage and
sessionStorage:
Example
// Store
[Link]("lastname", "Smith");
// Retrieve
[Link]("result").innerHTML =
[Link]("lastname");
Example explained:
// Store
[Link] = "Smith";
// Retrieve
[Link]("result").innerHTML = [Link];
[Link]("lastname");
Note: Name/value pairs are always stored as strings. Remember to convert them to
another format when needed!
The following example counts the number of times a user has clicked a button. In this
code the value string is converted to a number to be able to increase the counter:
Example
if ([Link]) {
[Link] = Number([Link]) + 1;
} else {
[Link] = 1;
}
[Link]("result").innerHTML = "You have clicked the
button " +
[Link] + " time(s).";
The following example counts the number of times a user has clicked a button, in the
current session:
Example
if ([Link]) {
[Link] = Number([Link]) + 1;
} else {
[Link] = 1;
}
[Link]("result").innerHTML = "You have clicked the
button " +
[Link] + " time(s) in this session.";
A web worker is a JavaScript that runs in the background, independently of other scripts,
without affecting the performance of the page. You can continue to do whatever you want:
clicking, selecting things, etc., while the web worker runs in the background.
Browser Support
The numbers in the table specify the first browser version that fully support Web Workers.
API
Example
Count numbers:
Here, we create a script that counts. The script is stored in the "demo_workers.js" file:
var i = 0;
function timedCount() {
i = i + 1;
postMessage(i);
setTimeout("timedCount()",500);
}
timedCount();
The important part of the code above is the postMessage() method - which is used to post a
message back to the HTML page.
Note: Normally web workers are not used for such simple scripts, but for more CPU intensive
tasks.
The following lines checks if the worker already exists, if not - it creates a new web worker
object and runs the code in "demo_workers.js":
if (typeof(w) == "undefined") {
w = new Worker("demo_workers.js");
}
Then we can send and receive messages from the web worker.
[Link] = function(event){
[Link]("result").innerHTML = [Link];
};
When the web worker posts a message, the code within the event listener is executed. The
data from the web worker is stored in [Link].
[Link]();
Reuse the Web Worker
If you set the worker variable to undefined, after it has been terminated, you can reuse the
code:
w = undefined;
Example
<!DOCTYPE html>
<html>
<body>
<script>
var w;
function startWorker() {
if (typeof(Worker) !== "undefined") {
if (typeof(w) == "undefined") {
w = new Worker("demo_workers.js");
}
[Link] = function(event) {
[Link]("result").innerHTML = [Link];
};
} else {
[Link]("result").innerHTML = "Sorry! No Web Worker
support.";
}
}
function stopWorker() {
[Link]();
w = undefined;
}
</script>
</body>
</html>