HTML Tutorial
HTML Tutorial
HTML Example
A small HTML document:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Try it Yourself
Example Explained
Using this description, a web browser can display a document with a heading and a paragraph.
HTML Tags
HTML tags are keywords (tag names) surrounded by angle brackets:
<tagname>content</tagname>
The first tag in a pair is the start tag, the second tag is the end tag
The end tag is written like the start tag, but with a slash before the tag name
The start tag is often called the opening tag. The end tag is often called the closing tag.
Web Browsers
The purpose of a web browser (Chrome, IE, Firefox, Safari) is to read HTML documents and display
them.
The browser does not display the HTML tags, but uses them to determine how to display the document:
Common Declarations
HTML5
<!DOCTYPE html>
HTML 4.01
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
XHTML 1.0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
All tutorials and examples at W3Schools use HTML5.
HTML Versions
Since the early days of the web, there have been many versions of HTML:
Version
HTML
HTML 2.0
HTML 3.2
HTML 4.01
XHTML
HTML5
Year
1991
1995
1997
1999
2000
2014
Adobe Dreamweaver
Microsoft Expression Web
CoffeeCup HTML Editor
However, for learning HTML we recommend a text editor like Notepad (PC) or TextEdit (Mac).
We believe using a simple text editor is a good way to learn HTML.
Follow the 4 steps below to create your first web page with Notepad.
You can use either .htm or .html as file extension. There is no difference, it is up to you.
To open a file in a browser, double click on the file, or right-click, and choose open with.
HTML Documents
All HTML documents must start with a type declaration: <!DOCTYPE html>.
The HTML document itself begins with <html> and ends with </html>.
The visible part of the HTML document is between <body> and </body>.
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Try it Yourself
HTML Headings
HTML headings are defined with the <h1> to <h6> tags:
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
Try it Yourself
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Try it Yourself
HTML Links
HTML links are defined with the <a> tag:
Example
<a href="http://www.w3schools.com">This is a link</a>
Try it Yourself
The link address is specified in the href attribute.
Attributes are used to provide additional information about HTML elements.
HTML Images
HTML images are defined with the <img> tag.
The source file (src), alternative text (alt), and size (width and height) are provided as attributes:
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">
Try it Yourself
You will learn more about attributes in a later chapter.
HTML Elements
Previous
Next Chapter
HTML documents are made up by HTML elements.
HTML Elements
HTML elements are written with a start tag, with an end tag, with the content in between:
<tagname>content</tagname>
The HTML element is everything from the start tag to the end tag:
Element content
End tag
My First Heading </h1>
My first paragraph. </p>
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Try it yourself
Example
<html>
<body>
<p>This is a paragraph
<p>This is a paragraph
</body>
</html>
Try it yourself
The example above works in all browsers, because the closing tag is considered optional.
Never rely on this. It might produce unexpected results and/or errors if you forget the end tag.
HTML Attributes
Previous
Next Chapter
Attributes provide additional information about HTML elements.
HTML Attributes
Example
<!DOCTYPE html>
<html lang="en-US">
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The first two letters specify the language (en). If there is a dialect, use two more letters (US).
Example
<p title="About W3Schools">
W3Schools is a web developer's site.
It provides tutorials and references covering
many aspects of web programming,
including HTML, CSS, JavaScript, XML, SQL, PHP, ASP, etc.
</p>
Try it Yourself
When you move the mouse over the element, the title will be displayed as a tooltip.
Example
<a href="http://www.w3schools.com">This is a link</a>
Try it Yourself
You will learn more about links and the <a> tag later in this tutorial.
Size Attributes
HTML images are defined with the <img> tag.
The filename of the source (src), and the size of the image (width and height) are all provided as
attributes:
Example
<img src="w3schools.jpg" width="104" height="142">
Try it Yourself
The image size is specified in pixels: width="104" means 104 screen pixels wide.
You will learn more about images and the <img> tag later in this tutorial.
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">
Try it Yourself
Example
<a href=http://www.w3schools.com>
Try it Yourself
W3C recommends quotes in HTML4, and demands quotes for stricter document types like XHTML.
Sometimes it is necessary to use quotes. This will not display correctly, because it contains a space:
Example
<p title=About W3Schools>
Try it Yourself
Using quotes are the most common. Omitting quotes can produce errors.
At W3Schools we always use quotes around attribute values.
Example
<p title='John "ShotGun" Nelson'>
Or vice versa:
Example
<p title="John 'ShotGun' Nelson">
Chapter Summary
Exercise 2
Exercise 3
Exercise 4
HTML Attributes
Below is an alphabetical list of some attributes often used in HTML:
Attribute
alt
disabled
href
id
src
style
title
value
Description
Specifies an alternative text for an image
Specifies that an input element should be disabled
Specifies the URL (web address) for a link
Specifies a unique id for an element
Specifies the URL (web address) for an image
Specifies an inline CSS style for an element
Specifies extra information about an element (displayed as a tool tip)
Specifies the value (text content) for an input element.
A complete list of all attributes for each HTML element, is listed in our: HTML Tag Reference.
HTML Headings
Previous
Next Chapter
Headings are important in HTML documents.
HTML Headings
Headings are defined with the <h1> to <h6> tags.
<h1> defines the most important heading. <h6> defines the least important heading.
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
Try it Yourself
Note: Browsers automatically add some empty space (a margin) before and after each heading.
Example
<p>This is a paragraph.</p>
<hr>
<p>This is a paragraph.</p>
<hr>
<p>This is a paragraph.</p>
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
.
.
Try it Yourself
Meta data means data about data. HTML meta data is data about the HTML document.
Exercise 1
Exercise 2
Exercise 3
Description
Defines an HTML document
Defines the document's body
Defines the document's head element
Defines HTML headings
Defines a horizontal line
HTML Paragraphs
Previous
Next Chapter
HTML documents are divided into paragraphs.
HTML Paragraphs
The HTML <p> element defines a paragraph.
Example
<p>This is a paragraph</p>
<p>This is another paragraph</p>
Try it Yourself
Browsers automatically add an empty line before and after a paragraph.
HTML Display
You cannot be sure how HTML will be displayed.
Large or small screens, and resized windows will create different results.
With HTML, you cannot change the output by adding extra spaces or extra lines in your HTML code.
The browser will remove extra spaces and extra lines when the page is displayed.
Any number of spaces, and any number of new lines, count as only one space.
Example
<p>
This paragraph
contains a lot of lines
in the source code,
but the browser
ignores it.
</p>
<p>
This paragraph
contains
a lot of spaces
in the source
code,
but the
browser
ignores it.
</p>
Try it Yourself
Example
<p>This is a paragraph
<p>This is another paragraph
Try it Yourself
The example above will work in most browsers, but do not rely on it.
Forgetting the end tag can produce unexpected results or errors.
Stricter versions of HTML, like XHTML, do not allow you to skip the end tag.
Example
<p>This is<br>a para<br>graph with line breaks</p>
Try it Yourself
The <br> element is an empty HTML element. It has no end tag.
Example
<pre>
My Bonnie lies over the ocean.
My Bonnie lies over the sea.
My Bonnie lies over the ocean.
Oh, bring back my Bonnie to me.
</pre>
Try it Yourself
Exercise 2
Exercise 3
Exercise 4
HTML Styles
Previous
Next Chapter
I am Red
I am Blue
Try it Yourself
HTML Styling
Every HTML element has a default style (background color is white and text color is black).
Changing the default style of an HTML element, can be done with the style attribute.
This example changes the default background color from white to lightgrey:
Example
<body style="background-color:lightgrey">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
Try it Yourself
The bgcolor attribute, supported in older versions of HTML, is not valid in HTML5.
Example
<body>
<h1 style="color:blue">This is a heading</h1>
<p style="color:red">This is a paragraph.</p>
</body>
Try it Yourself
HTML Fonts
The font-family property defines the font to be used for an HTML element:
Example
<body>
<h1 style="font-family:verdana">This is a heading</h1>
<p style="font-family:courier">This is a paragraph.</p>
</body>
Try it Yourself
The <font> tag, supported in older versions of HTML, is not valid in HTML5.
Example
<body>
<h1 style="font-size:300%">This is a heading</h1>
<p style="font-size:160%">This is a paragraph.</p>
</body>
Try it Yourself
Example
<body>
<h1 style="text-align:center">Centered Heading</h1>
<p>This is a paragraph.</p>
</body>
Try it Yourself
The <center> tag, supported in older versions of HTML, is not valid in HTML5.
Chapter Summary
Text Formatting
This text is bold
This text is italic
This is superscript
Bold text
Important text
Italic text
Emphasized text
Marked text
Small text
Deleted text
Inserted text
Subscripts
Superscripts
Example
<p>This text is normal.</p>
<p><b>This text is bold</b>.</p>
Try it Yourself
The HTML <strong> element defines strong text, with added semantic "strong" importance.
Example
<p>This text is normal.</p>
<p><strong>This text is strong</strong>.</p>
Try it Yourself
Example
<p>This text is normal.</p>
<p><i>This text is italic</i>.</p>
Try it Yourself
The HTML <em> element defines emphasized text, with added semantic importance.
Example
Example
<h2>HTML <small>Small</small> Formatting</h2>
Try it Yourself
Example
<h2>HTML <mark>Marked</mark> Formatting</h2>
Try it Yourself
HTML Formatting
The HTML <del> element defines deleted (removed) of text.
Example
<p>My favorite color is <del>blue</del> red.</p>
Try it Yourself
Example
Example
<p>This is <sub>subscripted</sub> text.</p>
Try it Yourself
Example
<p>This is <sup>superscripted</sup> text.</p>
Try it Yourself
Description
Defines bold text
Defines emphasized text
Defines italic text
Defines smaller text
Defines important text
Defines subscripted text
Defines superscripted text
Defines inserted text
Defines deleted text
Defines marked/highlighted text
Next Chapter
Quotation
Here is a quote from WWF's website:
For 50 years, WWF has been protecting the future of nature. The world's leading conservation
organization, WWF works in 100 countries and is supported by 1.2 million members in the United
States and close to 5 million globally.
Example
<p>WWF's goal is to: <q>Build a future where people live in harmony with nature.</q></p>
Try it Yourself
Example
<p>Here is a quote from WWF's website:</p>
<blockquote cite="http://www.worldwildlife.org/who/index.html">
For 50 years, WWF has been protecting the future of nature.
The world's leading conservation organization,
WWF works in 100 countries and is supported by
1.2 million members in the United States and
close to 5 million globally.
</blockquote>
Try it Yourself
Example
<p>The <abbr title="World Health Organization">WHO</abbr> was founded in 1948.</p>
Try it Yourself
Example
<address>
Written by Jon Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>
Try it Yourself
Example
<p><cite>The Scream</cite> by Edward Munch. Painted in 1893.</p>
Try it Yourself
Example
<bdo dir="rtl">This text will be written from right to left</bdo>
Try it Yourself
Description
Defines an abbreviation or acronym
Defines contact information for the author/owner of a document
Defines the text direction
Defines a section that is quoted from another source
Defines the definition of a term or an abbreviation.
Defines a short inline quotation
Defines the title of a work
Computer Code
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
}
Example
<p>To open a file, select:</p>
<p><kbd>File | Open...</kbd></p>
Try it Yourself
Example
<samp>
demo.example.com login: Apr 12 09:10:17
Linux 2.6.10-grsec+gg3+e+fhs6b+nfs+gr0501+++p3+c4a+gr2b-reslog-v6.189
</samp>
Try it Yourself
Example
<code>
var person = { firstName:"John", lastName:"Doe", age:50, eyeColor:"blue" }
</code>
Try it Yourself
The <code> element does not preserve extra whitespace and line-breaks:
Example
<p>Coding Example:</p>
<code>
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
}
</code>
Try it Yourself
Example
<p>Coding Example:</p>
<code>
<pre>
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
}
</pre>
</code>
Try it Yourself
Example
<p>Einstein wrote:</p>
<p><var>E = m c<sup>2</sup></var></p>
Try it Yourself
Description
Defines programming code
Defines keyboard input
Defines computer output
Defines a mathematical variable
Defines preformatted text
HTML Comments
Previous
Next Chapter
Comment tags <!-- and --> are used to insert comments in HTML.
Example
<!-- Write your comments here -->
Note: There is an exclamation point (!) in the opening tag, but not in the closing tag.
Comments are not displayed by the browser, but they can help document your HTML.
With comments you can place notifications and reminders in your HTML:
Example
<!-- This is a comment -->
<p>This is a paragraph.</p>
<!-- Remember to add more information here -->
Try it Yourself
Comments are also great for debugging HTML, because you can comment out HTML lines of code, one
at a time, to search for errors:
Example
<!-- Do not display this at the moment
<img border="0" src="pic_mountain.jpg" alt="Mountain">
-->
Try it Yourself
Conditional Comments
You might stumble upon conditional comments in HTML:
<!--[if IE 8]>
.... some HTML here ....
<![endif]-->
Conditional comments defines HTML tags to be executed by Internet Explorer only.
For example <!--webbot bot--> tags wrapped inside HTML comments by FrontPage and Expression Web.
As a rule, let these tags stay, to help support the software that created them.
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color:lightgray}
h1 {color:blue}
p {color:green}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself
The most common way to add styling, is to keep the styles in separate CSS files. But, in this tutorial, we
use internal styling, because it is easier to demonstrate, and easier for you to try it yourself.
You can learn much more about CSS in our CSS Tutorial.
CSS Syntax
CSS styling has the following syntax:
element { property:value; property:value }
The element is an HTML element name. The property is a CSS property. The value is a CSS value.
Multiple styles are separated with semicolon.
Example
<h1 style="color:blue">This is a Blue Heading</h1>
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color:lightgrey}
h1 {color:blue}
p {color:green}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself
CSS Fonts
The CSS color property defines the text color to be used for the HTML element.
The CSS font-family property defines the font to be used for the HTML element.
The CSS font-size property defines the text size to be used for the HTML element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color:blue;
font-family:verdana;
font-size:300%;
}
p {
color:red;
font-family:courier;
font-size:160%;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself
Example
p{
border:1px solid black;
}
Try it Yourself
The CSS padding property defines a padding (space) inside the border:
Example
p{
border:1px solid black;
padding:10px;
}
Try it Yourself
The CSS margin property defines a margin (space) outside the border:
Example
p{
border:1px solid black;
padding:10px;
margin:30px;
}
Try it Yourself
The CSS examples above use px to define sizes in pixels.
The id Attribute
All the examples above use CSS to style HTML elements in a general way.
To define a special style for one special element, first add an id attribute to the element:
Example
<p id="p01">I am different</p>
then define a different style for the (identified) element:
Example
p#p01 {
color:blue;
}
Try it Yourself
Example
<p class="error">I am different</p>
Now you can define a different style for all elements with the specified class:
Example
p.error {
color:red;
}
Try it Yourself
Use id to address single elements. Use class to address groups of elements.
In older HTML versions, several tags and attributes were used to style documents.
These tags and attributes are not supported in HTML5!
Avoid using the <font>, <center>, and <strike> elements.
Avoid using the color and bgcolor attributes.
Chapter Summary
HTML Links
Previous
Next Chapter
Links are found in nearly all web pages. Links allow users to click their way from page to page.
A hyperlink is a text or an image you can click on, and jump to another document.
Example:
<a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a>
Try it Yourself
The href attribute specifies the destination address (http://www.w3schools.com/html/)
The link text is the visible part (Visit our HTML tutorial).
Clicking on the link text, will send you to the specified address.
The link text does not have to be text. It can be an HTML image or any other HTML element.
Local Links
The example above used an absolute URL (A full web address).
A local link (link to the same web site) is specified with a relative URL (without http://www....).
Example:
<a href="html_images.asp">HTML Images</a>
Try it Yourself
Example
<style>
a:link {color:#000000; background-color:transparent; text-decoration:none}
a:visited {color:#000000; background-color:transparent; text-decoration:none}
a:hover {color:#ff0000; background-color:transparent; text-decoration:underline}
a:active {color:#ff0000; background-color:transparent; text-decoration:underline}
</style>
Try it Yourself
Example
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a>
Try it Yourself
Target Value
_blank
_self
_parent
_top
framename
Description
Opens the linked document in a new window or tab
Opens the linked document in the same frame as it was clicked (this is default)
Opens the linked document in the parent frame
Opens the linked document in the full body of the window
Opens the linked document in a named frame
If your webpage is locked in a frame, you can use target="_top" to break out of the frame:
Example
<a href="http://www.w3schools.com/html/" target="_top">HTML5 tutorial!</a>
Try it Yourself
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0">
</a>
Try it Yourself
border:0 is added to prevent IE9 (and earlier) from displaying a border around the image.
Example
Add an id attribute to any <a> element:
<a id="tips">Useful Tips Section</a>
Then create a link to the <a> element (Useful Tips Section):
<a href="#tips">Visit the Useful Tips Section</a>
Or, create a link to the <a> element (Useful Tips Section) from another page:
<a href="http://www.w3schools.com/html_links.htm#tips">Visit the Useful Tips Section</a>
Try it Yourself
Without a trailing slash on subfolder addresses, you might generate two requests to the server.
Many servers will automatically add a slash to the address, and then create a new request.
Chapter Summary
Description
<a>
Defines a hyperlink
HTML Images
Previous
Next Chapter
Example
GIF Images
JPG Images
PNG Images
<!DOCTYPE html>
<html>
<body>
<h2>Spectacular Mountains</h2>
<img src="pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
</body>
</html>
Try it Yourself
Always specify the width and height of an image. If width and height are not specified, the page will
flicker while the image loads.
Example
<img src="html5.gif" alt="The official HTML5 Icon">
The alt attribute is required. A web page will not validate correctly without it.
Example
<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
Try it Yourself
Alternatively, you can use width and height attributes.
The values are specified in pixels (without px after the value):
Example
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<style>
img { width:100%; }
</style>
</head>
<body>
<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
</body>
</html>
Try it Yourself
At W3schools we prefer to use the style attribute.
Example
<img src="/images/html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
Try it Yourself
If a browser cannot find an image, it will display a broken link icon:
Example
<img src="wrongname.gif" alt="HTML5 Icon" style="width:128px;height:128px">
Try it Yourself
Example
<img src="http://www.w3schools.com/images/w3schools_green.jpg">
Try it Yourself
Animated Images
Example
<img src="programming.gif" alt="Computer Man" style="width:48px;height:48px">
Try it Yourself
Note that the syntax of inserting animated images is no different from non-animated images.
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0">
</a>
Try it Yourself
We have added border:0 to prevent IE9 (and earlier) from displaying a border around the image.
Image Maps
For an image, you can create an image map, with clickable areas:
Example
<img src="planets.gif" alt="Planets" usemap="#planetmap" style="width:145px;height:126px">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" alt="Sun" href="sun.htm">
<area shape="circle" coords="90,58,3" alt="Mercury" href="mercur.htm">
<area shape="circle" coords="124,58,8" alt="Venus" href="venus.htm">
</map>
Try it Yourself
Image Floating
You can let an image float to the left or right of a paragraph:
Example
<p>
<img src="smiley.gif" alt="Smiley face" style="float:left;width:42px;height:42px">
A paragraph with an image. The image floats to the left of the text.
</p>
Try it Yourself
Chapter Summary
Exercise 2
Exercise 3
Exercise 4
Exercise 5
HTML Tables
Previous
Next Chapter
First Name
Eve
John
Adam
Jill
Last Name
Jackson
Doe
Johnson
Smith
Points
94
80
67
50
Example
<table border="1" style="width:100%">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself
The border attribute is on its way out of the HTML standard! It is better to use CSS.
To add borders, use the CSS border property:
Example
table, th, td {
border: 1px solid black;
}
Try it Yourself
Remember to define borders for both the table and the table cells.
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
Try it Yourself
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th,td {
padding: 15px;
}
Try it Yourself
Example
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself
To left-align the table headings, use the CSS text-align property:
Example
th {
text-align: left;
}
Try it Yourself
Example
table {
border-spacing: 5px;
}
Try it Yourself
If the table has collapsed borders, border-spacing has no effect.
Example
<table style="width:100%">
<tr>
<th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
Try it Yourself
Example
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th>
<td>555 77 854</td>
</tr>
<tr>
<td>555 77 855</td>
</tr>
</table>
Try it Yourself
Example
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
Try it Yourself
The <caption> tag must be inserted immediately after the <table> tag.
Example
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
Try it Yourself
Chapter Summary
Description
Defines a table
<th>
<tr>
<td>
<caption>
<colgroup>
<col>
<thead>
<tbody>
<tfoot>
HTML Lists
Previous
Next Chapter
Ordered List
1.
2.
3.
4.
Description List
The first item
Description of item
The second item
Description of item
Unordered List:
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
Description
The list items will be marked with bullets (default)
The list items will be marked with circles
The list items will be marked with squares
The list items will not be marked
Disc:
<ul style="list-style-type:disc">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
Circle:
<ul style="list-style-type:circle">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
Square:
<ul style="list-style-type:square">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
None:
<ul style="list-style-type:none">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
Using a type attribute <ul type="disc">, instead of <ul style="list-style-type:disc">, also works.
But in HTML5, the type attribute is not valid in unordered lists, only in ordered list.
Ordered List:
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
Description
The list items will be numbered with numbers (default)
The list items will be numbered with uppercase letters
The list items will be numbered with lowercase letters
The list items will be numbered with uppercase roman numbers
The list items will be numbered with lowercase roman numbers
Numbers:
<ol type="1">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
Upper Case:
<ol type="A">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
Lower Case:
<ol type="a">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
Description List:
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
Try it Yourself
Nested Lists:
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
Try it Yourself
List items can contain new list, and other HTML elements, like images and links, etc.
Horizontal Lists
HTML lists can be styled in many different ways with CSS.
One popular way, is to style a list to display horizontally:
Horizontal List:
<!DOCTYPE html>
<html>
<head>
<style>
ul#menu li {
display:inline;
}
</style>
</head>
<body>
<h2>Horizontal List</h2>
<ul id="menu">
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
<li>PHP</li>
</ul>
</body>
</html>
Try it Yourself
With a little extra style, you can make it look like a menu:
New Style:
ul#menu {
padding: 0;
}
ul#menu li {
display: inline;
}
ul#menu li a {
background-color: black;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 4px 4px 0 0;
}
ul#menu li a:hover {
background-color: orange;
}
Try it Yourself
Chapter Summary
Description
Defines an unordered list
Defines an ordered list
Defines a list item
Defines a description list
Defines the term in a description list
Defines the description in a description list
London
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.
Standing on the River Thames, London has been a major settlement for two millennia, its history going
back to its founding by the Romans, who named it Londinium.
Example
<div style="background-color:black; color:white; padding:20px;">
<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>
Try it Yourself
Example
<h1>My <span style="color:red">Important</span> Heading</h1>
Try it Yourself
HTML Classes
Previous
Next Chapter
Classing HTML elements, makes it possible to define CSS styles for classes of elements.
Equal styles for equal classes, or different styles for different classes.
London
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.
Standing on the River Thames, London has been a major settlement for two millennia, its history going
back to its founding by the Romans, who named it Londinium.
Example
<!DOCTYPE html>
<html>
<head>
<style>
.cities {
background-color:black;
color:white;
margin:20px;
padding:20px;
}
</style>
</head>
<body>
<div class="cities">
<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>
</body>
</html>
Try it Yourself
London
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.
Standing on the River Thames, London has been a major settlement for two millennia, its history going
back to its founding by the Romans, who named it Londinium.
Paris
Paris is the capital and most populous city of France.
Situated on the Seine River, it is at the heart of the le-de-France region, also known as the rgion
parisienne.
Within its metropolitan area is one of the largest population centers in Europe, with over 12 million
inhabitants.
Tokyo
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.
The Tokyo prefecture is part of the world's most populous metropolitan area with 38 million people and
the world's largest urban economy.
Example
<!DOCTYPE html>
<html>
<head>
<style>
.cities {
background-color:black;
color:white;
margin:20px;
padding:20px;
}
</style>
</head>
<body>
<div class="cities">
<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="cities">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>
<div class="cities">
<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>
</body>
</html>
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<style>
span.red {color:red;}
</style>
</head>
<body>
<h1>My <span class="red">Important</span> Heading</h1>
</body>
</html>
Try it Yourself
HTML Classes
Previous
Next Chapter
Classing HTML elements, makes it possible to define CSS styles for classes of elements.
Equal styles for equal classes, or different styles for different classes.
London
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.
Standing on the River Thames, London has been a major settlement for two millennia, its history going
back to its founding by the Romans, who named it Londinium.
Example
<!DOCTYPE html>
<html>
<head>
<style>
.cities {
background-color:black;
color:white;
margin:20px;
padding:20px;
}
</style>
</head>
<body>
<div class="cities">
<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>
</body>
</html>
Try it Yourself
London
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.
Standing on the River Thames, London has been a major settlement for two millennia, its history going
back to its founding by the Romans, who named it Londinium.
Paris
Paris is the capital and most populous city of France.
Situated on the Seine River, it is at the heart of the le-de-France region, also known as the rgion
parisienne.
Within its metropolitan area is one of the largest population centers in Europe, with over 12 million
inhabitants.
Tokyo
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.
The Tokyo prefecture is part of the world's most populous metropolitan area with 38 million people and
the world's largest urban economy.
Example
<!DOCTYPE html>
<html>
<head>
<style>
.cities {
background-color:black;
color:white;
margin:20px;
padding:20px;
}
</style>
</head>
<body>
<div class="cities">
<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="cities">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>
<div class="cities">
<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>
</body>
</html>
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<style>
span.red {color:red;}
</style>
</head>
<body>
<h1>My <span class="red">Important</span> Heading</h1>
</body>
</html>
Try it Yourself
Using Bootstrap
Another way to create a responsive design, is to use an already existing CSS framework.
Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive webs.
Bootstrap helps you to develop sites that look nice at any size; screen, laptop, tablet, or phone:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>W3Schools Demo</h1>
<p>Resize this responsive page!</p>
</div>
<div class="row">
<div class="col-md-4">
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
</div>
<div class="col-md-4">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>
<div class="col-md-4">
<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>
</body>
</html>
Try it yourself
To learn more about Bootstrap read our Bootstrap Tutorial.
HTML Iframes
Previous
Next Chapter
Iframe Syntax
The syntax for adding an iframe is:
<iframe src="URL"></iframe>
The src attribute specifies the URL (web address) of the iframe page.
Example
<iframe src="demo_iframe.htm" width="200" height="200"></iframe>
Try it Yourself
Example
<iframe src="demo_iframe.htm" style="border:none"></iframe>
Try it Yourself
With CSS, you can also change the size, style and color of the iframe's border:
Example
<iframe src="demo_iframe.htm" style="border:5px dotted red"></iframe>
Try it Yourself
Example
<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
<p><a href="http://www.w3schools.com" target="iframe_a">W3Schools.com</a></p>
Try it Yourself
Click on a color name (or a hex value) to view the color as the background-color along with different text
colors:
Color Name
AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
BlanchedAlmond
Blue
BlueViolet
Brown
BurlyWood
CadetBlue
Chartreuse
Chocolate
Coral
CornflowerBlue
Cornsilk
Crimson
Cyan
DarkBlue
DarkCyan
DarkGoldenRod
DarkGray
DarkGreen
DarkKhaki
DarkMagenta
DarkOliveGreen
DarkOrange
DarkOrchid
DarkRed
DarkSalmon
DarkSeaGreen
DarkSlateBlue
DarkSlateGray
DarkTurquoise
DarkViolet
DeepPink
DeepSkyBlue
DimGray
DodgerBlue
FireBrick
FloralWhite
ForestGreen
HEX
#F0F8FF
#FAEBD7
#00FFFF
#7FFFD4
#F0FFFF
#F5F5DC
#FFE4C4
#000000
#FFEBCD
#0000FF
#8A2BE2
#A52A2A
#DEB887
#5F9EA0
#7FFF00
#D2691E
#FF7F50
#6495ED
#FFF8DC
#DC143C
#00FFFF
#00008B
#008B8B
#B8860B
#A9A9A9
#006400
#BDB76B
#8B008B
#556B2F
#FF8C00
#9932CC
#8B0000
#E9967A
#8FBC8F
#483D8B
#2F4F4F
#00CED1
#9400D3
#FF1493
#00BFFF
#696969
#1E90FF
#B22222
#FFFAF0
#228B22
Color
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Fuchsia
Gainsboro
GhostWhite
Gold
GoldenRod
Gray
Green
GreenYellow
HoneyDew
HotPink
IndianRed
Indigo
Ivory
Khaki
Lavender
LavenderBlush
LawnGreen
LemonChiffon
LightBlue
LightCoral
LightCyan
LightGoldenRodYellow
LightGray
LightGreen
LightPink
LightSalmon
LightSeaGreen
LightSkyBlue
LightSlateGray
LightSteelBlue
LightYellow
Lime
LimeGreen
Linen
Magenta
Maroon
MediumAquaMarine
MediumBlue
MediumOrchid
MediumPurple
MediumSeaGreen
MediumSlateBlue
MediumSpringGreen
MediumTurquoise
MediumVioletRed
MidnightBlue
MintCream
MistyRose
Moccasin
#FF00FF
#DCDCDC
#F8F8FF
#FFD700
#DAA520
#808080
#008000
#ADFF2F
#F0FFF0
#FF69B4
#CD5C5C
#4B0082
#FFFFF0
#F0E68C
#E6E6FA
#FFF0F5
#7CFC00
#FFFACD
#ADD8E6
#F08080
#E0FFFF
#FAFAD2
#D3D3D3
#90EE90
#FFB6C1
#FFA07A
#20B2AA
#87CEFA
#778899
#B0C4DE
#FFFFE0
#00FF00
#32CD32
#FAF0E6
#FF00FF
#800000
#66CDAA
#0000CD
#BA55D3
#9370DB
#3CB371
#7B68EE
#00FA9A
#48D1CC
#C71585
#191970
#F5FFFA
#FFE4E1
#FFE4B5
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
NavajoWhite
Navy
OldLace
Olive
OliveDrab
Orange
OrangeRed
Orchid
PaleGoldenRod
PaleGreen
PaleTurquoise
PaleVioletRed
PapayaWhip
PeachPuff
Peru
Pink
Plum
PowderBlue
Purple
RebeccaPurple
Red
RosyBrown
RoyalBlue
SaddleBrown
Salmon
SandyBrown
SeaGreen
SeaShell
Sienna
Silver
SkyBlue
SlateBlue
SlateGray
Snow
SpringGreen
SteelBlue
Tan
Teal
Thistle
Tomato
Turquoise
Violet
Wheat
White
WhiteSmoke
Yellow
YellowGreen
#FFDEAD
#000080
#FDF5E6
#808000
#6B8E23
#FFA500
#FF4500
#DA70D6
#EEE8AA
#98FB98
#AFEEEE
#DB7093
#FFEFD5
#FFDAB9
#CD853F
#FFC0CB
#DDA0DD
#B0E0E6
#800080
#663399
#FF0000
#BC8F8F
#4169E1
#8B4513
#FA8072
#F4A460
#2E8B57
#FFF5EE
#A0522D
#C0C0C0
#87CEEB
#6A5ACD
#708090
#FFFAFA
#00FF7F
#4682B4
#D2B48C
#008080
#D8BFD8
#FF6347
#40E0D0
#EE82EE
#F5DEB3
#FFFFFF
#F5F5F5
#FFFF00
#9ACD32
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Shades
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Mix
Color Values
Colors are defined using a hexadecimal (hex) notation for the Red, Green, and Blue values (RGB).
The lowest value for each light source is 0 (hex 00). The highest value is 255 (hex FF).
Hex values are written as # followed by either three or six hex characters.
Three-digit notations (#rgb) are automatically converted to six digits (#rrggbb):
Color
Try it Yourself
Shades of grey (from black to white) are defined using equal values for all the 3 light sources:
Color
Try it Yourself
HEX
Color
Shades
Mix
Black
#000000
Shades
Mix
Navy
#000080
Shades
Mix
DarkBlue
#00008B
Shades
Mix
MediumBlue
#0000CD
Shades
Mix
Blue
#0000FF
Shades
Mix
DarkGreen
#006400
Shades
Mix
Green
#008000
Shades
Mix
Teal
#008080
Shades
Mix
DarkCyan
#008B8B
Shades
Mix
DeepSkyBlue
#00BFFF
Shades
Mix
DarkTurquoise
#00CED1
Shades
Mix
MediumSpringGreen
#00FA9A
Shades
Mix
Lime
#00FF00
Shades
Mix
SpringGreen
#00FF7F
Shades
Mix
Aqua
#00FFFF
Shades
Mix
Cyan
#00FFFF
Shades
Mix
MidnightBlue
#191970
Shades
Mix
DodgerBlue
#1E90FF
Shades
Mix
LightSeaGreen
#20B2AA
Shades
Mix
ForestGreen
#228B22
Shades
Mix
SeaGreen
#2E8B57
Shades
Mix
DarkSlateGray
#2F4F4F
Shades
Mix
LimeGreen
#32CD32
Shades
Mix
MediumSeaGreen
#3CB371
Shades
Mix
Turquoise
#40E0D0
Shades
Mix
RoyalBlue
#4169E1
Shades
Mix
SteelBlue
#4682B4
Shades
Mix
DarkSlateBlue
#483D8B
Shades
Mix
MediumTurquoise
#48D1CC
Shades
Mix
Indigo
#4B0082
Shades
Mix
DarkOliveGreen
#556B2F
Shades
Mix
CadetBlue
#5F9EA0
Shades
Mix
CornflowerBlue
#6495ED
Shades
Mix
RebeccaPurple
#663399
Shades
Mix
MediumAquaMarine
#66CDAA
Shades
Mix
DimGray
#696969
Shades
Mix
SlateBlue
#6A5ACD
Shades
Mix
OliveDrab
#6B8E23
Shades
Mix
SlateGray
#708090
Shades
Mix
LightSlateGray
#778899
Shades
Mix
MediumSlateBlue
#7B68EE
Shades
Mix
LawnGreen
#7CFC00
Shades
Mix
Chartreuse
#7FFF00
Shades
Mix
Aquamarine
#7FFFD4
Shades
Mix
Maroon
#800000
Shades
Mix
Purple
#800080
Shades
Mix
Olive
#808000
Shades
Mix
Gray
#808080
Shades
Mix
SkyBlue
#87CEEB
Shades
Mix
LightSkyBlue
#87CEFA
Shades
Mix
BlueViolet
#8A2BE2
Shades
Mix
DarkRed
#8B0000
Shades
Mix
DarkMagenta
#8B008B
Shades
Mix
SaddleBrown
#8B4513
Shades
Mix
DarkSeaGreen
#8FBC8F
Shades
Mix
LightGreen
#90EE90
Shades
Mix
MediumPurple
#9370DB
Shades
Mix
DarkViolet
#9400D3
Shades
Mix
PaleGreen
#98FB98
Shades
Mix
DarkOrchid
#9932CC
Shades
Mix
YellowGreen
#9ACD32
Shades
Mix
Sienna
#A0522D
Shades
Mix
Brown
#A52A2A
Shades
Mix
DarkGray
#A9A9A9
Shades
Mix
LightBlue
#ADD8E6
Shades
Mix
GreenYellow
#ADFF2F
Shades
Mix
PaleTurquoise
#AFEEEE
Shades
Mix
LightSteelBlue
#B0C4DE
Shades
Mix
PowderBlue
#B0E0E6
Shades
Mix
FireBrick
#B22222
Shades
Mix
DarkGoldenRod
#B8860B
Shades
Mix
MediumOrchid
#BA55D3
Shades
Mix
RosyBrown
#BC8F8F
Shades
Mix
DarkKhaki
#BDB76B
Shades
Mix
Silver
#C0C0C0
Shades
Mix
MediumVioletRed
#C71585
Shades
Mix
IndianRed
#CD5C5C
Shades
Mix
Peru
#CD853F
Shades
Mix
Chocolate
#D2691E
Shades
Mix
Tan
#D2B48C
Shades
Mix
LightGray
#D3D3D3
Shades
Mix
Thistle
#D8BFD8
Shades
Mix
Orchid
#DA70D6
Shades
Mix
GoldenRod
#DAA520
Shades
Mix
PaleVioletRed
#DB7093
Shades
Mix
Crimson
#DC143C
Shades
Mix
Gainsboro
#DCDCDC
Shades
Mix
Plum
#DDA0DD
Shades
Mix
BurlyWood
#DEB887
Shades
Mix
LightCyan
#E0FFFF
Shades
Mix
Lavender
#E6E6FA
Shades
Mix
DarkSalmon
#E9967A
Shades
Mix
Violet
#EE82EE
Shades
Mix
PaleGoldenRod
#EEE8AA
Shades
Mix
LightCoral
#F08080
Shades
Mix
Khaki
#F0E68C
Shades
Mix
AliceBlue
#F0F8FF
Shades
Mix
HoneyDew
#F0FFF0
Shades
Mix
Azure
#F0FFFF
Shades
Mix
SandyBrown
#F4A460
Shades
Mix
Wheat
#F5DEB3
Shades
Mix
Beige
#F5F5DC
Shades
Mix
WhiteSmoke
#F5F5F5
Shades
Mix
MintCream
#F5FFFA
Shades
Mix
GhostWhite
#F8F8FF
Shades
Mix
Salmon
#FA8072
Shades
Mix
AntiqueWhite
#FAEBD7
Shades
Mix
Linen
#FAF0E6
Shades
Mix
LightGoldenRodYellow
#FAFAD2
Shades
Mix
OldLace
#FDF5E6
Shades
Mix
Red
#FF0000
Shades
Mix
Fuchsia
#FF00FF
Shades
Mix
Magenta
#FF00FF
Shades
Mix
DeepPink
#FF1493
Shades
Mix
OrangeRed
#FF4500
Shades
Mix
Tomato
#FF6347
Shades
Mix
HotPink
#FF69B4
Shades
Mix
Coral
#FF7F50
Shades
Mix
DarkOrange
#FF8C00
Shades
Mix
LightSalmon
#FFA07A
Shades
Mix
Orange
#FFA500
Shades
Mix
LightPink
#FFB6C1
Shades
Mix
Pink
#FFC0CB
Shades
Mix
Gold
#FFD700
Shades
Mix
PeachPuff
#FFDAB9
Shades
Mix
NavajoWhite
#FFDEAD
Shades
Mix
Moccasin
#FFE4B5
Shades
Mix
Bisque
#FFE4C4
Shades
Mix
MistyRose
#FFE4E1
Shades
Mix
BlanchedAlmond
#FFEBCD
Shades
Mix
PapayaWhip
#FFEFD5
Shades
Mix
LavenderBlush
#FFF0F5
Shades
Mix
SeaShell
#FFF5EE
Shades
Mix
Cornsilk
#FFF8DC
Shades
Mix
LemonChiffon
#FFFACD
Shades
Mix
FloralWhite
#FFFAF0
Shades
Mix
Snow
#FFFAFA
Shades
Mix
Yellow
#FFFF00
Shades
Mix
LightYellow
#FFFFE0
Shades
Mix
Ivory
#FFFFF0
Shades
Mix
White
#FFFFFF
Shades
Mix
HTML Scripts
Previous
Next Chapter
JavaScripts make HTML pages more dynamic and interactive.
Example
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
Try it Yourself
To learn all about JavaScript, visit our JavaScript Tutorial!
Example
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
Try it Yourself
Exercise 2
Exercise 3
Exercise 4
HTML Head
Previous
Next Chapter
Example
<!DOCTYPE html>
<head>
<title>Page Title</title>
</head>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
Try it Yourself
W3Schools does 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> and <body> can crash badly written DOM and XML software.
Finally, omitting <body> can produce errors in older browsers (IE9).
Omitting <head>
In the HTML5 standard, the <head> tag can also be omitted.
By default, browsers will add all elements before <body>, to a default <head> element.
You can reduce the complexity of HTML, by omitting the <head> tag:
Example
<!DOCTYPE html>
<html>
<title>Page Title</title>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself
Omitting tags is unfamiliar to web developers. It needs time to be established as a guideline.
Example
<!DOCTYPE html>
<html>
<title>Page Title</title>
<body>
The content of the document......
</body>
</html>
Try it Yourself
Example
<style>
body {background-color:yellow;}
p {color:blue;}
</style>
Try it Yourself
Example
<link rel="stylesheet" href="mystyle.css">
Try it Yourself
Example
<meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript">
Define a description of your web page:
Example
<meta name="description" content="Free Web tutorials on HTML and CSS">
Define the character set used:
Example
<meta charset="UTF-8">
Define the author of a page:
Example
<meta name="author" content="Hege Refsnes">
Try it Yourself
Refresh document every 30 seconds:
Example
<meta http-equiv="refresh" content="30">
Example
<script>
function myFunction {
document.getElementById("demo").innerHTML = "Hello JavaScript!";
}
</script>
Try it Yourself
To learn all about JavaScript, visit our JavaScript Tutorial!
Example
<base href="http://www.w3schools.com/images/" target="_blank">
Try it Yourself
Description
Defines information about the document
Defines the title of a document
Defines a default address or a default target for all links on a page
Defines the relationship between a document and an external resource
Defines metadata about an HTML document
Defines a client-side script
Defines style information for a document
HTML Entities
Previous
Next Chapter
Reserved characters in HTML must be replaced with character entities.
Characters, not present on your keyboard, can also be replaced by entities.
HTML Entities
Some characters are reserved in HTML.
If you use the less than (<) or greater than (>) signs in your text, the browser might mix them with tags.
Character entities are used to display reserved characters in HTML.
A character entity looks like this:
&entity_name;
OR
&#entity_number;
To display a less than sign we must write: < or <
The advantage of using an entity name, instead of a number, is that the name is easier to remember.
The disadvantage is that browsers may not support all entity names, but the support for numbers is
good.
Description
 
less than
<
greater than
>
ampersand
&
cent
¢
pound
£
yen
¥
euro
€
copyright
©
registered trademark ®
<
>
&
¢
£
¥
€
©
®
Character
a
à
a
á
a
â
a
ã
O
Ò
Construct
Result
a
a
a
a
O
O
O
O
Ó
Ô
Õ
O
O
O
You will see more HTML symbols in the next chapter of this tutorial.
HTML Symbols
Previous
Next Chapter
Example
<p>I will display €</p>
<p>I will display €</p>
<p>I will display €</p>
Number
Entity
∀ ∀
∂ ∂
∃ ∃
∅ ∅
∇ ∇
∈ ∈
Description
FOR ALL
PARTIAL DIFFERENTIAL
THERE EXISTS
EMPTY SETS
NABLA
ELEMENT OF
∉ ∉
∋ ∋
∏ ∏
∑ ∑
NOT AN ELEMENT OF
CONTAINS AS MEMBER
N-ARY PRODUCT
N-ARY SUMMATION
Α Α
Β Β
Γ Γ
Δ Δ
Ε Ε
Ζ Ζ
Description
GREEK CAPITAL LETTER ALPHA
GREEK CAPITAL LETTER BETA
GREEK CAPITAL LETTER GAMMA
GREEK CAPITAL LETTER DELTA
GREEK CAPITAL LETTER EPSILON
GREEK CAPITAL LETTER ZETA
© ©
® ®
€ €
™ ™
← ←
↑ ↑
→ →
↓ ↓
♠ ♠
♣ ♣
♥ ♥
♦ ♦
Description
COPYRIGHT SIGN
REGISTERED SIGN
EURO SIGN
TRADEMARK
LEFTWARDS ARROW
UPWARDS ARROW
RIGHTWARDS ARROW
DOWNWARDS ARROW
BLACK SPADE SUIT
BLACK CLUB SUIT
BLACK HEART SUIT
BLACK DIAMOND SUIT
To display an HTML page correctly, a web browser must know the character set (character encoding) to
use.
For HTML4:
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
For HTML5:
<meta charset="UTF-8">
If a browser detects ISO-8859-1 in a web page, it defaults to ANSI, because ANSI is identical to
ISO-8859-1 except that ANSI has 32 extra characters.
Description
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
$
%
&
'
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
#
$
%
&
'
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
#
$
%
&
'
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
#
$
%
&
'
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
number sign
dollar sign
percent sign
ampersand
apostrophe
left parenthesis
right parenthesis
asterisk
plus sign
comma
hyphen-minus
full stop
solidus
digit zero
digit one
digit two
digit three
digit four
digit five
digit six
digit seven
digit eight
digit nine
colon
semicolon
less-than sign
equals sign
greater-than sign
question mark
commercial at
Latin capital letter A
Latin capital letter B
Latin capital letter C
Latin capital letter D
Latin capital letter E
Latin capital letter F
Latin capital letter G
Latin capital letter H
Latin capital letter I
Latin capital letter J
Latin capital letter K
Latin capital letter L
Latin capital letter M
Latin capital letter N
Latin capital letter O
Latin capital letter P
Latin capital letter Q
Latin capital letter R
Latin capital letter S
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
DEL
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
T
U
V
W
X
Y
Z
[
\
]
^
_
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
horizontal ellipsis
dagger
double dagger
modifier letter circumflex accent
per mille sign
Latin capital letter S with caron
single left-pointing angle quotation mark
Latin capital ligature OE
NOT USED
Latin capital letter Z with caron
NOT USED
NOT USED
left single quotation mark
right single quotation mark
left double quotation mark
right double quotation mark
bullet
en dash
em dash
small tilde
trade mark sign
Latin small letter s with caron
single right-pointing angle quotation mark
Latin small ligature oe
NOT USED
Latin small letter z with caron
Latin capital letter Y with diaeresis
no-break space
inverted exclamation mark
cent sign
pound sign
currency sign
yen sign
broken bar
section sign
diaeresis
copyright sign
feminine ordinal indicator
left-pointing double angle quotation mark
not sign
soft hyphen
registered sign
macron
degree sign
plus-minus sign
superscript two
superscript three
acute accent
micro sign
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
O
O
a
a
O
O
a
a
O
O
a
a
pilcrow sign
middle dot
cedilla
superscript one
masculine ordinal indicator
right-pointing double angle quotation mark
vulgar fraction one quarter
vulgar fraction one half
vulgar fraction three quarters
inverted question mark
Latin capital letter A with grave
Latin capital letter A with acute
Latin capital letter A with circumflex
Latin capital letter A with tilde
Latin capital letter A with diaeresis
Latin capital letter A with ring above
Latin capital letter AE
Latin capital letter C with cedilla
Latin capital letter E with grave
Latin capital letter E with acute
Latin capital letter E with circumflex
Latin capital letter E with diaeresis
Latin capital letter I with grave
Latin capital letter I with acute
Latin capital letter I with circumflex
Latin capital letter I with diaeresis
Latin capital letter Eth
Latin capital letter N with tilde
Latin capital letter O with grave
Latin capital letter O with acute
Latin capital letter O with circumflex
Latin capital letter O with tilde
Latin capital letter O with diaeresis
multiplication sign
Latin capital letter O with stroke
Latin capital letter U with grave
Latin capital letter U with acute
Latin capital letter U with circumflex
Latin capital letter U with diaeresis
Latin capital letter Y with acute
Latin capital letter Thorn
Latin small letter sharp s
Latin small letter a with grave
Latin small letter a with acute
Latin small letter a with circumflex
Latin small letter a with tilde
Latin small letter a with diaeresis
Latin small letter a with ring above
Latin small letter ae
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
Example
scheme://host.domain:port/path/filename
Explanation:
Used for
Common web pages. Not encrypted
Secure web pages. Encrypted
Downloading or uploading files
A file on your computer
URL Encoding
URLs can only be sent over the Internet using the ASCII character-set.
Since URLs often contain characters outside the ASCII set, the URL has to be converted into ASCII.
URL encoding converts characters into a format that can be transmitted over the Internet.
URL encoding replaces non ASCII characters with a "%" followed by hexadecimal digits.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign, or %20.
Try It Yourself
Hello Gnter
If you click "Submit", the browser will URL encode the input before it is sent to the server.
A page at the server will display the received input.
Try some other input and click Submit again.
%80
%E2%82%AC
%A3
%C2%A3
%A9
%AE
%C0
%C1
%C2
%C3
%C4
%C5
%C2%A9
%C2%AE
%C3%80
%C3%81
%C3%82
%C3%83
%C3%84
%C3%85
For a complete reference of all URL encodings, visit our URL Encoding Reference.
What Is XHTML?
Why XHTML?
Many pages on the internet contain "bad" HTML.
This HTML code works fine in most browsers (even if it does not follow the HTML rules):
<html>
<head>
<title>This is bad HTML</title>
<body>
<h1>Bad HTML
<p>This is a paragraph
</body>
Today's market consists of different browser technologies. Some browsers run on computers, and some
browsers run on mobile phones or other small devices. Smaller devices often lack the resources or power
to interpret "bad" markup.
XML is a markup language where documents must be marked up correctly (be "well-formed").
If you want to study XML, please read our XML tutorial.
XHTML Elements
XHTML Attributes
</html>
<body>
<p>This is a paragraph</p>
</body>
Previous
Next Chapter
WEB HOSTING
UK Reseller Hosting
WEB BUILDING
FREE Website BUILDER Free HTML5 Templates
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, and XML Certifications
COLOR PICKER
HTML Forms
Previous
Next Chapter
Example
<form>
.
form elements
.
</form>
HTML forms contain form elements.
Form elements are different types of input elements, checkboxes, radio buttons, submit buttons, and more.
Text Input
<input type="text"> defines a one-line input field for text input:
Example
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
This is how it will look like in a browser:
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.
Example
<form>
<input type="radio" name="sex" value="male" checked>Male
<br>
<input type="radio" name="sex" value="female">Female
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
Male
Female
Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
First name:
Mickey
Last name:
Mouse
POST offers better security because the submitted data is not visible in the page address.
Example
<form action="action_page.php">
First name:<br>
<input type="text" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself
Example
<form action="action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit"></fieldset>
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
Personal information: First name:
Mickey
Last name:
Mouse
Example
<form action="action_page.php" method="GET" target="_blank" accept-charset="UTF-8"
enctype="application/x-www-form-urlencoded" autocomplete="off" novalidate>
.
form elements
.
</form>
Here is the list of <form> attributes:
Attribute
accept-charset
action
autocomplete
enctype
method
name
novalidate
target
Description
Specifies the charset used in the submitted form (default: the page charset).
Specifies an address (url) where to submit the form (default: the submitting page).
Specifies if the browser should autocomplete the form (default: on).
Specifies the encoding of the submitted data (default: is url-encoded).
Specifies the HTTP method used when submitting the form (default: GET).
Specifies a name used to identify the form (for DOM usage: document.forms.name).
Specifies that the browser should not validate the form.
Specifies the target of the address in the action attribute (default: _self).
More Examples
Send e-mail from a form
How to send e-mail from a form.
The <input> element can vary in many ways, depending on the type attribute.
All HTML input types are covered in the next chapter.
Example
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Try it Yourself
The <option> elements defines the options to select.
The list will normally show the first item as selected.
You can add a selected attribute to define a predefined option.
Example
<option value="fiat" selected>Fiat</option>
Try it Yourself
Example
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
Example
<button type="button" onclick="alert('Hello World!')">Click Me!</button>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
<datalist>
<keygen>
<output>
By default, browsers do not display unknown elements. New elements will not destroy your page.
Example
An <input> element with pre-defined values in a <datalist>:
<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>
Try it Yourself
Example
A form with a keygen field:
<form action="action_page.php">
Username: <input type="text" name="user">
Encryption: <keygen name="security">
<input type="submit">
</form>
Try it Yourself
Example
Perform a calculation and show the result in an <output> element:
<form action="action_page.asp"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
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>
Try it Yourself
Description
Defines an HTML form for user input
Defines an input control
Defines a multiline input control (text area)
Defines a label for an <input> element
Groups related elements in a form
Defines a caption for a <fieldset> element
Defines a drop-down list
Defines a group of related options in a drop-down list
Defines an option in a drop-down list
Defines a clickable button
Specifies a list of pre-defined options for input controls
Defines a key-pair generator field (for forms)
Defines the result of a calculation
Input Types
This chapter describes the input types of the <input> element.
Example
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
First name:
Last name:
Example
<form>
User name:<br>
<input type="text" name="username">
<br>
User password:<br>
<input type="password" name="psw">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
User name:
User password:
The characters in a password field are masked (shown as asterisks or circles).
Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
First name:
Mickey
Last name:
Mouse
If you omit the submit button's value attribute, the button will get a default text:
Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit">
</form>
Try it Yourself
Example
<form>
<input type="radio" name="sex" value="male" checked>Male
<br>
Example
<form>
<input type="checkbox" name="vehicle" value="Bike">I have a bike
<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
I have a bike
I have a car
Example
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
Try it Yourself
This is how the HTML code above will be displayed in a browser:
color
date
datetime
datetime-local
email
month
number
range
search
tel
time
url
week
Input types, not supported by old web browsers, will behave as input type text.
Example
<form>
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
</form>
Try it Yourself
Input Restrictions
Here is a list of some common input restrictions (some are new in HTML5):
Attribute
disabled
max
maxlength
min
pattern
readonly
Description
Specifies that an input field should be disabled
Specifies the maximum value for an input field
Specifies the maximum number of character for an input field
Specifies the minimum value for an input field
Specifies a regular expression to check the input value against
Specifies that an input field is read only (cannot be changed)
required
size
step
value
You will learn more about input restrictions in the next chapter.
Example
<form>
Quantity:
<input type="number" name="points" min="0" max="100" step="10" value="30">
</form>
Try it Yourself
Example
<form>
Birthday:
<input type="date" name="bday">
</form>
Try it Yourself
You can add restrictions to the input:
Example
<form>
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31"><br>
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02"><br>
</form>
Try it Yourself
Example
<form>
Select your favorite color:
<input type="color" name="favcolor">
</form>
Try it Yourself
Example
<form>
<input type="range" name="points" min="0" max="10">
</form>
Try it Yourself
You can use the following attributes to specify restrictions: min, max, step, value.
Example
<form>
Birthday (month and year):
Example
<form>
Select a week:
<input type="week" name="week_year">
</form>
Try it Yourself
Example
<form>
Select a time:
<input type="time" name="usr_time">
</form>
Try it Yourself
Example
<form>
Birthday (date and time):
<input type="datetime" name="bdaytime">
</form>
Try it Yourself
The input type datetime is removed from the HTML standard. Use datetime-local instead.
Example
<form>
Birthday (date and time):
<input type="datetime-local" name="bdaytime">
</form>
Try it Yourself
Example
<form>
E-mail:
<input type="email" name="email">
</form>
Try it Yourself
Example
<form>
Search Google:
<input type="search" name="googlesearch">
</form>
Try it Yourself
Example
<form>
Telephone:
<input type="tel" name="usrtel">
</form>
Try it Yourself
Example
<form>
Add your homepage:
<input type="url" name="homepage">
</form>
Try it Yourself
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" readonly>
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
The readonly attribute does not need a value. It is the same as writing readonly="readonly".
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" disabled>
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
The disabled attribute does not need a value. It is the same as writing disabled="disabled".
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" size="40">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
Example
<form action="">
First name:<br>
<input type="text" name="firstname" maxlength="10">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
With a maxlength attribute, the input control will not accept more than the allowed number of characters.
The attribute does not provide any feedback. If you want to alert the user, you must write JavaScript code.
Input restrictions are not foolproof. JavaScript provides many ways to add illegal input.
To safely restrict input, restrictions must be checked by the receiver (the server) as well.
HTML5 Attributes
HTML5 added the following attributes for <input>:
autocomplete
autofocus
form
formaction
formenctype
formmethod
formnovalidate
formtarget
height and width
list
min and max
multiple
pattern (regexp)
placeholder
required
step
autocomplete
novalidate
Example
An HTML form with autocomplete on (and off for one input field):
Example
Indicates that the form is not to be validated on submit:
<form action="action_page.php" novalidate>
E-mail: <input type="email" name="user_email">
<input type="submit">
</form>
Try it Yourself
Example
Let the "First name" input field automatically get focus when the page loads:
First name:<input type="text" name="fname" autofocus>
Try it Yourself
The form attribute specifies one or more forms an <input> element belongs to.
Tip: To refer to more than one form, use a space-separated list of form ids.
Example
An input field located outside the HTML form (but still a part of the form):
<form action="action_page.php" id="form1">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
Last name: <input type="text" name="lname" form="form1">
Try it Yourself
Example
An HTML form with two submit buttons, with different actions:
<form action="action_page.php">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formaction="demo_admin.asp"
value="Submit as admin">
</form>
Try it Yourself
Example
Send form-data that is default encoded (the first submit button), and encoded as "multipart/form-data" (the
second submit button):
<form action="demo_post_enctype.asp" method="post">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
<input type="submit" formenctype="multipart/form-data"
value="Submit as Multipart/form-data">
</form>
Try it Yourself
Example
The second submit button overrides the HTTP method of the form:
<form action="action_page.php" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
<input type="submit" formmethod="post" formaction="demo_post.asp"
value="Submit using POST">
</form>
Try it Yourself
Example
A form with two submit buttons (with and without validation):
<form action="action_page.php">
E-mail: <input type="email" name="userid"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formnovalidate value="Submit without validation">
</form>
Try it Yourself
Example
A form with two submit buttons, with different target windows:
<form action="action_page.php">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit as normal">
<input type="submit" formtarget="_blank"
value="Submit to a new window">
</form>
Try it Yourself
Example
Define an image as the submit button, with height and width attributes:
<input type="image" src="img_submit.gif" alt="Submit" width="48" height="48">
Try it Yourself
Example
An <input> element with pre-defined values in a <datalist>:
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
Try it Yourself
Example
<input> elements with min and max values:
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31">
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02">
Example
A file upload field that accepts multiple values:
Select images: <input type="file" name="img" multiple>
Try it Yourself
Example
An input field that can contain only three letters (no numbers or special characters):
Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter
country code">
Try it Yourself
The hint is displayed in the input field before the user enters a value.
The placeholder attribute works with the following input types: text, search, url, tel, email, and password.
Example
An input field with a placeholder text:
<input type="text" name="fname" placeholder="First name">
Try it Yourself
Example
A required input field:
Username: <input type="text" name="usrname" required>
Try it Yourself
Example
Exercise 2
Exercise 3
Exercise 4
HTML5 Introduction
Previous
Next Chapter
HTML5 Example:
<!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.
HTML Geolocation
HTML Drag and Drop
HTML Local Storage
HTML Application Cache
HTML Web Workers
HTML SSE
Local storage is a powerful replacement for cookies.
Use instead
<abbr>
<object>
CSS
CSS
CSS
<ul>
CSS
CSS
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 web, there have been many versions of HTML:
Version
Tim Berners-Lee invented www
Tim Berners-Lee invented HTML
Year
1989
1991
1993
1995
1997
1999
2000
2008
2012
2014
Tim Berners-Lee invented the "World Wide Web" in 1989, and the Internet took off in the 1990s.
From 1991 to 1998, HTML developed from version 1 to version 4.
In 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, WHATWG (Web Hypertext Application Technology Working Group) was formed in response to
slow W3C development, and W3C's decision to close down the development of HTML, in favor of
XHTML.
WHATWG wanted to develop HTML, consistent with how the web was used, while being backward
compatible with older versions of HTML.
In the period 2004-2006, the WHATWG initiative gained support by the major browser vendors.
In 2006, W3C announced that they would support WHATWG.
In 2008, the first HTML5 public draft was released
In 2012, WHATWG and W3C decided on a separation:
WHATWG will develop HTML as a "Living Standard".
A living standard is never fully complete, but always updated and improved. New features can be added,
but old functionality can not be removed.
The WHATWG Living Standard was published in 2012, and is continuously updated.
W3C will develop a definitive HTML5 and XHTML5 standard, as a "snapshot" of WHATWG.
The W3C HTML5 recommendation was released 28. October 2014.
Example
header, section, footer, aside, nav, main, article, figure {
display: block;
}
Example
<!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>
</body>
</html>
Try it Yourself
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.
An HTML5 Skeleton
Example
<!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_geolocation.asp">HTML5 Geolocation</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>
Try it Yourself
Description
Defines an article in the document
Defines content aside from the page content
Defines a part of text that might be formatted in a different direction from other text
Defines additional details that the user can view or hide
Defines a dialog box or window
Defines a caption for a <figure> element
Defines self-contained content, like illustrations, diagrams, photos, code listings, etc.
<footer>
<header>
<main>
<mark>
<menuitem>
<meter>
<nav>
<progress>
<rp>
<rt>
<ruby>
<section>
<summary>
<time>
<wbr>
step
Learn all about old and new input types in HTML Input Types.
Learn all about input attributes in HTML Input Attributes.
Example
<input type="text" value="John Doe" disabled>
<input type="text" value=John>
<input type="text" value="John Doe">
<input type="text" value='John Doe'>
In HTML5, all 4 syntaxes may be used, depending on what is needed for the attribute.
HTML5 Graphics
Tag
Description
<canvas> Defines graphic drawing using JavaScript
<svg>
Defines graphic drawing using SVG
Read more about HTML5 Canvas.
Read more about HTML5 SVG.
Description
Defines sound or music content
Defines containers for external applications (like plug-ins)
Defines sources for <video> and <audio>
Defines tracks for <video> and <audio>
Defines video or movie content
Browser Support
<article>
<aside>
<details>
<figcaption>
<figure>
<footer>
<header>
<main>
<mark>
<nav>
<section>
<summary>
<time>
Example
<section>
<h1>WWF</h1>
<p>The World Wide Fund for Nature (WWF) is....</p>
</section>
Try it Yourself
Forum post
Blog post
Newspaper article
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>
Try it Yourself
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>
Try it Yourself
Example
<footer>
<p>Posted by: Hege Refsnes</p>
<p>Contact information: <a href="mailto:someone@example.com">
someone@example.com</a>.</p>
</footer>
Try it Yourself
Example
<nav>
<a href="/html/">HTML</a> |
<a href="/css/">CSS</a> |
<a href="/js/">JavaScript</a> |
<a href="/jquery/">jQuery</a>
</nav>
Try it Yourself
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
Example
<figure>
<img src="pic_mountain.jpg" alt="The Pulpit Rock" width="304" height="228">
<figcaption>Fig1. - The Pulpit Rock, Norway.</figcaption>
</figure>
Try it Yourself
The <img> element defines the image, the <figcaption> element defines the caption.
Description
Defines an article
<aside>
<details>
<figcaption>
<figure>
<footer>
<header>
<main>
<mark>
<nav>
<section>
<summary>
<time>
HTML5 Migration
Previous
Next Chapter
Typical HTML5
<header>
<nav>
<section>
<article>
<footer>
background-color:#ddd;
}
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,footer,section,article {
border:1px solid grey;margin:5px;margin-bottom:15px;padding:8px;background-color:white;
}
header,footer {
color:white;background-color:#444;margin-bottom:5px;
}
section {
background-color:#ddd;
}
nav ul {
margin:0;padding:0;
}
nav ul li {
display:inline; margin:5px;
}
Try it Yourself
Example
<!DOCTYPE html>
<html lang="en">
<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,footer,section,article {
border:1px solid grey;
margin:5px;margin-bottom:15px;padding:8px;
background-color:white;
}
header,footer {
color:white;background-color:#444;margin-bottom:5px;
}
section {
background-color:#ddd;
}
nav ul {
margin:0;padding:0;
}
nav ul li {
display:inline; margin:5px;
}
</style>
<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>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
lurum hurum turum.</p>
<p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
lurum hurum turum.</p>
<p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
lurum hurum turum.</p>
</article>
<article>
<h2>News Article</h2>
<p>Ipsum lurum hurum turum ipsum lurum hurum turum ipsum lurum hurum turum ipsum
<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
Bad:
<SECTION>
<p>This is a paragraph.</p>
</SECTION>
Very Bad:
<Section>
<p>This is a paragraph.</p>
</SECTION>
Good:
<section>
<p>This is a paragraph.</p>
</section>
Looking bad:
<div CLASS="menu">
Looking good:
<div class="menu">
Image Attributes
Always use the alt attribute with images. It is important when the image cannot be viewed.
<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">
Always define image size. It reduces flickering because the browser can reserve space for images before
they are loaded.
<img src="html5.gif" alt="HTML5" style="width:128px;height:128px">
Unnecessary:
<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>
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>LondonA</li>
<li>Paris</li>
<li>Tokyo</li>
</ol>
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>?
In the HTML5 standard, the <head> tag can also be omitted.
By default, browsers will add all elements before <body>, to a default <head> element.
You can reduce the complexity of HTML, by omitting the <head> tag:
Example
<!DOCTYPE html>
<html>
<title>Page Title</title>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself
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
Short comments should be written on one line, with a space after <!-- and a space before -->:
<!-- This is a comment -->
Long comments, spanning many lines, should be written with <!-- and --> on separate lines:
<!-This is a long comment example. This is a long comment example. This is a long comment example.
This is a long comment example. This is a long comment example. This is a long comment example.
-->
Long comments are easier to observe, if they are indented 2 spaces.
Style Sheets
Use simple syntax for linking 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.into {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 2 spaces of indentation.
Use colon plus one space between each property and its value.
Use space after each comma or semicolon.
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.
Adding a space after a comma, or a semicolon, is a general rule in all types of writing.
<script src="myscript.js">
File Extensions
HTML files should have a .html extension (not .htm).
CSS files should have a .css extension.
JavaScript files should have a .js extension.
HTML5 Canvas
Previous
Next Chapter
Browser Support
The numbers in the table specify the first browser version that fully supports the <canvas> element.
Element
<canvas> 4.0
9.0
2.0
3.1
9.0
Canvas Examples
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:
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75);
</script>
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
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10,10,150,80);
Try it Yourself
Draw Image
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img,10,10);
Try it Yourself
HTML5 SVG
Previous
Next Chapter
What is SVG?
Browser Support
The numbers in the table specify the first browser version that fully supports the <svg> element.
Element
<svg> 4.0
9.0
3.0
3.2
10.1
SVG Circle
Example
<!DOCTYPE html>
<html>
<body>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
</body>
</html>
Try it Yourself
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>
Try it Yourself
Example
<svg width="400" height="180">
<rect x="50" y="20" rx="20" ry="20" width="150" height="150"
style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
</svg>
Try it Yourself
SVG Star
Sorry, your browser does not support inline SVG.
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>
Try it Yourself
SVG Logo
SVG Sorry, your browser does not support inline 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>
Try it Yourself
Resolution dependent
No support for event handlers
Poor text rendering capabilities
You can save the resulting image as .png or
.jpg
SVG
Resolution independent
Support for event handlers
Best suited for applications with large
rendering areas (Google Maps)
Slow rendering if complex (anything that
uses the DOM a lot will be slow)
Not suited for game applications
HTML Multimedia
Previous
Next Chapter
Multimedia on the web, is sound, music, videos, movies, and animations.
What is Multimedia?
Multimedia comes in many different formats. It can be almost anything you can hear or see.
Examples: Pictures, music, sound, videos, records, films, animations, and more.
Web pages often contains 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 even support for pictures!
The support for sounds, animations, and videos is handled differently by various browsers. Different types
and formats are supported, and some formats requires 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 sounds or videos) are stored in media files.
The most common way to discover the type of a file, is to look at the file extension. When a browser sees
the file extension .htm or .html, it will treat the file as an HTML file. The .xml extension indicates an
XML file, and the .css extension indicates a style sheet file. Pictures are recognized by extensions like .gif,
.png and .jpg.
Multimedia files also have their own formats and different extensions like: .swf, .wav, .mp3, .mp4,
.mpg, .wmv, and .avi.
File
Description
MPEG. Developed by the Moving Pictures Expert Group. The first popular video
.mpg
MPEG
format on the web. Used to be supported by all browsers, but it is not supported in
.mpeg
HTML5 (See MP4).
AVI (Audio Video Interleave). Developed by Microsoft. Commonly used in video
AVI
.avi
cameras and TV hardware. Plays well on Windows computers, but not in web
browsers.
WMV (Windows Media Video). Developed by Microsoft. Commonly used in video
WMV
.wmv cameras and TV hardware. Plays well on Windows computers, but not in web
browsers.
QuickTime. Developed by Apple. Commonly used in video cameras and TV hardware.
QuickTime .mov
Plays well on Apple computers, but not in web browsers. (See MP4)
RealVideo .rm
RealVideo. Developed by Real Media to allow video streaming with low bandwidths.
.ram
.swf
.flv
.ogg
Flash
Ogg
WebM
MPEG-4
or MP4
It is still used for online video and Internet TV, but does not play in web browsers.
Flash. Developed by Macromedia. Often requires an extra component (plug-in) to play
in web browsers.
Theora Ogg. Developed by the Xiph.Org Foundation. Supported by HTML5.
WebM. Developed by the web giants, Mozilla, Opera, Adobe, and Google. Supported
.webm
by HTML5.
MP4. Developed by the Moving Pictures Expert Group. Based on QuickTime.
.mp4 Commonly used in newer video cameras and TV hardware. Supported by all HTML5
browsers. Recommended by YouTube.
Only MP4, WebM, and Ogg video is supported by the newest HTML5 standard.
Sound 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.
Format
File
MIDI
.mid
.midi
RealAudio
.rm
.ram
WMA
.wma
AAC
.aac
WAV
.wav
Ogg
.ogg
MP3
.mp3
MP4
.mp4
Description
MIDI (Musical Instrument Digital Interface). Main format for all electronic music
devices like synthesizers and PC sound cards. MIDI files do not contain sound, but
digital notes that can be played by electronics. Plays well on all computers and music
hardware, but not in web browsers.
RealAudio. Developed by Real Media to allow streaming of audio with low
bandwidths. Does not play in web browsers.
WMA (Windows Media Audio). Developed by Microsoft. Commonly used in music
players. Plays well on Windows computers, but not in web browsers.
AAC (Advanced Audio Coding). Developed by Apple as the default format for iTunes.
Plays well on Apple computers, but not in web browsers.
WAV. Developed by IBM and Microsoft. Plays well on Windows, Macintosh, and
Linux operating systems. Supported by HTML5.
Ogg. Developed by the Xiph.Org Foundation. Supported by HTML5.
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 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 is supported by the newest HTML5 standard.
Browser Support
The numbers in the table specify the first browser version that fully supports the <video> element.
Element
<video> 4.0
9.0
3.5
4.0
10.5
Example
<video width="320" height="240" 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
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 browser does not know the size of the video. The effect will be that the
page will change (or flicker) while the video loads.
Text between the <video> and </video> tags will only display in browsers that do not support the <video>
element.
Multiple <source> elements can link to different video files. The browser will use the first recognized
format.
Example
<video width="320" height="240" autoplay>
<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 autoplay attribute does not work in Safari and Opera, or in mobile devices like iPad and iPhone.
MP4
YES
NO
YES
YES
YES
YES
YES
NO
YES (from Opera 25) YES
WebM
Ogg
NO
YES
YES
NO
YES
Tag
Description
<video>
<source>
<track>
HTML5 Audio
Previous
Next Chapter
HTML5 provides a standard for playing audio files.
Browser Support
The numbers in the table specify the first browser version that fully supports the <audio> element.
Element
<audio> 4.0
9.0
3.5
4.0
10.5
Example
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
Try it yourself
The controls attribute adds audio controls, like play, pause, and volume.
Text between the <audio> and </audio> tags will display in browsers that do not support the <audio>
element.
Multiple <source> elements can link to different audio files. The browser will use the first recognized
format.
MP3
YES
YES
YES
YES
YES
Wav
NO
YES
YES
YES
YES
Ogg
NO
YES
YES
NO
YES
Description
Defines sound content
Defines multiple media resources for media elements, such as <video> and
<audio>
HTML Plug-ins
Previous
Next Chapter
The purpose of a plug-in, is to extend the functionality of the HTML browser.
Example
<object width="400" height="50" data="bookmark.swf"></object>
Try it Yourself
The <object> element can also be used to include HTML in HTML:
Example
<object width="100%" height="500px" data="snippet.html"></object>
Try it Yourself
Or images if you like:
Example
<object data="audi.jpeg"></object>
Try it Yourself
Example
<embed width="400" height="50" src="bookmark.swf">
Try it Yourself
Note that the <embed> element does not have a closing tag. It can not contain alternative text.
The <embed> element can also be used to include HTML in HTML:
Example
<embed width="100%" height="500px" src="snippet.html">
Try it Yourself
Or images if you like:
Example
<embed src="audi.jpeg">
Try it Yourself
HTML5 Geolocation
Previous
Next Chapter
HTML Geolocation is used to locate a user's position.
Browser Support
The numbers in the table specify the first browser version that fully supports Geolocation.
API
Geolocation 5.0
9.0
3.5
5.0
16.0
Note: Geolocation is much more accurate for devices with GPS, like iPhone.
Example
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
Try it yourself
Example explained:
The example above is a very basic Geolocation script, with no error handling.
Example
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
Try it yourself
Error Codes:
Example
function showPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center=
"+latlon+"&zoom=14&size=400x300&sensor=false";
Location-specific Information
This page demonstrated how to show a user's position on a map. However, Geolocation is also very useful
for location-specific information.
Examples:
Description
The latitude as a decimal number
The longitude as a decimal number
The accuracy of position
The altitude in meters above the mean sea level
The altitude accuracy of position
The heading as degrees clockwise from North
The speed in meters per second
The date/time of the response
Example
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
Try it yourself
Browser Support
The numbers in the table specify the first browser version that fully supports Drag and Drop.
API
Drag and Drop 4.0
9.0
3.5
6.0
12.0
Example
<!DOCTYPE HTML>
<html>
<head>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<img id="drag1" src="img_logo.gif" draggable="true"
ondragstart="drag(event)" width="336" height="69">
</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").
Call preventDefault() to prevent the browser default handling of the data (default is open as link on
drop)
Get the dragged data with the dataTransfer.getData() 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
More Examples
Browser Support
The numbers in the table specify the first browser version that fully supports Local Storage.
API
Web Storage 4.0
8.0
3.5
4.0
11.5
Before using local storage, check browser support for localStorage and sessionStorage:
if(typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
} else {
// Sorry! No Web Storage support..
}
Example
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
Try it Yourself
Example explained:
Example
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
localStorage.clickcount + " time(s).";
Try it Yourself
The sessionStorage object is equal to the localStorage object, except that it stores the data for only one
session. The data is deleted when the user closes the browser window.
The following example counts the number of times a user has clicked a button, in the current session:
Example
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";
Try it Yourself
Browser Support
The numbers in the table specify the first browser version that fully supports Application Cache.
API
Application Cache 4.0
10.0
3.5
4.0
11.5
The example below shows an HTML document with a cache manifest (for offline browsing):
Example
<!DOCTYPE HTML>
<html manifest="demo.appcache">
<body>
The content of the document......
</body>
</html>
Try it Yourself
CACHE MANIFEST - Files listed under this header will be cached after they are downloaded for
the first time
NETWORK - Files listed under this header require a connection to the server, and will never be
cached
FALLBACK - Files listed under this header specifies fallback pages if a page is inaccessible
CACHE MANIFEST
The first line, CACHE MANIFEST, is required:
CACHE MANIFEST
/theme.css
/logo.gif
/main.js
The manifest file above lists three resources: a CSS file, a GIF image, and a JavaScript file. When the
manifest file is loaded, the browser will download the three files from the root directory of the web site.
Then, whenever the user is not connected to the internet, the resources will still be available.
NETWORK
The NETWORK section below specifies that the file "login.asp" should never be cached, and will not be
available offline:
NETWORK:
login.asp
An asterisk can be used to indicate that all other resources/files require an internet connection:
NETWORK:
*
FALLBACK
The FALLBACK section below specifies that "offline.html" will be served in place of all files in the
/html/ catalog, in case an internet connection cannot be established:
FALLBACK:
/html/ /offline.html
Note: The first URI is the resource, the second is the fallback.
FALLBACK:
/html/ /offline.html
Tip: Lines starting with a "#" are comment lines, but can also serve another purpose. An application's
cache is only updated when its manifest file changes. If you edit an image or change a JavaScript
function, those changes will not be re-cached. Updating the date and version in a comment line is one
way to make the browser re-cache your files.
Browser Support
The numbers in the table specify the first browser version that fully support Web Workers.
API
Web Workers 4.0
10.0
3.5
4.0
11.5
Example
Count numbers:
Try it yourself
w.onmessage = function(event){
document.getElementById("result").innerHTML = event.data;
};
When the web worker posts a message, the code within the event listener is executed. The data from the
web worker is stored in event.data.
Example
<!DOCTYPE html>
<html>
<body>
<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<br><br>
<script>
var w;
function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("demo_workers.js");
}
w.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
} else {
document.getElementById("result").innerHTML = "Sorry! No Web Worker support.";
}
}
function stopWorker() {
w.terminate();
w = undefined;
}
</script>
</body>
</html>
Try it yourself
Browser Support
The numbers in the table specify the first browser version that fully support server-sent events.
API
SSE
6.0
5.0
11.5
Example
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
Try it yourself
Example explained:
Create a new EventSource object, and specify the URL of the page sending the updates (in this
example "demo_sse.php")
Each time an update is received, the onmessage event occurs
When an onmessage event occurs, put the received data into the element with id="result"
HTML Examples
Previous
Next Chapter
HTML Basic
HTML document
HTML headings
HTML paragraphs
HTML links
HTML images
Examples explained
HTML Attributes
The title attribute
The href attribute
HTML Headings
HTML headings
HTML horizontal rules
HTML head
Examples explained
HTML Paragraphs
HTML paragraphs
More HTML paragraphs
The use of line breaks in HTML
Poem problems (some problems with HTML formatting)
How to control the line breaks and spaces with the <pre> tag
Examples explained
HTML Styles
HTML styles
HTML background color
HTML text color
HTML text font
HTML text size
HTML text alignment
Examples explained
HTML Comments
Hidden comments
Conditional comments
Comments for debugging
Examples explained
HTML CSS
HTML with inline CSS
HTML with internal CSS
HTML with external CSS
HTML with CSS fonts
HTML with CSS using the id attribute
HTML with CSS using the class attribute
HTML and CSS borders
HTML and CSS padding
HTML Links
Linking, using an absolute URL
Linking, using a relative URL
Changing the color of links
Removing the underline from links
Changing the target of a link
An image as a link
Creating a bookmark link
A link that breaks out of a frame
A mailto link
A mailto link with subject
Examples explained
HTML Images
The Mountain
An image height and width using attributes
An image height and width using CSS
An image height and width using both
An image in another folder
An image with a broken link
An image on another server
Using an image as a link
A moving image
An image map with clickable regions
A floating image
Examples explained
HTML Tables
Basic HTML tables
A table with borders
A table with collapsed borders
A table with cell padding
A table with headings
A table with left-aligned headings
Horizontal/Vertical table headings
A table with a caption
Table cells that span more than one column
Table cells that span more than one row
HTML Lists
An unordered list (default)
An unordered list with disc bullets
An unordered list with circle bullets
An unordered list with square bullets
An unordered list without bullets
An ordered list (default)
An ordered list with numbers
An ordered list with letters
An ordered list with lowercase letters
An ordered list with roman numbers
An ordered list with lowercase roman numbers
A description list
A nested list I
A nested list II
A horizontal list
A horizontal list menu
Examples explained
HTML Classes
Classing <div> elements I
Classing <div> elements II
Classing <span> elements
Examples explained
HTML Layout
HTML IFrame
Inline frame (a frame inside an HTML page)
Examples explained
HTML Scripts
Insert a script
Use of the <noscript> tag
Examples explained
HTML Forms
Form with text input
Form with radio button input
Form with text fields and a submit button
Form with a text fields without a name attribute
Grouping Form Data
Send e-mail from a form
Examples explained
Examples explained
Examples explained
Examples explained
HTML5 Canvas
Draw on the canvas with JavaScript
Draw a line with lineTo()
Draw a circle with arc()
Draw a text with fillText()
Draw a text with strokeText()
Draw a linear gradient
Draw a circular gradient
Draw an image with drawImage()
Examples explained
HTML5 SVG
SVG Cirlce
SVG Rectangle
SVG Rounded Rectangle
SVG Star
SVG Logo
Examples explained
HTML5 Media
Play Bunny
Play bear video with controls
Play bear video with autoplay
Play Horse sound with controls
Examples explained
HTML5 Geolocation
Get geolocation coordinates
Handle geolocation errors
Get geolocation with a map
Get geolocation with Google map script
Get geolocation and watch the position
Examples explained