HTML With CSS Programming
HTML With CSS Programming
With our online HTML editor, you can edit the HTML, and click on a button to view the result.
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
HTML Examples
At the end of the HTML tutorial, you can find more than 200 examples.
With our online editor, you can edit and test each example yourself.
HTML References
At W3Schools you will find complete references about tags, attributes, events, color names, entities, character-sets, URL encoding,
language codes, HTTP messages, and more.
HTML Tag Reference
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
HTML Introduction
What is HTML?
HTML is a markup language for describing web documents (web pages).
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>
Example Explained
The text between <head> and </head> provides information about the document
The text between <title> and </title> provides a title for the document
The text between <body> and </body> describes the visible page content
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
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:
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
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">
HTML Versions
Since the early days of the web, there have been many versions of HTML:
Version
Year
HTML
1991
HTML 2.0
1995
HTML 3.2
1997
HTML 4.01
1999
XHTML
2000
HTML5
2012
HTML Editors
Write HTML Using Notepad or TextEdit
HTML can be edited by using a professional HTML editor like:
Adobe Dreamweaver
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.
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
You can use either .htm or .html as file extension. There is no difference, it is up to you.
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
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>
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
HTML Links
HTML links are defined with the <a> tag:
Example
<a href="http://www.w3schools.com">This is a link</a>
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">
HTML Elements
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
<h1>
My First Heading
</h1>
<p>
My first paragraph.
</p>
<br>
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The <body> element defines the document body.
It has a start tag <body> and an end tag </body>.
The element content is two other HTML elements (<h1> and <p>).
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
The <h1> element defines a heading.
It has a start tag <h1> and an end tag </h1>.
The element content is: My First Heading.
Example
<html>
<body>
<p>This is a paragraph
<p>This is a paragraph
</body>
</html>
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
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>
Example
<a href="http://www.w3schools.com">This is a link</a>
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">
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">
Example
<a href=http://www.w3schools.com>
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
Example
<p title='John "ShotGun" Nelson'>
Or vice versa:
Example
<p title="John 'ShotGun' Nelson">
Chapter Summary
The HTML width and height attributes provide size information for images
HTML Attributes
Below is an alphabetical list of some attributes often used in HTML:
Attribute
Description
alt
disabled
href
id
src
style
title
value
A complete list, of all legal attributes for each HTML element, is listed in our: HTML Tag Reference.
HTML Headings
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>
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>
Example
<!DOCTYPE html>
<html>
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
.
.
You will learn more about HTML tags and attributes in the next chapters of this tutorial.
Tag
Description
<html>
<body>
<head>
<h1> to <h6>
<hr>
HTML Paragraphs
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>
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>
Example
<p>This is a paragraph
<p>This is another paragraph
The example above will work in most browsers, but don't rely on it.
Forgetting the end tag can produce unexpected results or errors.
Example
<p>This is<br>a para<br>graph with line breaks</p>
The <br> element is an empty HTML element. It has no end tag.
</p>
Example
<p>This will display as a poem:</p>
<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>
Tag
Description
<p>
Defines a paragraph
<br>
<pre>
HTML Styles
I am Red
I am Blue
HTML Styling
Every HTML element has a default style (background color is white, text color is black, text-size is 12px ...)
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>
Example
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue">This is a heading</h1>
<p style="color:red">This is a paragraph.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<h1 style="font-family:verdana">This is a heading</h1>
<p style="font-family:courier">This is a paragraph.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<h1 style="font-size:300%">This is a heading</h1>
<p style="font-size:160%">This is a paragraph.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Centered Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Chapter Summary
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>
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>
Example
Example
<p>This text is normal.</p>
<p><em>This text is emphasized</em>.</p>
Example
<h2>HTML <small>Small</small> Formatting</h2>
Example
<h2>HTML <mark>Marked</mark> Formatting</h2>
Example
<p>My favorite color is <del>blue</del> red.</p>
Example
<p>My favorite <ins>color</ins> is red.</p>
Example
<p>This is <sub>subscripted</sub> text.</p>
Example
<p>This is <sup>superscripted</sup> text.</p>
Description
<b>
<em>
<i>
<small>
<strong>
<sub>
<sup>
<ins>
<del>
<mark>
Example
<p>WWF's goal is to: <q>Build a future where people live in harmony with nature.</q></p>
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>
Marking abbreviations can give useful information to browsers, translation systems and search-engines.
Example
<p>The <abbr title="World Health Organization">WHO</abbr> was founded in 1948.</p>
Example
<p>The <dfn title="World Health Organization">WHO</dfn> was founded in 1948.</p>
2. If the <dfn> element contains an <abbr> element with a title, then that title defines the term:
Example
<p>The <dfn><abbr title="World Health Organization">WHO</abbr></dfn> was founded in 1948.</p>
3. Otherwise, the <dfn> text content is the term, and the parent element contains the definition.
Example
<p>The <dfn>WHO</dfn> World Health Organization was founded in 1948.</p>
Example
<address>
Written by Jon Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>
Example
<p><cite>The Scream</cite> by Edward Munch. Painted in 1893.</p>
Example
<bdo dir="rtl">This text will be written from right to left</bdo>
Description
<abbr>
<address>
<bdo>
<blockquote>
<dfn>
<q>
<cite>
The
computer code.
<kbd>, <samp>, and <code> elements all support fixed letter size and spacing.
Example
<p>To open a file, select:</p>
<p><kbd>File | Open...</kbd></p>
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>
Example
<code>
var person = { firstName:"John", lastName:"Doe", age:50, eyeColor:"blue" }
</code>
Example
<p>Coding Example:</p>
<code>
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
}
</code>
To fix this, you must wrap the code in a <pre> element:
Example
<p>Coding Example:</p>
<code>
<pre>
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
}
</pre>
</code>
Example
<p>Einstein wrote:</p>
<p><var>E = m c<sup>2</sup></var></p>
Description
<code>
<kbd>
<samp>
<var>
<pre>
HTML Comments
Comment tags <!-- and --> are used to insert comments in HTML.
Example
<!-- Write your comments here -->
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 -->
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">
-->
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.
Example
<!DOCTYPE html>
<html>
<head>
<style>
T e x t
B o x e s
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>
The most common way to add styling, is to keep CSS syntax 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.
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>
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>
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>
CSS Fonts
The CSS property color defines the text color to be used for an HTML element.
The CSS property font-family defines the font to be used for an HTML element.
The CSS property font-size defines the text size to be used for an 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>
Every visible HTML element has a box around it, even if you cannot see it.
The CSS border property defines a visible border around an HTML element:
Example
p{
border:1px solid black;
}
The CSS padding property defines a padding (space) inside the border:
Example
p{
border:1px solid black;
padding:10px;
}
The CSS margin property defines a margin (space) outside the border:
Example
p{
border:1px solid black;
padding:10px;
margin:30px;
}
The id Attribute
All the examples above use CSS to style HTML elements in a general way.
The CSS styles define an equal style for all equal elements.
To define a special style for a 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;
}
Example
<p class="error">I am different</p>
Now you can define a different style for this type (class) of element:
Example
p.error {
color:red;
}
Chapter Summary
Use the HTML <head> element to store <style> and <link> elements
Use the CSS padding property for space inside the border
Use the CSS margin property for space outside the border
Description
<style>
<link>
HTML Links
Links are found in nearly all web pages. Links allow users to click their way from page to page.
Link Syntax:
<a href="url">link text</a>
Example:
<a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a>
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.
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>
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>
Example
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a>
Target Value
Description
_blank
_self
Opens the linked document in the same frame as it was clicked (this is default)
_parent
_top
framename
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>
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0">
</a>
Example
Add an id attribute to any <a> element:
Chapter Summary
Use the HTML target attribute to define where to open the linked document
Use the HTML <img> element (inside <a>) to use an image as a link
HTML Images
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>
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">
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">
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>
Example
<img src="/images/html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
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">
Example
<img src="http://www.w3schools.com/images/w3schools_green.jpg" >
Animated Images
The GIF standard allows animated images:
Example
<img src="programming.gif" alt="Computer Man" style="width:48px;height:48px">
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>
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>
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>
Chapter Summary
Use the HTML src attribute to define the image file name
Use the HTML width and height attributes to define the image size
Use the CSS width and height properties to define the image size (alternatively)
Description
<img>
Defines an image
<map>
Defines an image-map
<area>
HTML Tables
HTML Table Example
Number
First Name
Last Name
Points
Eve
Jackson
94
John
Doe
80
Adam
Johnson
67
Jill
Smith
50
A table row can also be divided into table headings with the <th> tag.
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>
The border attribute is on its way out of the HTML standard! It is better to use CSS.
Example
table, th, td {
border:
}
Remember to define borders for both the table and the table cells.
Example
table, th, td {
Example
table, th, td {
border:
border-collapse:
}
th,td {
padding:
15px;
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>
To left-align the table headings, use the CSS text-align property:
Example
th {
text-align:
left;
Example
table {
border-spacing:
}
5px;
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>
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>
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>
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>
100%;
width:
background-color:
}
#f1f1c1;
#fff;
white;
background-color:
}
black;
Chapter Summary
Use the CSS border-spacing property to set the spacing between cells
Description
<table>
Defines a table
<th>
<tr>
<td>
<caption>
<colgroup>
<col>
<thead>
<tbody>
<tfoot>
HTML Lists
HTML can have Unordered Lists, Ordered Lists, or Description Lists:
Unordered List:
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Style
Description
list-style-type:disc
list-style-type:circle
list-style-type:square
list-style-type:none
Disc:
<ul style="list-style-type:disc">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Circle:
<ul style="list-style-type:circle">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Square:
<ul style="list-style-type:square">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
None:
<ul style="list-style-type:none">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
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>Milk</li>
</ol>
Type
Description
type="1"
type="A"
type="a"
type="I"
type="i"
Numbers:
<ol type="1">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Upper Case:
<ol type="A">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Lower Case:
<ol type="a">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Roman Upper Case:
<ol type="I">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Roman Lower Case:
<ol type="i">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Description List:
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
Nested Lists:
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
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>Apples</li>
<li>Bananas</li>
<li>Lemons</li>
<li>Oranges</li>
</ul>
</body>
</html>
With a little extra style, you can make it look like a menu:
Tables
Lists
Blocks
Classes
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;
}
Chapter Summary
Description
<ul>
<ol>
<li>
<dl>
<dt>
<dd>
Example
<div
<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>
Example
<h1>My <span
style="color:red">Important</span>Heading</h1>
Description
<div>
<span>
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>
HTML Layouts
Previous
Next Chapter
City Gallery
London
Paris
Tokyo
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.
Copyright W3Schools.com
Example
<body>
<div id="header">
<h1>City Gallery</h1>
</div>
<div id="nav">
London<br>
Paris<br>
Tokyo<br>
</div>
<div id="section">
<h1>London</h1>
<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>
<p >
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.
</p>
</div>
<div id="footer">
Copyright W3Schools.com
</div>
</body>
Try it yourself
The CSS:
<style>
#header {
background-color:black;
color:white;
text-align:center;
padding:5px;
}
#nav {
line-height:30px;
background-color:#eeeeee;
height:300px;
width:100px;
float:left;
padding:5px;
#section {
width:350px;
float:left;
padding:10px;
#footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
</style>
header
nav
section
article
aside
footer
details
y
This example uses <header>, <nav>, <section>, and <footer> to create a multiple column layout:
Example
<body>
<header>
<h1>City Gallery</h1>
</header>
<nav>
London<br>
Paris<br>
Tokyo<br>
</nav>
<section>
<h1>London</h1>
<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>
<p >
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.
</p>
</section>
<footer>
Copyright W3Schools.com
</footer>
</body>
Try it yourself
The CSS
<style>
header {
background-color:black;
color:white;
text-align:center;
padding:5px;
nav {
line-height:30px;
background-color:#eeeeee;
height:300px;
width:100px;
float:left;
padding:5px;
}
section {
width:350px;
float:left;
padding:10px;
footer {
background-color:black;
color:white;
clear:both;
text-align:center;
padding:5px;
Layout can be achieved using the <table> element, because table elements can be styled with CSS:
Example
<body>
<table class="lamp">
<tr>
<th>
<img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px">
</th>
<td>
The table element was not designed to be a layout tool.
</td>
</tr>
</table>
</body>
Try it yourself
The CSS
<style>
table.lamp {
}
width:100%;
border:1px solid #d4d4d4;
table.lamp th, td {
}
padding:10px;
table.lamp td {
}
width:40px;
</style>
html>
<html lang="en-US">
<head>
<style>
.city {
float: left;
margin: 5px;
padding: 15px;
width: 300px;
height: 300px;
border: 1px solid black;
}
</style>
</head>
<body>
<h1>W3Schools Demo</h1>
<h2>Resize this responsive page!</h2>
<br>
<div class="city">
<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="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>
</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>
<head>
html>
charset="utf-8">
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">
<meta
<meta
</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,
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" frameborder="0"></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
Description
<iframe>
The 17 colors from the HTML specification are: aqua, black, blue, fuchsia, gray, green, lime,
maroon, navy, olive, orange, purple, red, silver, teal, white, and yellow.
Color Name
HEX
Color
Shade Mi
s
x
AliceBlue
#F0F8FF
Shades Mi
x
AntiqueWhite
#FAEBD7
Shades Mi
x
Aqua
#00FFFF
Shades Mi
x
Aquamarine
#7FFFD4
Shades Mi
x
Azure
#F0FFFF
Shades Mi
x
Beige
#F5F5DC
Shades Mi
x
Bisque
#FFE4C4
Shades Mi
x
Black
#000000
Shades Mi
x
BlanchedAlmond
#FFEBCD
Shades Mi
x
Blue
#0000FF
Shades Mi
x
BlueViolet
#8A2BE2
Shades Mi
x
Brown
#A52A2A
Shades Mi
x
BurlyWood
#DEB887
Shades Mi
x
CadetBlue
#5F9EA0
Shades Mi
x
Chartreuse
#7FFF00
Shades Mi
x
Chocolate
#D2691E
Shades Mi
x
Coral
#FF7F50
Shades Mi
x
CornflowerBlue
#6495ED
Shades Mi
x
Cornsilk
#FFF8DC
Shades Mi
x
Crimson
#DC143C
Shades Mi
x
Cyan
#00FFFF
Shades Mi
x
DarkBlue
#00008B
Shades Mi
x
DarkCyan
#008B8B
Shades Mi
x
DarkGoldenRod
#B8860B
Shades Mi
x
DarkGray
#A9A9A9
Shades Mi
x
DarkGreen
#006400
Shades Mi
x
DarkKhaki
#BDB76B
Shades Mi
x
DarkMagenta
#8B008B
Shades Mi
x
DarkOliveGreen
#556B2F
Shades Mi
x
DarkOrange
#FF8C00
Shades Mi
x
DarkOrchid
#9932CC
Shades Mi
x
DarkRed
#8B0000
Shades Mi
x
DarkSalmon
#E9967A
Shades Mi
x
DarkSeaGreen
#8FBC8F
Shades Mi
x
DarkSlateBlue
#483D8B
Shades Mi
x
DarkSlateGray
#2F4F4F
Shades Mi
x
DarkTurquoise
#00CED1
Shades Mi
x
DarkViolet
#9400D3
Shades Mi
x
DeepPink
#FF1493
Shades Mi
x
DeepSkyBlue
#00BFFF
Shades Mi
x
DimGray
#696969
Shades Mi
x
DodgerBlue
#1E90FF
Shades Mi
x
FireBrick
#B22222
Shades Mi
x
FloralWhite
#FFFAF0
Shades Mi
x
ForestGreen
#228B22
Shades Mi
x
Fuchsia
#FF00FF
Shades Mi
x
Gainsboro
#DCDCDC
Shades Mi
x
GhostWhite
#F8F8FF
Shades Mi
x
Gold
#FFD700
Shades Mi
x
GoldenRod
#DAA520
Shades Mi
x
Gray
#808080
Shades Mi
x
Green
#008000
Shades Mi
x
GreenYellow
#ADFF2F
Shades Mi
x
HoneyDew
#F0FFF0
Shades Mi
x
HotPink
#FF69B4
Shades Mi
x
IndianRed
#CD5C5C
Shades Mi
x
Indigo
#4B0082
Shades Mi
x
Ivory
#FFFFF0
Shades Mi
x
Khaki
#F0E68C
Shades Mi
x
Lavender
#E6E6FA
Shades Mi
x
LavenderBlush
#FFF0F5
Shades Mi
x
LawnGreen
#7CFC00
Shades Mi
x
LemonChiffon
#FFFACD
Shades Mi
x
LightBlue
#ADD8E6
Shades Mi
x
LightCoral
#F08080
Shades Mi
x
LightCyan
#E0FFFF
Shades Mi
x
LightGoldenRodYel #FAFAD2
low
Shades Mi
x
LightGray
#D3D3D3
Shades Mi
x
LightGreen
#90EE90
Shades Mi
x
LightPink
#FFB6C1
Shades Mi
x
LightSalmon
#FFA07A
Shades Mi
x
LightSeaGreen
#20B2AA
Shades Mi
x
LightSkyBlue
#87CEFA
Shades Mi
x
LightSlateGray
#778899
Shades Mi
x
LightSteelBlue
#B0C4DE
Shades Mi
x
LightYellow
#FFFFE0
Shades Mi
x
Lime
#00FF00
Shades Mi
x
LimeGreen
#32CD32
Shades Mi
x
Linen
#FAF0E6
Shades Mi
x
Magenta
#FF00FF
Shades Mi
x
Maroon
#800000
Shades Mi
x
MediumAquaMarin #66CDAA
e
Shades Mi
x
MediumBlue
#0000CD
Shades Mi
x
MediumOrchid
#BA55D3
Shades Mi
x
MediumPurple
#9370DB
Shades Mi
x
MediumSeaGreen
#3CB371
Shades Mi
x
MediumSlateBlue
#7B68EE
Shades Mi
x
MediumSpringGre
en
#00FA9A
Shades Mi
x
MediumTurquoise
#48D1CC
Shades Mi
x
MediumVioletRed
#C71585
Shades Mi
x
MidnightBlue
#191970
Shades Mi
x
MintCream
#F5FFFA
Shades Mi
x
MistyRose
#FFE4E1
Shades Mi
x
Moccasin
#FFE4B5
Shades Mi
x
NavajoWhite
#FFDEAD
Shades Mi
x
Navy
#000080
Shades Mi
x
OldLace
#FDF5E6
Shades Mi
x
Olive
#808000
Shades Mi
x
OliveDrab
#6B8E23
Shades Mi
x
Orange
#FFA500
Shades Mi
x
OrangeRed
#FF4500
Shades Mi
x
Orchid
#DA70D6
Shades Mi
x
PaleGoldenRod
#EEE8AA
Shades Mi
x
PaleGreen
#98FB98
Shades Mi
x
PaleTurquoise
#AFEEEE
Shades Mi
x
PaleVioletRed
#DB7093
Shades Mi
x
PapayaWhip
#FFEFD5
Shades Mi
x
PeachPuff
#FFDAB9
Shades Mi
x
Peru
#CD853F
Shades Mi
x
Pink
#FFC0CB
Shades Mi
x
Plum
#DDA0DD
Shades Mi
x
PowderBlue
#B0E0E6
Shades Mi
x
Purple
#800080
Shades Mi
x
RebeccaPurple
#663399
Shades Mi
x
Red
#FF0000
Shades Mi
x
RosyBrown
#BC8F8F
Shades Mi
x
RoyalBlue
#4169E1
Shades Mi
x
SaddleBrown
#8B4513
Shades Mi
x
Salmon
#FA8072
Shades Mi
x
SandyBrown
#F4A460
Shades Mi
x
SeaGreen
#2E8B57
Shades Mi
x
SeaShell
#FFF5EE
Shades Mi
x
Sienna
#A0522D
Shades Mi
x
Silver
#C0C0C0
Shades Mi
x
SkyBlue
#87CEEB
Shades Mi
x
SlateBlue
#6A5ACD
Shades Mi
x
SlateGray
#708090
Shades Mi
x
Snow
#FFFAFA
Shades Mi
x
SpringGreen
#00FF7F
Shades Mi
x
SteelBlue
#4682B4
Shades Mi
x
Tan
#D2B48C
Shades Mi
x
Teal
#008080
Shades Mi
x
Thistle
#D8BFD8
Shades Mi
x
Tomato
#FF6347
Shades Mi
x
Turquoise
#40E0D0
Shades Mi
x
Violet
#EE82EE
Shades Mi
x
Wheat
#F5DEB3
Shades Mi
x
White
#FFFFFF
Shades Mi
x
WhiteSmoke
#F5F5F5
Shades Mi
x
Yellow
#FFFF00
Shades Mi
x
YellowGreen
#9ACD32
Shades Mi
x
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
Color 3 digit
HEX
Color 6 digit
HEX
Color RGB
#F00
#FF0000
rgb(255,0,0)
#0F0
#00FF00
rgb(0,255,0)
#00F
#0000FF
rgb(0,0,255)
Try it Yourself
Shades of grey (from black to white) are defined using equal values for all the 3 light sourses:
Color
Color 3 digit
HEX
Color 6 digit
HEX
Color RGB
#000
#000000
rgb(0,0,0)
#888
#888888
rgb(136,136,136)
#FFF
#FFFFFF
rgb(255,255,255)
Try it Yourself
Color Name
HEX
Color
Shade Mix
s
Black
#000000
Shade
s
Mix
Navy
#000080
Shade
s
Mix
DarkBlue
#00008B
Shade
s
Mix
MediumBlue
#0000CD
Shade
s
Mix
Blue
#0000FF
Shade
s
Mix
DarkGreen
#006400
Shade
s
Mix
Green
#008000
Shade
s
Mix
Teal
#008080
Shade
s
Mix
DarkCyan
#008B8B
Shade
s
Mix
DeepSkyBlue
#00BFFF
Shade
s
Mix
DarkTurquoise
#00CED1
Shade
s
Mix
MediumSpringGre
en
#00FA9A
Shade
s
Mix
Lime
#00FF00
Shade
s
Mix
SpringGreen
#00FF7F
Shade
s
Mix
Aqua
#00FFFF
Shade
s
Mix
Cyan
#00FFFF
Shade
s
Mix
MidnightBlue
#191970
Shade
s
Mix
DodgerBlue
#1E90FF
Shade
s
Mix
LightSeaGreen
#20B2AA
Shade
s
Mix
ForestGreen
#228B22
Shade
s
Mix
SeaGreen
#2E8B57
Shade
s
Mix
DarkSlateGray
#2F4F4F
Shade
s
Mix
LimeGreen
#32CD32
Shade
s
Mix
MediumSeaGreen
#3CB371
Shade
s
Mix
Turquoise
#40E0D0
Shade
s
Mix
RoyalBlue
#4169E1
Shade
s
Mix
SteelBlue
#4682B4
Shade
s
Mix
DarkSlateBlue
#483D8B
Shade
Mix
s
MediumTurquoise
#48D1CC
Shade
s
Mix
Indigo
#4B0082
Shade
s
Mix
DarkOliveGreen
#556B2F
Shade
s
Mix
CadetBlue
#5F9EA0
Shade
s
Mix
CornflowerBlue
#6495ED
Shade
s
Mix
RebeccaPurple
#663399
Shade
s
Mix
MediumAquaMari
ne
#66CDAA
Shade
s
Mix
DimGray
#696969
Shade
s
Mix
SlateBlue
#6A5ACD
Shade
s
Mix
OliveDrab
#6B8E23
Shade
s
Mix
SlateGray
#708090
Shade
s
Mix
LightSlateGray
#778899
Shade
s
Mix
MediumSlateBlue
#7B68EE
Shade
s
Mix
LawnGreen
#7CFC00
Shade
s
Mix
Chartreuse
#7FFF00
Shade
s
Mix
Aquamarine
#7FFFD4
Shade
s
Mix
Maroon
#800000
Shade
s
Mix
Purple
#800080
Shade
s
Mix
Olive
#808000
Shade
s
Mix
Gray
#808080
Shade
s
Mix
SkyBlue
#87CEEB
Shade
s
Mix
LightSkyBlue
#87CEFA
Shade
s
Mix
BlueViolet
#8A2BE2
Shade
s
Mix
DarkRed
#8B0000
Shade
s
Mix
DarkMagenta
#8B008B
Shade
s
Mix
SaddleBrown
#8B4513
Shade
s
Mix
DarkSeaGreen
#8FBC8F
Shade
s
Mix
LightGreen
#90EE90
Shade
s
Mix
MediumPurple
#9370DB
Shade
s
Mix
DarkViolet
#9400D3
Shade
s
Mix
PaleGreen
#98FB98
Shade
s
Mix
DarkOrchid
#9932CC
Shade
s
Mix
YellowGreen
#9ACD32
Shade
s
Mix
Sienna
#A0522D
Shade
s
Mix
Brown
#A52A2A
Shade
s
Mix
DarkGray
#A9A9A9
Shade
s
Mix
LightBlue
#ADD8E6
Shade
s
Mix
GreenYellow
#ADFF2F
Shade
s
Mix
PaleTurquoise
#AFEEEE
Shade
Mix
s
LightSteelBlue
#B0C4DE
Shade
s
Mix
PowderBlue
#B0E0E6
Shade
s
Mix
FireBrick
#B22222
Shade
s
Mix
DarkGoldenRod
#B8860B
Shade
s
Mix
MediumOrchid
#BA55D3
Shade
s
Mix
RosyBrown
#BC8F8F
Shade
s
Mix
DarkKhaki
#BDB76B
Shade
s
Mix
Silver
#C0C0C0
Shade
s
Mix
MediumVioletRed
#C71585
Shade
s
Mix
IndianRed
#CD5C5C
Shade
s
Mix
Peru
#CD853F
Shade
s
Mix
Chocolate
#D2691E
Shade
s
Mix
Tan
#D2B48C
Shade
s
Mix
LightGray
#D3D3D3
Shade
s
Mix
Thistle
#D8BFD8
Shade
s
Mix
Orchid
#DA70D6
Shade
s
Mix
GoldenRod
#DAA520
Shade
s
Mix
PaleVioletRed
#DB7093
Shade
s
Mix
Crimson
#DC143C
Shade
s
Mix
Gainsboro
#DCDCD
C
Shade
s
Mix
Plum
#DDA0D
D
Shade
s
Mix
BurlyWood
#DEB887
Shade
s
Mix
LightCyan
#E0FFFF
Shade
s
Mix
Lavender
#E6E6FA
Shade
s
Mix
DarkSalmon
#E9967A
Shade
s
Mix
Violet
#EE82EE
Shade
s
Mix
PaleGoldenRod
#EEE8AA
Shade
s
Mix
LightCoral
#F08080
Shade
s
Mix
Khaki
#F0E68C
Shade
s
Mix
AliceBlue
#F0F8FF
Shade
s
Mix
HoneyDew
#F0FFF0
Shade
s
Mix
Azure
#F0FFFF
Shade
s
Mix
SandyBrown
#F4A460
Shade
s
Mix
Wheat
#F5DEB3
Shade
s
Mix
Beige
#F5F5DC
Shade
s
Mix
WhiteSmoke
#F5F5F5
Shade
s
Mix
MintCream
#F5FFFA
Shade
s
Mix
GhostWhite
#F8F8FF
Shade
s
Mix
Salmon
#FA8072
Shade
Mix
s
AntiqueWhite
#FAEBD7
Shade
s
Mix
Linen
#FAF0E6
Shade
s
Mix
LightGoldenRodYe
llow
#FAFAD2
Shade
s
Mix
OldLace
#FDF5E6
Shade
s
Mix
Red
#FF0000
Shade
s
Mix
Fuchsia
#FF00FF
Shade
s
Mix
Magenta
#FF00FF
Shade
s
Mix
DeepPink
#FF1493
Shade
s
Mix
OrangeRed
#FF4500
Shade
s
Mix
Tomato
#FF6347
Shade
s
Mix
HotPink
#FF69B4
Shade
s
Mix
Coral
#FF7F50
Shade
s
Mix
DarkOrange
#FF8C00
Shade
s
Mix
LightSalmon
#FFA07A
Shade
s
Mix
Orange
#FFA500
Shade
s
Mix
LightPink
#FFB6C1
Shade
s
Mix
Pink
#FFC0CB
Shade
s
Mix
Gold
#FFD700
Shade
s
Mix
PeachPuff
#FFDAB9
Shade
s
Mix
NavajoWhite
#FFDEAD
Shade
s
Mix
Moccasin
#FFE4B5
Shade
s
Mix
Bisque
#FFE4C4
Shade
s
Mix
MistyRose
#FFE4E1
Shade
s
Mix
BlanchedAlmond
#FFEBCD
Shade
s
Mix
PapayaWhip
#FFEFD5
Shade
s
Mix
LavenderBlush
#FFF0F5
Shade
s
Mix
SeaShell
#FFF5EE
Shade
s
Mix
Cornsilk
#FFF8DC
Shade
s
Mix
LemonChiffon
#FFFACD
Shade
s
Mix
FloralWhite
#FFFAF0
Shade
s
Mix
Snow
#FFFAFA
Shade
s
Mix
Yellow
#FFFF00
Shade
s
Mix
LightYellow
#FFFFE0
Shade
s
Mix
Ivory
#FFFFF0
Shade
s
Mix
White
#FFFFFF
Shade
s
Mix
Shades of Gray
Gray colors are displayed using an equal amount of power to all of the light sources.
To make it easy for you to select a gray color we have compiled a table of gray shades for
you:
Gray Shades
HEX
RGB
#000000
rgb(0,0,0)
#080808
rgb(8,8,8)
#101010
rgb(16,16,16)
#181818
rgb(24,24,24)
#202020
rgb(32,32,32)
#282828
rgb(40,40,40)
#303030
rgb(48,48,48)
#383838
rgb(56,56,56)
#404040
rgb(64,64,64)
#484848
rgb(72,72,72)
#505050
rgb(80,80,80)
#585858
rgb(88,88,88)
#606060
rgb(96,96,96)
#686868
rgb(104,104,104)
#707070
rgb(112,112,112)
#787878
rgb(120,120,120)
#808080
rgb(128,128,128)
#888888
rgb(136,136,136)
#909090
rgb(144,144,144)
#989898
rgb(152,152,152)
#A0A0A0
rgb(160,160,160)
#A8A8A8
rgb(168,168,168)
#B0B0B0
rgb(176,176,176)
#B8B8B8
rgb(184,184,184)
#C0C0C0
rgb(192,192,192)
#C8C8C8
rgb(200,200,200)
#D0D0D0
rgb(208,208,208)
#D8D8D8
rgb(216,216,216)
#E0E0E0
rgb(224,224,224)
#E8E8E8
rgb(232,232,232)
#F0F0F0
rgb(240,240,240)
#F8F8F8
rgb(248,248,248)
#FFFFFF
rgb(255,255,255)
HEX
RGB
#000000
rgb(0,0,0)
#080000
rgb(8,0,0)
#100000
rgb(16,0,0)
#180000
rgb(24,0,0)
#200000
rgb(32,0,0)
#280000
rgb(40,0,0)
#300000
rgb(48,0,0)
#380000
rgb(56,0,0)
#400000
rgb(64,0,0)
#480000
rgb(72,0,0)
#500000
rgb(80,0,0)
#580000
rgb(88,0,0)
#600000
rgb(96,0,0)
#680000
rgb(104,0,0)
#700000
rgb(112,0,0)
#780000
rgb(120,0,0)
#800000
rgb(128,0,0)
#880000
rgb(136,0,0)
#900000
rgb(144,0,0)
#980000
rgb(152,0,0)
#A00000
rgb(160,0,0)
#A80000
rgb(168,0,0)
#B00000
rgb(176,0,0)
#B80000
rgb(184,0,0)
#C00000
rgb(192,0,0)
#C80000
rgb(200,0,0)
#D00000
rgb(208,0,0)
#D80000
rgb(216,0,0)
#E00000
rgb(224,0,0)
#E80000
rgb(232,0,0)
#F00000
rgb(240,0,0)
#F80000
rgb(248,0,0)
#FF0000
rgb(255,0,0)
000033
000066
000099
0000CC
0000
003300
003333
003366
003399
0033CC
0033
006600
006633
006666
006699
0066CC
0066
009900
009933
009966
009999
0099CC
0099
00CC00
00CC33
00CC66
00CC99
00CCCC
00CC
00FF00
00FF33
00FF66
00FF99
00FFCC
00FF
330000
330033
330066
330099
3300CC
3300
333300
333333
333366
333399
3333CC
3333
336600
336633
336666
336699
3366CC
3366
339900
339933
339966
339999
3399CC
3399
33CC00
33CC33
33CC66
33CC99
33CCCC
33CC
33FF00
33FF33
33FF66
33FF99
33FFCC
33FF
660000
660033
660066
660099
6600CC
6600
663300
663333
663366
663399
6633CC
6633
666600
666633
666666
666699
6666CC
6666
669900
669933
669966
669999
6699CC
6699
66CC00
66CC33
66CC66
66CC99
66CCCC
66CC
66FF00
66FF33
66FF66
66FF99
66FFCC
66FF
990000
990033
990066
990099
9900CC
9900
993300
993333
993366
993399
9933CC
9933
996600
996633
996666
996699
9966CC
9966
999900
999933
999966
999999
9999CC
9999
99CC00
99CC33
99CC66
99CC99
99CCCC
99CC
99FF00
99FF33
99FF66
99FF99
99FFCC
99FF
CC0000
CC0033
CC0066
CC0099
CC00CC
CC00
CC3300
CC3333
CC3366
CC3399
CC33CC
CC33
CC6600
CC6633
CC6666
CC6699
CC66CC
CC66
CC9900
CC9933
CC9966
CC9999
CC99CC
CC99
CCCC00
CCCC33
CCCC66
CCCC99
CCCCCC
CCCC
CCFF00
CCFF33
CCFF66
CCFF99
CCFFCC
CCFF
FF0000
FF0033
FF0066
FF0099
FF00CC
FF00
FF3300
FF3333
FF3366
FF3399
FF33CC
FF33
FF6600
FF6633
FF6666
FF6699
FF66CC
FF66
FF9900
FF9933
FF9966
FF9999
FF99CC
FF99
FFCC00
FFCC33
FFCC66
FFCC99
FFCCCC
FFCC
FFFF00
FFFF33
FFFF66
FFFF99
FFFFCC
FFFF
HTML Scripts
Previous
Next Chapter
Example
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
Try it Yourself
Example
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
Try it Yourself
Try it Yourself
Try it Yourself
Try it Yourself
Description
<script>
<noscript>
Defines an alternate content for users that do not support client-side scripts
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:
html>
lang="en-US">
<!DOCTYPE
<html
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
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
Example
<meta
Example
<meta
charset="UTF-8">
Example
<meta
Try it Yourself
Example
<meta
http-equiv="refresh" content="30">
Example
<script>
function myFunction {
document.getElementById("demo").innerHTML = "Hello JavaScript!";
}
</script>
Try it Yourself
Example
<base
href="http://www.w3schools.com/images/" target="_blank">
Try it Yourself
Description
<head>
<title>
<base>
<link>
<meta>
<script>
<style>
HTML Entities
Previous
Next Chapter
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.
A common character entity used in HTML is the non breaking space ( ).
Remember that browsers will always truncate spaces in HTML pages. If you write 10 spaces in your text, the browser will remove 9
of them. To add real spaces to your text, you can use the character entity.
Description
Entity Name
Entity Number
non-breaking space
 
<
less than
<
<
>
greater than
>
>
&
ampersand
&
&
cent
¢
¢
pound
£
£
yen
¥
¥
euro
€
€
copyright
©
©
registered trademark
®
®
Mark
Character
Construct
Result
à
á
â
ã
Ò
Ó
Ô
Õ
You will see more HTML symbols in the next chapter of this tutorial.
HTML Symbols
Previous
Next Chapter
If you use an HTML entity name or a hexadecimal number, the character will always display
correctly.
This is independent of what character set (encoding) your page uses!
Example
<p>I will display €</p>
<p>I will display €</p>
<p>I will display €</p>
Try it Yourself
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
Numbe
r
Entity
Description
Α
Α
Β
Β
Γ
Γ
Δ
Δ
Ε
Ε
Ζ
Ζ
Number
Entity
Description
©
©
COPYRIGHT SIGN
®
®
REGISTERED SIGN
€
€
EURO SIGN
™
™
TRADEMARK
←
←
LEFTWARDS ARROW
↑
↑
UPWARDS ARROW
→
→
RIGHTWARDS ARROW
↓
↓
DOWNWARDS ARROW
♠
♠
♣
♣
♥
♥
♦
♦
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.
Num
b
ASCII
ANSI
8859
UTF8
32
Description
space
33
exclamation mark
34
"
"
"
"
quotation mark
35
number sign
36
dollar sign
37
percent sign
38
&
&
&
&
ampersand
39
'
'
'
'
apostrophe
40
left parenthesis
41
right parenthesis
42
asterisk
43
plus sign
44
comma
45
hyphen-minus
46
full stop
47
solidus
48
digit zero
49
digit one
50
digit two
51
digit three
52
digit four
53
digit five
54
digit six
55
digit seven
56
digit eight
57
digit nine
58
colon
59
semicolon
60
<
<
<
<
less-than sign
61
equals sign
62
>
>
>
>
greater-than sign
63
question mark
64
commercial at
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
reverse solidus
93
94
circumflex accent
95
low line
96
grave accent
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
vertical line
125
126
tilde
127
DEL
128
euro sign
129
130
131
NOT USED
132
133
horizontal ellipsis
134
dagger
135
double dagger
136
137
138
139
140
141
142
143
NOT USED
144
NOT USED
145
146
147
148
149
bullet
150
en dash
151
em dash
152
small tilde
153
154
155
156
157
158
159
NOT USED
Latin capital letter Z with caron
160
NOT USED
no-break space
161
162
cent sign
163
pound sign
164
currency sign
165
yen sign
166
broken bar
167
section sign
168
diaeresis
169
copyright sign
170
171
172
not sign
173
soft hyphen
174
registered sign
175
macron
176
degree sign
177
plus-minus sign
178
superscript two
179
superscript three
180
acute accent
181
micro sign
182
pilcrow sign
183
middle dot
184
cedilla
185
superscript one
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
multiplication sign
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
division sign
248
249
250
251
252
253
254
255
Example
scheme://host.domain:port/path/filename
Explanation:
host - defines the domain host (default host for http is www)
port - defines the port number at the host (default for http is 80)
path - defines a path at the server (If omitted: the root directory of the site)
Scheme
Short for
Used for
http
https
ftp
file
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
Submit
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.
Character
From Windows-1252
From UTF-8
%80
%E2%82%AC
%A3
%C2%A3
%A9
%C2%A9
%AE
%C2%AE
%C0
%C3%80
%C1
%C3%81
%C2
%C3%82
%C3
%C3%83
%C4
%C3%84
%C5
%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.
By combining the strengths of HTML and XML, XHTML was developed.
XHTML Elements
XHTML Attributes
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of document</title>
</head>
<body>
some content
</body>
</html>
In XHTML, all elements must be properly nested within each other, like this:
<p>This is a paragraph
<p>This is another paragraph
This is correct:
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<br>
A horizontal rule:
An image:
<hr>
This is correct:
A break:
<br />
<hr />
<img src="happy.gif" alt="Happy face" />
A horizontal rule:
An image:
<BODY>
<P>This is a paragraph</P>
</BODY>
This is correct:
<body>
<p>This is a paragraph</p>
</body>
<table WIDTH="100%">
This is correct:
<table width="100%">
<table width=100%>
This is correct:
<table width="100%">
2.
3.
4.
5.
6.
http://w w w .w 3schools.com/html/demo_xhtml.asp
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.
Type
Description
text
radio
submit
You will learn a lot more about input types later in this tutorial.
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
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
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
First name:
Mickey
Last name:
Mouse
Submit
GET is best suited to short amounts of data. Size limitations are set in your browser.
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
Mickey
Last name:
Mouse
Submit
An HTML <form> element, with all possible attributes set, will look like this:
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
Description
accept-charset
Specifies the charset used in the submitted form (default: the page charset).
action
Specifies an address (url) where to submit the form (default: the submitting page).
autocomplete
enctype
method
Specifies the HTTP method used when submitting the form (default: GET).
name
Specifies a name used to identify the form (for DOM usage: document.forms.name).
novalidate
target
Specifies the target of the address in the action attribute (default: _self).
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
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
Example
<button type="button" onclick="alert('Hello World!')">Click Me!</button>
Try it Yourself
Click Me!
<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"
100 +
Tag
Description
<form>
<input>
<textarea>
<label>
<fieldset>
<legend>
<select>
<optgroup>
<option>
<button>
<datalist>
<keygen>
<output>
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
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
Password:
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
Mickey
Last name:
Mouse
Submit
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>
<input type="radio" name="sex" value="female">Female
</form>
Try it Yourself
Female
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
I have a car
Example
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
Try it Yourself
color
date
datetime
datetime-local
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):
Try it Yourself
Input Restrictions
Here is a list of some common input restrictions (some are new in HTML5):
Attribute
Description
disabled
max
maxlength
min
pattern
readonly
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
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.
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Birthday (month and year):
<input type="month" name="bdaymonth">
</form>
Try it Yourself
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
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>
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
list
multiple
pattern (regexp)
placeholder
required
step
autocomplete
novalidate
Example
An HTML form with autocomplete on (and off for one input field):
<form action="action_page.php" autocomplete="on">
First name:<input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
Try it Yourself
Tip: In some browsers you may need to activate the autocomplete function for this to work.
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
When present, it specifies that an <input> element should automatically get focus when the page loads.
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
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
Always specify the size of images. If the browser does not know the size, the page will flicker while
images load.
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">
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
Try it Yourself
Example
A file upload field that accepts multiple values:
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
Example
An input field with a placeholder text:
<input type="text" name="fname" placeholder="First name">
Try it Yourself
Example
Try it Yourself
Example
An input field with a specified legal number intervals:
<input type="number" name="points" step="3">
Try it Yourself
HTML5 Introduction
Previous
Next Chapter
html>
charset="UTF-8">
HTML5 Example:
<!DOCTYPE
<html>
<head>
html>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<body>
Content of the document......
</body>
</html>
In the chapter HTML5 Support, you will learn how to "teach" old browsers to handle HTML5
semantic.
HTML Geolocation
HTML SSE
Element
Use instead
<acronym>
<abbr>
<applet>
<object>
<basefont>
CSS
<big>
CSS
<center>
CSS
<dir>
<ul>
<font>
CSS
<frame>
<frameset>
<noframes>
<strike>
CSS
<tt>
CSS
In the chapter HTML5 Migration, you will learn how to easily migrate from HTML4 to HTML5.
HTML History
Since the early days of the web, there have been many versions of HTML:
Version
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.
You can even teach stone age IE6 (Windows XP 2001) how to handle unknown HTML elements.
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>
<p>My first paragraph.</p>
<myHero>My First Hero</myHero>
</body>
</html>
Try it Yourself
Internet Explorer 8 and earlier, does not allow styling of unknown elements.
Thankfully, Sjoerd Visscher created the "HTML5 Enabling JavaScript", "the shiv":
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
The code above is a comment, but versions previous to IE9 will read it (and understand it).
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
Tag
Description
<article>
<aside>
<bdi>
Defines a part of text that might be formatted in a different direction from other text
<details>
<dialog>
<figcaption>
<figure>
Defines self-contained content, like illustrations, diagrams, photos, code listings, etc.
<footer>
<header>
<main>
<mark>
<menuitem>
Defines a command/menu item that the user can invoke from a popup menu
<meter>
<nav>
<progress>
<rp>
<rt>
<ruby>
<section>
<summary>
<time>
Defines a date/time
<wbr>
Description
<datalist>
<keygen>
<output>
Read all about old and new form elements in HTML Form Elements.
color
autocomplete
date
autofocus
datetime
form
datetime-local
formaction
formenctype
month
formmethod
number
formnovalidate
range
formtarget
search
tel
list
time
url
multiple
week
pattern (regexp)
placeholder
required
step
Learn all about old and new input types in HTML Input Types.
Learn all about input attributes in HTML Input Attributes.
Type
Example
Empty
Unquoted
Doublequoted
Single-quoted
In HTML5, all 4 syntaxes may be used, depending on what is needed for the attribute.
HTML5 Graphics
Tag
Description
<canvas>
<svg>
Description
<audio>
<embed>
<source>
<track>
<video>
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
In the HTML5 standard, the <article> element defines a complete, self-contained block of related elements.
The <section> element is defined as a block of related elements.
Can we use the definitions to decide how to nest elements? No, we cannot!
On the Internet, you will find HTML pages with <section> elements containing <article> elements, and <article> elements
containing <sections> elements.
You will also find pages with <section> elements containing <section> elements, and <article> elements containing <article>
elements.
Newspaper: The sports articles in the sports section, have a technical section in each article.
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.
With HTML4, developers used their own favorite attribute names to style page elements:
header, top, bottom, footer, menu, navigation, main, container, content, article, sidebar, topnav, ...
This made it impossible for search engines to identify the correct web page content.
With HTML5 elements like: <header> <footer> <nav> <section> <article>, this will become easier.
According to the W3C, a Semantic Web:
"Allows data to be shared and reused across applications, enterprises, and communities."
Tag
Description
<article>
Defines an article
<aside>
<details>
<figcaption>
<figure>
Specifies self-contained content, like illustrations, diagrams, photos, code listings, etc.
<footer>
<header>
<main>
<mark>
<nav>
<section>
<summary>
<time>
Defines a date/time
HTML5 Migration
Previous
Next Chapter
You can migrate to HTML5 from HTML4, and XHTML, using the same recipe.
Typical HTML4
Typical HTML5
<div id="header">
<header>
<div id="menu">
<nav>
<div id="content">
<section>
<div id="post">
<article>
<div id="footer">
<footer>
Try it Yourself
Try it Yourself
Try it Yourself
Try it Yourself
div#header,div#footer,div#content,div#post {
div#header,div#footer {
color:white;background-color:#444;margin-bottom:5px;
div#content {
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 {
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
Try it Yourself
Try it Yourself
Try it Yourself
Try it Yourself
div#header,div#footer,div#content,div#post {
div#header,div#footer {
color:white;background-color:#444;margin-bottom:5px;
div#content {
background-color:#ddd;
div#menu ul {
margin:0;padding:0;
div#menu 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
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>
</section>
<footer>
<p>&copy; 2014 Monday Times. All rights reserved.</p>
</footer>
</body>
</html>
Try it Yourself
<article> in <article>:
<article>
<h2>Famous Cities</h2>
<article>
<h2>London</h2>
<p>London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
</article>
<article>
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</article>
<article>
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</article>
</article>
Try it Yourself
<div> in <article>:
<article>
<h2>Famous Cities</h2>
<div class="city">
<h2>London</h2>
<p>London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
</div>
<div class="city">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>
<div class="city">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</div>
</article>
Try it Yourself
<div class="country">
<h2>England</h2>
<p>London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
</div>
<div class="country">
<h2>France</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>
<div class="country">
<h2>Japan</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</div>
</section>
</article>
Try it Yourself
If you want consistently with lower case tags, you can use:
<!doctype html>
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>
This is allowed:
<meta charset="utf-8">
This is also allowed:
<meta charset="utf-8" />
The slash (/) is required in XHTML and XML.
If you expect XML software to access your page, it might be a good idea to keep it.
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
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
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:
body {
background-color: lightgrey;
font-family: "Arial Black", Helvetica, sans-serif;
font-size: 16em;
color: black;
}
Use colon plus one space between each property and its value.
Adding a space after a comma, or a semicolon, is a general rule in all types of writing.
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
What is Canvas?
Canvas is a medium for oil painting. One of the earliest oils on canvas is a French Madonna from 1410. Canvas is typically
stretched across a wooden frame.
On the HTML canvas, you can draw all kinds of graphics, from simple lines, to complex graphic objects.
Canvas Examples
Basic Canvas Example
<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>
Try it Yourself
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
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
Draw Gradient
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Create gradient
var grd = ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
Try it Yourself
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?
SVG Circle
<!DOCTYPE
<html>
<body>
<svg
html>
width="100" height="100">
cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
<circle
</svg>
</body>
</html>
Try it Yourself
SVG Rectangle
width="400" height="100">
<rect width="400" height="100" style="fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)" />
<svg
</svg>
Try it Yourself
<svg
<rect
</svg>
Try it Yourself
SVG Star
width="300" height="200">
points="100,10 40,198 190,78 10,78 160,198"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
<svg
<polygon
</svg>
Try it Yourself
SVG Logo
SVG
<svg height="130"
<defs>
width="500">
<linearGradient
<stop
</linearGradient>
</defs>
<ellipse
Try it Yourself
Canvas
SVG
Resolution dependent
Resolution independent
HTML Multimedia
Previous
Next Chapter
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.
Format
File
Description
MPEG
.mpg
.mpeg
MPEG. Developed by the Moving Pictures Expert Group. The first popular video format
on the web. Used to be supported by all browsers, but it is not supported in HTML5
(See MP4).
AVI
.avi
WMV
.wmv
QuickTim
e
.mov
RealVideo
.rm
.ram
RealVideo. Developed by Real Media to allow video streaming with low bandwidths. It
is still used for online video and Internet TV, but does not play in web browsers.
Flash
.swf
.flv
Ogg
.ogg
WebM
.
webm
WebM. Developed by the web giants, Mozilla, Opera, Adobe, and Google. Supported
by HTML5.
MPEG-4
or MP4
.mp4
MP4. Developed by the Moving Pictures Expert Group. Based on QuickTime. 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
Description
MIDI
.mid
.midi
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
.rm
.ram
RealAudio. Developed by Real Media to allow streaming of audio with low bandwidths.
Does not play in web browsers.
WMA
.wma
AAC
.aac
AAC (Advanced Audio Coding). Developed by Apple as the default format for iTunes.
Plays well on Apple computers, but not in web browsers.
WAV
.wav
WAV. Developed by IBM and Microsoft. Plays well on Windows, Macintosh, and Linux
operating systems. Supported by HTML5.
Ogg
.ogg
MP3
.mp3
MP3 files are actually the sound part of MPEG files. MP3 is the most popular format for
music players. Combines good compression (small files) with high quality. Supported by
all browsers.
MP4
.mp4
MP4 is a video format, but can also be used for audio. MP4 video is the upcoming video
format on the internet. This leads to automatic support for MP4 audio by all browsers.
Only MP3, WAV, and Ogg audio is supported by the newest HTML5 standard.
Previous
Next Chapter
HTML5 Video
Previous
Next Chapter
Try it yourself
Browser Support
Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support the <video> element.
Note: Internet Explorer 8 and earlier versions, do not support the <video> element.
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
Try it yourself
Browser
MP4
WebM
Ogg
Internet Explorer
YES
NO
NO
Chrome
YES
YES
YES
Firefox
YES
YES
YES
Safari
YES
NO
NO
Opera
YES
YES
Media Type
MP4
video/mp4
WebM
video/webm
Ogg
video/ogg
Try it yourself
Description
<video>
<source>
Defines multiple media resources for media elements, such as <video> and <audio>
<track>
HTML5 Audio
Previous
Next Chapter
Browser Support
Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support the <audio> element.
Note: Internet Explorer 8 and earlier versions, do not support the <audio> element.
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.
Browser
MP3
Wav
Ogg
Internet Explorer
YES
NO
NO
Chrome
YES
YES
YES
Firefox
YES
YES
YES
Safari
YES
YES
NO
Opera
YES
YES
YES
Media Type
MP3
audio/mpeg
Ogg
audio/ogg
Wav
audio/wav
Description
<audio>
<source>
Defines multiple media resources for media elements, such as <video> and <audio>
HTML Plug-ins
Previous
Next Chapter
To display video and audio: Use the <video> and <audio> tags.
Example
<object width="400" height="50" data="bookmark.swf"></object>
Try it Yourself
Example
<object width="100%" height="500px" data="snippet.html"></object>
Try it Yourself
Example
<object data="audi.jpeg"></object>
Try it Yourself
The <embed> element also defines an embedded object within an HTML document.
Web browsers have supported the <embed> element for a long time. However, it has not been a part of the HTML specification
before HTML5. The element will validate in an HTML5 page, but not in an HTML 4 page.
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.
Example
<embed width="100%" height="500px" src="snippet.html">
Try it Yourself
Example
<embed src="audi.jpeg">
Try it Yourself
Try it Yourself
Try it Yourself
Try it Yourself
YouTube will display the code to use (like:XGSy3_Czz8k), when you click "Share" under the video.
HTML5 Geolocation
Previous
Next Chapter
Browser Support
Internet Explorer 9+, Firefox, Chrome, Safari and Opera support Geolocation.
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:
If supported, run the getCurrentPosition() method. If not, display a message to the user
If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter
( showPosition )
The showPosition() function gets the displays the Latitude and Longitude
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";
Try it yourself
In the example above we use the returned latitude and longitude data to show the location in a Google map (using a static image).
Google Map Script
How to use a script to show an interactive map with a marker, zoom and drag options.
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:
Property
Description
coords.latitude
coords.longitude
coords.accuracy
coords.altitude
coords.altitudeAccuracy
coords.heading
coords.speed
timestamp
watchPosition() - Returns the current position of the user and continues to return updated position as the user moves (like the GPS
in a car).
clearWatch() - Stops the watchPosition() method.
The example below shows the watchPosition() method. You need an accurate GPS device to test this (like iPhone):
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
Internet Explorer 9+, Firefox, Opera, Chrome, and Safari support drag and drop.
Note: Drag and drop does not work in Safari 5.1.7 and earlier versions.
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 this case, the data type is "text" and the value is the id of the draggable element ("drag1").
event.preventDefault()
Code explained:
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
More Examples
Drag image back and forth
How to drag (and drop) an image back and forth between two <div> elements.
Browser Support
Local storage is supported in Internet Explorer 8+, Firefox, Opera, Chrome, and Safari.
Note: Internet Explorer 7 and earlier versions, do not support Web Storage.
window.sessionStorage - stores data for one session (data is lost when the tab is closed)
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:
Retrieve the value of "lastname" and insert it into the element with id="result"
Note: Name/value pairs are always stored as strings. Remember to convert them to another format when needed!
The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number
to be able to increase the counter:
Example
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
Try it Yourself
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
With application cache it is easy to make an offline version of a web application, by creating a cache manifest file.
Offline browsing - users can use the application when they're offline
2.
3.
Reduced server load - the browser will only download updated/changed resources from the server
Browser Support
Internet Explorer 10, Firefox, Chrome, Safari and Opera support Application cache.
Example
<!DOCTYPE HTML>
<html manifest="demo.appcache">
<body>
The content of the document......
</body>
</html>
Try it Yourself
Every page with the manifest attribute specified will be cached when the user visits it. If the manifest attribute is not specified, the
page will not be cached (unless the page is specified directly in the manifest file).
The recommended file extension for manifest files is: ".appcache"
A manifest file needs to be served with the correct media type, which is "text/cache-manifest".
Must be configured on the web server.
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.
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.
A web worker is a JavaScript running in the background, without affecting the performance of the page.
Browser Support
Internet Explorer 10, Firefox, Chrome, Safari and Opera support Web workers.
Example
Count numbers:
Start Worker Stop Worker
Try it yourself
The important part of the code above is the postMessage() method - which is used to post a message back to the HTML page.
Note: Normally web workers are not used for such simple scripts, but for more CPU intensive tasks.
Then we can send and receive messages from the web worker.
Add an "onmessage" event listener to the web worker.
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
Server-Sent Events are supported in all major browsers, except Internet Explorer.
Example
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
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")
When an onmessage event occurs, put the received data into the element with id="result"
Code explained:
Events
Description
onopen
onmessage
onerror