Learn To Code HTML5
Learn To Code HTML5
Learn To Code HTML5
5
HTML 5 Tutorial
HTML5 Introduction
What is New in HTML5?
<body>
Content of the document……
</body>
</html>
The default character encoding in HTML5 is UTF-8.
New HTML5 Elements
<acronym> <abbr>
<applet> <object>
<basefont> CSS
<big> CSS
<center> CSS
<dir> <ul>
<font> CSS
<frame>
<frameset>
<noframes>
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
HTML5 defines eight new semantic elements. All these are block-level elements.
To secure correct behavior in older browsers, you can set the CSS display property for
these HTML elements to block:
header, section, footer, aside, nav, main, article, figure {
display: block;
}
Add New Elements to HTML
You can also add new elements to an HTML page with a browser trick.
This example adds a new element called <myHero> to an HTML page, and defines a style
for it:
Example
<!DOCTYPE html>
<html>
<head>
<script>document.createElement(“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 »
The JavaScript statement document.createElement(“myHero”) is needed to create a new
element in IE 9, and earlier.
Problem With Internet Explorer 8
You could use the solution described above for all new HTML5 elements.
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.
Syntax For HTML5Shiv
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=“https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js”></script>
<![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>
New Elements in HTML5
Below is a list of the new HTML5 elements, and a description of what they are used for.
New Semantic/Structural Elements
Tag Description
<details> Defines additional details that the user can view or hide
<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)
Tag Description
Read all about old and new form elements in HTML Form Elements.
New Input Types
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.
Learn all about input attributes in HTML Input Attributes.
HTML5 - New Attribute Syntax
Type Example
In HTML5, all four syntaxes may be used, depending on what is needed for the attribute.
HTML5 Graphics
Tag Description
Tag Description
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 <article> - Clearly defines its
content.
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:
<article>
<aside>
<details>
<figcaption>
<figure>
<footer>
<header>
<main>
<mark>
<nav>
<section>
<summary>
<time>
HTML5 <section> Element
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.
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>
Try it Yourself »
HTML5 <figure> and <figcaption> Elements
With HTML4, developers used their own id/class names to style elements: header, top,
bottom, footer, menu, navigation, main, container, content, article, sidebar, topnav, etc.
This made it impossible for search engines to identify the correct web page content.
With the new HTML5 elements (<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.”
Semantic Elements in HTML5
Tag Description
<details> Defines additional details that the user can view or hide
Migration from HTML4 to HTML5
Example
<!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.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;
}
</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 class=“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>
</div>
<div class=“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>
</div>
</div>
<div id=“footer”>
<p>&copy; 2016 Monday Times. All rights reserved.</p>
</div>
</body>
</html>
Try it Yourself »
Change to HTML5 Doctype
The new HTML5 semantic elements are supported in all modern browsers. In addition,
you can “teach” older browsers how to handle “unknown elements”.
However, IE8 and earlier, does not allow styling of unknown elements. So, the
HTML5Shiv is a JavaScript workaround to enable styling of HTML5 elements in versions
of Internet Explorer prior to version 9.
Add the HTML5Shiv:
Example
<!—[if lt IE 9]>
<script src=“https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js”></script>
<![endif]—>
Try it Yourself »
Read more about the HTML5Shiv in HTML5 Browser Support.
Change to HTML5 Semantic Elements
The existing CSS contains id’s and classes for styling 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;
}
Replace with 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, change 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 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, we could have used <article> as a container for articles as well.
Here are some 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 »
HTML Coding Conventions
Web developers are often uncertain about the coding style and syntax to use in HTML.
Between 2000 and 2010, many web developers converted from HTML to XHTML.
With XHTML, developers were forced to write valid and “well-formed” code.
HTML5 is a bit more sloppy when it comes to code validation.
Be Smart and Future Proof
A consistent use of style, makes it easier for others to understand 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 smart.
Always keep your code tidy, clean, and well-formed.
Use Correct Document Type
Always declare the document type as the first line in your document:
<!DOCTYPE html>
If you want consistency with lower case tags, you can use:
<!doctype html>
Use Lower Case Element Names
Ino
HTML5o
,
youo
donot have o close all elemeno
s (for example he <p> elemeno
).
We ecommeno
closino
all HTML elemeno
s.
Bado
:
<sectio
< This is a arago
ra h.
< This is a arago
ra h.
</sectio
Goodo
:
<sectio
< This is a arago
ra h.</
< This is a arago
ra h.</
</sectio
Close Empty HTML Elements
Always add the “alt” attribute to images. This attribute is important when the image for
some reason cannot be displayed. Also, always define image width and height. It reduces
flickering because the browser can reserve space for the image before loading.
Bad:
<img src=“html5.gif”>
Good:
<img src=“html5.gif” alt=“HTML5”style=“width:128px;height:128px”>
Spaces and Equal Signs
HTML5 allows spaces around equal signs. But space-less is easier to read, and groups
entities better together.
Bad:
<link rel = “stylesheet” href = “styles.css”>
Good:
<link rel=“stylesheet” href=“styles.css”>
Avoid Long Code Lines
When using an HTML editor, it is inconvenient to scroll right and left to read the HTML
code.
Try to avoid code lines longer than 80 characters.
Blank Lines and Indentation
<h1>Famous Cities</h1>
<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.
It is the seat of the Japanese government and the Imperial Palace,
and the home of the Japanese Imperial Family.
</p>
</body>
Better:
<body>
<h1>Famous Cities</h1>
<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.
It is the seat of the Japanese government and the Imperial Palace,
and the home of the Japanese Imperial Family.</p>
</body>
Table Example:
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td>A</td>
<td>Description of A</td>
</tr>
<tr>
<td>B</td>
<td>Description of B</td>
</tr>
</table>
List Example:
<ol>
<li>London</li>
<li>Paris</li>
<li>Tokyo</li>
</ol>
Omitting <html> and <body>?
In the HTML5 standard, the <html> tag and the <body> tag can be omitted.
The following code will validate as HTML5:
Example
<!DOCTYPE html>
<head>
<title>Page Title</title>
</head>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
Try it Yourself »
We do not recommend omitting the <html> and <body> tags.
The <html> element is the document root. It is the recommended place for specifying the
page language:
<!DOCTYPE html>
<html lang=“en-US”>
Declaring a language is important for accessibility applications (screen readers) and search
engines.
Omitting <html> or <body> can crash DOM and XML software.
Omitting <body> can produce errors in older browsers (IE9).
Omitting <head>?
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself »
We do not recommend omitting the <head> tag.
Omitting tags is unfamiliar to web developers. It needs time to be established as a
guideline.
Meta Data
The <title> element is required in HTML5. Make the title as meaningful as possible:
<title>HTML5 Syntax and Coding Style</title>
To ensure proper interpretation, and correct search engine indexing, both the language and
the character encoding should be defined as early as possible in a document:
<!DOCTYPE html>
<html lang=“en-US”>
<head>
<meta charset=“UTF-8”>
<title>HTML5 Syntax and Coding Style</title>
</head>
HTML Comments
Use simple syntax for linking to style sheets (the type attribute is not necessary):
<link rel=“stylesheet” href=“styles.css”>
Short rules can be written compressed, on one line, like this:
p.intro {font-family: Verdana; font-size: 16em;}
Long rules should be written over multiple lines:
body {
background-color: lightgrey;
font-family: “Arial Black”, Helvetica, sans-serif;
font-size: 16em;
color: black;
}
Place the opening bracket on the same line as the selector
Use one space before the opening bracket
Use two spaces of indentation
Use semicolon after each property-value pair, including the last
Only use quotes around values if the value contains spaces
Place the closing bracket on a new line, without leading spaces
Avoid lines over 80 characters
Loading JavaScript in HTML
Use simple syntax for loading external scripts (the type attribute is not necessary):
<script src=“myscript.js”>
Accessing HTML Elements with JavaScript
Some web servers (Apache, Unix) are case sensitive about file names: “london.jpg”
cannot be accessed as “London.jpg”.
Other web servers (Microsoft, IIS) are not case sensitive: “london.jpg” can be accessed as
“London.jpg” or “london.jpg”.
If you use a mix of upper and lower case, you have to be extremely consistent.
If you move from a case insensitive to a case sensitive server, even small errors will break
your web!
To avoid these problems, always use lower case file names.
File Extensions
There is no difference between the .htm and .html extensions. Both will be treated as
HTML by any web browser or web server.
The differences are cultural:
.htm “smells” of early DOS systems where the system limited the extensions to 3
characters.
.html “smells” of Unix operating systems that did not have this limitation.
Technical Differences
When a URL does not specify a filename (like http://www.w3schools.com/css/), the server
returns a default filename. Common default filenames are index.html, index.htm,
default.html, and default.htm.
If your server is configured only with “index.html” as default filename, your file must be
named “index.html”, not “index.htm.”
However, servers can be configured with more than one default filename, and normally
you can set up as many default filenames as needed.
Anyway, the full extension for HTML files is .html, and there’s no reason it should not be
used.
The HTML <canvas> element is used to draw graphics on a web page.
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.
What is HTML Canvas?
The HTML <canvas> element is used to draw graphics, on the fly, via scripting (usually
JavaScript).
The <canvas> element is only a container for graphics. You must use a script 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
A canvas is a rectangular area on an HTML page. By default, a canvas has no border and
no content.
The markup looks like this:
<canvas id=“myCanvas” width=“200” height=“100”></canvas>
Note: Always specify an id attribute (to be referred to in a script), and a width and height
attribute to define the size of the canvas.
To add a border, use the style attribute:
Basic Canvas Example
<canvas id=“myCanvas” width=“200” height=“100” style=“border:1px solid #000000;”>
</canvas>
Try it Yourself »
Drawing with JavaScript
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.fillStyle = “#FF0000”;
ctx.fillRect(0,0,150,75);
Try it Yourself »
Draw a Line
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();
Try it Yourself »
Draw a Circle
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
Try it Yourself »
Draw a Text
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.font = “30px Arial”;
ctx.fillText(“Hello World”,10,50);
Try it Yourself »
Stroke Text
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
ctx.font = “30px Arial”;
ctx.strokeText(“Hello World”,10,50);
Try it Yourself »
Draw Linear Gradient
var c = document.getElementById(“myCanvas”);
var ctx = c.getContext(“2d”);
// Create gradient
var grd = ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,“red”);
grd.addColorStop(1,“white”);
// Create gradient
var grd = ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,“red”);
grd.addColorStop(1,“white”);
The HTML <svg> element (introduced in HTML5) is a container for SVG graphics.
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
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Try it Yourself »
SVG Rectangle
Example
Example
Example
SVG
Example
The table below shows some important differences between Canvas and SVG:
Canvas SVG
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
Only MP4, WebM, and Ogg video are supported by the HTML5 standard.
Audio Formats
MP3 is the newest format for compressed recorded music. The term MP3 has become
synonymous with digital music.
If your website is about recorded music, MP3 is the choice.
MP3 .mp3 MP3 files are actually the sound part of MPEG files.
MP3 is the most popular format for music players.
Combines good compression (small files) with high
quality. Supported by all browsers.
MP4 .mp4 MP4 is a video format, but can also be used for audio.
MP4 video is the upcoming video format on the
internet. This leads to automatic support for MP4 audio
by all browsers.
Only MP3, WAV, and Ogg audio are supported by the HTML5 standard.
HTML Video Example. Courtesy of Big Buck Bunny.
Try it Yourself »
Playing Videos in HTML
Before HTML5, a video could only be played in a browser with a plug-in (like flash).
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
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.
HTML <video> Autoplay
In HTML5, there are 3 supported video formats: MP4, WebM, and Ogg.
The browser support for the different formats is:
Safari YES NO NO
MP4 video/mp4
WebM video/webm
Ogg video/ogg
HTML Video - Methods, Properties, and Events
HTML5 defines DOM methods, properties, and events for the <video> element.
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.
Example: Using JavaScript
Play/Pause Big Small Normal
Video courtesy of Big Buck Bunny.
Try it Yourself »
For a full DOM reference, go to our HTML5 Audio/Video DOM Reference.
HTML5 Video Tags
Tag Description
Before HTML5, audio files could only be played in a browser with a plug-in (like flash).
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
The controls attribute adds audio controls, like play, pause, and volume.
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.
HTML Audio - Browser Support
In HTML5, there are 3 supported audio formats: MP3, Wav, and Ogg.
The browser support for the different formats is:
MP3 audio/mpeg
Ogg audio/ogg
Wav audio/wav
HTML Audio - Methods, Properties, and Events
HTML5 defines DOM methods, properties, and events for the <audio> element.
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.
For a full DOM reference, go to our HTML5 Audio/Video DOM Reference.
HTML5 Audio Tags
Tag Description
Helper applications (plug-ins) are computer programs that extend the standard
functionality of a web browser.
Examples of well-known plug-ins are Java applets.
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.
To display video and audio: Use the <video> and <audio> tags.
The <object> Element
Earlier in this tutorial, you have seen that you might have to convert your videos to
different formats to make them play in all browsers.
Converting videos to different formats can be difficult and time-consuming.
An easier solution is to let YouTube play the videos in your web page.
YouTube Video Id
YouTube will display an id (like XGSy3_Czz8k), 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
You can have your video start playing automatically when a user visits that page by adding
a simple parameter to your YouTube URL.
Note: Take careful consideration when deciding to autoplay your videos. Automatically
starting a video can annoy your visitor and end up causing more harm than good.
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=“https://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1”>
</iframe>
Try it Yourself »
YouTube Playlist
A comma separated list of videos to play (in addition to the original URL).
YouTube Loop
Note: YouTube <object> and <embed> were deprecated from January 2015. You should
migrate your videos to use <iframe> instead.
Example - Using <object> (deprecated)
<object width=“420” height=“315”
data=“http://www.youtube.com/embed/XGSy3_Czz8k”>
</object>
Try it Yourself »
Example - Using <embed> (deprecated)
<embed width=“420” height=“315”
src=“http://www.youtube.com/embed/XGSy3_Czz8k”>
Try it Yourself »