HTML
HTML
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>
Try it yourself
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
The start tag is often called the opening tag. The end tag is often called the closing
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:
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
Only the <body> area (the white area) is displayed by the browser.
<!DOCTYPE html>
<!DOCTYPE HTML>
<!doctype html>
<!Doctype Html>
Common Declarations
HTML5
<!DOCTYPE html>
HTML 4.01
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
2014
HTML Documents
All HTML documents must start with a type declaration: <!DOCTYPE html>.
The HTML document itself begins with <html> and ends with </html>.
The visible part of the HTML document is between <body> and </body>.
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
Try it Yourself
HTML Headings
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
Try it yourself
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Try it yourself
HTML Links
HTML links are defined with the <a> tag:
Example
<a href="http://www.w3schools.com">This is a link</a>
Try it yourself
The link's destination is specified in the href attribute.
Attributes are used to provide additional information about HTML elements.
HTML Images
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142"
>
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>
<p>My first paragraph.</p>
</body>
</html>
Try it yourself
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The <body> element defines the document body.
<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>
Try it yourself
The example above works in all browsers, because the closing tag is considered
optional.
Never rely on this. It might produce unexpected results and/or errors if you
forget the end tag.
HTML Attributes
Previous
Next Chapter
HTML Attributes
<!DOCTYPE html>
<html lang="en-US">
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
The first two letters specify the language (en). If there is a dialect, use two
more letters (US).
Example
<p title="About W3Schools">
W3Schools is a web developer's site.
It provides tutorials and references covering
many aspects of web programming,
including HTML, CSS, JavaScript, XML, SQL, PHP, ASP, etc.
</p>
Try it Yourself
When you move the mouse over the element, the title will be displayed as a tooltip.
Example
<a href="http://www.w3schools.com">This is a link</a>
Try it yourself
You will learn more about links and the <a> tag later in this tutorial.
Size Attributes
Example
<img src="w3schools.jpg" width="104" height="142">
Try it Yourself
The image size is specified in pixels: width="104" means 104 screen pixels
wide.
You will learn more about images and the <img> tag later in this tutorial.
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142"
>
Try it Yourself
Example
<a href=http://www.gantaas.com>
Try it Yourself
W3C recommends quotes in HTML4, and demands quotes for stricter
document types like XHTML.
Sometimes it is necessary to use quotes. This will not display correctly,
because it contains a space:
Example
<p title=About santosh>
Try it Yourself
Using quotes are the most common. Omitting quotes can produce errors.
At W3Schools we always use quotes around attribute values.
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
HTML Headings
Headings are defined with the <h1> to <h6> tags.
<h1> defines the most important heading. <h6> defines the least important
heading.
Example
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
Try it yourself
Note: Browsers automatically add some empty space (a margin) before and
after each heading.
Example
<p>This is a paragraph.</p>
<hr>
<p>This is a paragraph.</p>
<hr>
<p>This is a paragraph.</p>
Try it yourself
Example
<!DOCTYPE html>
<html>
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
.
.
Try it Yourself
Meta data means data about data. HTML meta data is data about the HTML documen
Exercise 2
Exercise 3
Exercise 4
Tag
Description
<html>
<body>
<head>
<h1> to <h6>
<hr>
HTML Paragraphs
Previous
Next Chapter
HTML Paragraphs
The HTML <p> element defines a paragraph.
Example
<p>This is a paragraph</p>
<p>This is another paragraph</p>
Try it yourself
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
Example
<p>This is a paragraph
<p>This is another paragraph
Try it yourself
The example above will work in most browsers, but do not rely on it.
Forgetting the end tag can produce unexpected results or errors.
Stricter versions of HTML, like XHTML, do not allow you to skip the end tag.
Example
<p>This is<br>a para<br>graph with line breaks</p>
Try it yourself
The <br> element is an empty HTML element. It has no end tag.
Example
<pre>
My Bonnie lies over the ocean.
My Bonnie lies over the sea.
My Bonnie lies over the ocean.
Oh, bring back my Bonnie to me.
</pre>
Try it Yourself
Exercise 2
Exercise 3
Exercise 4
Tag
Description
<p>
Defines a paragraph
<br>
<pre>
HTML Styles
Previous
HTML Styles
Next Chapter
I am red
I am blue
Try it Yourself
style="property:value;"
The property is a CSS property. The value is a CSS value.
Example
<body style="background-color:lightgrey;">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
Try it Yourself
Example
<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
Try it Yourself
HTML Fonts
The font-family property defines the font to be used for an HTML element:
Example
<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>
Try it Yourself
Example
Example
<h1 style="text-align:center;">Centered Heading</h1>
<p>This is a paragraph.</p>
Try it Yourself
Chapter Summary
Text Formatting
Next Chapter
superscript
Bold text
Important text
Italic text
Emphasized text
Marked text
Small text
Deleted text
Inserted text
Subscripts
Superscripts
Example
<p>This text is normal.</p>
<p><b>This text is bold</b>.</p>
Try it Yourself
The HTML <strong> element defines strong text, with added semantic
"strong" importance.
Example
<p>This text is normal.</p>
<p><strong>This text is strong</strong>.</p>
Try it Yourself
Example
<p>This text is normal.</p>
<p><i>This text is italic</i>.</p>
Try it Yourself
The HTML <em> element defines emphasized text, with added semantic
importance.
Example
However, there is a difference in the meaning of these tags: <b> and <i> defines bold
but <strong> and <em> means that the text is "important".
HTML
Small
Formatting
Example
<h2>HTML <small>Small</small> Formatting</h2>
Try it Yourself
Example
<h2>HTML <mark>Marked</mark> Formatting</h2>
Try it Yourself
Example
<p>My favorite color is <del>blue</del> red.</p>
Try it Yourself
Example
<p>My favorite <ins>color</ins> is red.</p>
Try it Yourself
HTML
Subscript
Formatting
Example
<p>This is <sub>subscripted</sub> text.</p>
Try it Yourself
HTML
Superscript
Formatting
Example
<p>This is <sup>superscripted</sup> text.</p>
Try it Yourself
Exercise 2
Exercise 3
Exercise 4
Exercise 5
Description
<b>
<em>
<i>
<small>
<strong>
<sub>
<sup>
<ins>
<del>
<mark>
HTML Comments
Previous
Next Chapter
Comment tags <!-- and --> are used to insert comments in HTML.
Note: There is an exclamation point (!) in the opening tag, but not in the closing tag.
Comments are not displayed by the browser, but they can help document your
HTML.
With comments you can place notifications and reminders in your HTML:
Example
Example
<!-- Do not display this at the moment
<img border="0" src="pic_mountain.jpg" alt="Mountain">
-->
Try it Yourself
Conditional Comments
You might stumble upon conditional comments in HTML:
<!--[if IE 8]>
.... some HTML here ....
<![endif]-->
Conditional comments defines HTML tags to be executed by Internet Explorer
only.
As a rule, let these tags stay, to help support the software that created them.
HTML Links
Previous
Next Chapter
Links are found in nearly all web pages. Links allow users to click their way
from page to page.
Example
<a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a>
Try it Yourself
The href attribute specifies the destination address
(http://www.w3schools.com/html/)
The link text is the visible part (Visit our HTML tutorial).
Clicking on the link text, will send you to the specified address.
The link text does not have to be text. It can be an HTML image or any other HTML ele
Without a trailing slash on subfolder addresses, you might generate two requests to th
will automatically add a trailing slash to the address, and then create a new request.
Local Links
The example above used an absolute URL (A full web address).
A local link (link to the same web site) is specified with a relative URL (without
http://www....).
Example
<a href="html_images.asp">HTML Images</a>
Try it Yourself
Example
<style>
a:link {color:green; background-color:transparent; text-decoration:none}
a:visited {color:pink; background-color:transparent; text-decoration:none}
a:hover {color:red; background-color:transparent; textdecoration:underline}
a:active {color:yellow; background-color:transparent; textdecoration:underline}
</style>
Try it Yourself
Example
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!
</a>
Try it yourself
Target Value
Description
_blank
_self
Opens the linked document in the same frame as it was clicked (this
_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>
Try it Yourself
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial" style="width:42px;height:42px;border:0">
</a>
Try it Yourself
border:0 is added to prevent IE9 (and earlier) from displaying a border around the ima
Example
First, create a bookmark with the id attribute:
Example
<a href="html_tips.htm#tips">Visit the Useful Tips Section</a>
Try it yourself
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
Description
<a>
Defines a hyperlink
HTML IMAGES
<!DOCTYPE html>
<html>
<body>
<h2>Spectacular Mountain</h2>
<img src="pic_mountain.jpg" alt="Mountain
View" style="width:304px;height:228px;">
</body>
</html>
Try it Yourself
Always specify the width and height of an image. If width and height are not specified,
while the image loads.
Example
<img src="wrongname.gif" alt="HTML5
Icon" style="width:128px;height:128px;">
Try it Yourself
The alt attribute is required. A web page will not validate correctly without it.
Example
<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px;">
Try it Yourself
Alternatively, you can use width and height attributes. Here, the values are
specified in pixels by default:
Example
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
width:100%;
}
</style>
</head>
<body>
<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px;">
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
</body>
</html>
Try it Yourself
If not specified, the browser expects to find the image in the same folder as the
web page.
However, it is common to store images in a sub-folder. You must then include
the folder name in the src attribute:
Example
<img src="/images/html5.gif" alt="HTML5
Icon" style="width:128px;height:128px;">
Try it Yourself
Example
<img src="http://www.w3schools.com/images/w3schools_green.jpg" alt="W3
Schools.com">
Try it Yourself
Animated Images
The GIF standard allows animated images:
Example
<img src="programming.gif" alt="Computer
Man" style="width:48px;height:48px;">
Try it Yourself
Note that the syntax of inserting animated images is no different from nonanimated images.
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial" style="width:42px;height:42px;border:0;">
</a>
Try it Yourself
Add "border:0;" to prevent IE9 (and earlier) from displaying a border around the imag
Image Floating
Use the CSS float property to let the image float.
The image can float to the right or to the left of a text:
Example
<p>
<img src="smiley.gif" alt="Smiley
face" style="float:right;width:42px;height:42px;">
The image will float to the right of the text.
</p>
<p>
<img src="smiley.gif" alt="Smiley
face" style="float:left;width:42px;height:42px;">
Image Maps
Use the <map> tag to define an image-map. An image-map is an image with
clickable areas.
The name attribute of the <map> tag is associated with the <img>'s usemap
attribute and creates a relationship between the image and the map.
The <map> tag contains a number of <area> tags, that defines the clickable
areas in the image-map:
Example
<img src="planets.gif" alt="Planets" usemap="#planetmap" style="width:1
45px;height:126px;">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" alt="Sun" href="sun.htm">
<area shape="circle" coords="90,58,3" alt="Mercury" href="mercur.htm">
<area shape="circle" coords="124,58,8" alt="Venus" href="venus.htm">
</map>
Try it Yourself
Chapter Summary
Use the HTML src attribute to define the URL of the image
Use the HTML alt attribute to define an alternate text for an image, if it
cannot be displayed
Use the HTML width and height attributes to define the size of the image
Use the CSS width and height properties to define the size of the image
(alternatively)
Use the HTML <area> element to define the clickable areas in the imagemap
Loading images takes time. Large images can slow down your page. Use images carefu
Description
<img>
Defines an image
<map>
Defines an image-map
<area>
HTML Lists
Previous
Next Chapter
Item
Item
Item
Item
Ordered List:
1. First item
2. Second item
3. Third item
4. Fourth item
The list items will be marked with bullets (small black circles):
Example
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
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>
Try it Yourself
Circle:
<ul style="list-style-type:circle">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
Square:
<ul style="list-style-type:square">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
None:
<ul style="list-style-type:none">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Try it Yourself
Example
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
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>
Try it Yourself
Uppercase Letters:
<ol type="A">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
Lowercase Letters:
<ol type="a">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
Example
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
Try it Yourself
Example
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
Try it Yourself
List items can contain new list, and other HTML elements, like images and links, etc.
Horizontal Lists
HTML lists can be styled in many different ways with CSS.
One popular way, is to style a list to be displayed horizontally:
Example
<!DOCTYPE html>
<html>
<head>
<style>
ul#menu li {
display:inline;
}
</style>
</head>
<body>
<h2>Horizontal List</h2>
<ul id="menu">
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
<li>PHP</li>
</ul>
</body>
</html>
Try it Yourself
With a little extra style, you can make it look like a menu:
Example
ul#menu {
padding: 0;
}
ul#menu li {
display: inline;
}
ul#menu li a {
background-color: black;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 4px 4px 0 0;
}
ul#menu li a:hover {
background-color: orange;
}
Try it Yourself
Chapter Summary
Description
<ul>
<ol>
<li>
<dl>
<dt>
<dd>
HTML Tables
Previous
Next Chapter
First Name
Last Name
Eve
Jackson
John
Doe
Adam
Johnson
Jill
Smith
Try it yourself
Example explained:
Tables are defined with the <table> tag.
Tables are divided into table rows with the <tr> tag.
Table rows are divided into table data with the <td> tag.
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>
Try it Yourself
The border attribute is on its way out of the HTML standard! It is better to use CSS.
Example
table, th, td {
border: 1px solid black;
}
Try it Yourself
Remember to define borders for both the table and the table cells.
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
Try it Yourself
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
Try it Yourself
Example
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself
To left-align the table headings, use the CSS text-align property:
Example
th {
text-align: left;
}
Try it Yourself
Example
table {
border-spacing: 5px;
}
Try it Yourself
Example
<table style="width:100%">
<tr>
<th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
Try it Yourself
Example
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th>
<td>555 77 854</td>
</tr>
<tr>
<td>555 77 855</td>
</tr>
</table>
Try it Yourself
Example
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
Try it Yourself
The <caption> tag must be inserted immediately after the <table> tag.
Example
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
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 Forms
Previous
Next Chapter
<form>
.
form elements
.
</form>
HTML forms contain form elements.
Form elements are different types of input elements, checkboxes, radio buttons,
submit buttons, and more.
The <input> element has many variations, depending on the type attribute.
Here are the types used in this chapter:
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
This is how it will look like in a browser:
First name:
Last name:
Note: The form itself is not visible. Also note that the default width of a text
field is 20 characters.
Example
<form>
<input type="radio" name="sex" value="male" checked>Male
<br>
<input type="radio" name="sex" value="female">Female
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
Male
Female
Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
First name:
Mickey
Last name:
Mouse
Submit
<form action="action_page.php">
If the action attribute is omitted, the action is set to the current page.
action_page.php?firstname=Mickey&lastname=Mouse
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
This is how the HTML code above will be displayed in a browser:
Personal information:First name:
Mickey
Last name:
Mouse
Submit
Exercise 2
Exercise 3
Exercise 4
Attribute
Description
accept-charset
Specifies the charset used in the submitted form (default: the page chars
action
Specifies an address (url) where to submit the form (default: the submitt
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.for
novalidate
target
Specifies the target of the address in the action attribute (default: _self).
Next Chapter
Example
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Try it Yourself
The <option> elements defines the options to select.
The list will normally show the first item as selected.
You can add a selected attribute to define a predefined option.
Example
<option value="fiat" selected>Fiat</option>
Try it Yourself
Example
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
Example
<button type="button" onclick="alert('Hello World!')">Click Me!</button>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
Click Me!
<datalist>
<keygen>
<output>
By default, browsers do not display unknown elements. New elements will not destroy
The list attribute of the <input> element, must refer to the id attribute of the
<datalist> element.
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
<form action="action_page.php">
Username: <input type="text" name="user">
Encryption: <keygen name="security">
<input type="submit">
</form>
Try it Yourself
Example
Perform a calculation and show the result in an <output> element:
<form action="action_page.asp"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0
<input type="range" id="a" name="a" value="50">
100 +
<input type="number" id="b" name="b" value="50">
=
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
Try it Yourself
Exercise 2
Exercise 3
Tag
Description
<form>
<input>
<textarea>
<label>
<fieldset>
<legend>
<select>
<optgroup>
<option>
<button>
<datalist>
<keygen>
<output>
Next Chapter
Input Types
This chapter describes the input types of the <input> element.
Example
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
First name:
Last name:
Example
<form>
User name:<br>
<input type="text" name="username">
<br>
User password:<br>
<input type="password" name="psw">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
User name:
User password:
Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
First name:
Mickey
Last name:
Mouse
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
This is how the HTML code above will be displayed in a browser:
Male
Female
Example
<form>
<input type="checkbox" name="vehicle1" value="Bike"> I have a bike
<br>
<input type="checkbox" name="vehicle2" value="Car"> I have a car
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
I have a bike
I have a car
Example
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
Try it Yourself
This is how the HTML code above will be displayed in a browser:
color
date
datetime
datetime-local
month
number
range
search
tel
time
url
week
Input types, not supported by old web browsers, will behave as input type text.
Example
<form>
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
</form>
Try it Yourself
Input Restrictions
Here is a list of some common input restrictions (some are new in HTML5):
Attribute
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" valu
e="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.
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
The input type datetime is removed from the HTML standard. Use datetime-local instea
Example
<form>
Birthday (date and time):
<input type="datetime-local" name="bdaytime">
</form>
Try it Yourself
Example
<form>
E-mail:
<input type="email" name="email">
</form>
Try it Yourself
Example
<form>
Search Google:
<input type="search" name="googlesearch">
</form>
Try it Yourself
Example
<form>
Telephone:
<input type="tel" name="usrtel">
</form>
Try it Yourself
Example
<form>
Add your homepage:
<input type="url" name="homepage">
</form>
Try it Yourself
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 we
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):
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:
Example
Let the "First name" input field automatically get focus when the page loads:
Example
An input field located outside the HTML form (but still a part of the form):
Example
<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):
Example
The second submit button overrides the HTTP method of the form:
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
The height and width attributes specify the height and width of an <input>
element.
The height and width attributes are only used with <input type="image">.
Always specify the size of images. If the browser does not know the size, the page will
load.
Example
Define an image as the submit button, with height and width attributes:
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:
Example
A file upload field that accepts multiple values:
Example
An input field that can contain only three letters (no numbers or special
characters):
The hint is displayed in the input field before the user enters a value.
The placeholder attribute works with the following input types: text, search, url,
tel, email, and password.
Example
An input field with a placeholder text:
Example
A required input field:
The step attribute specifies the legal number intervals for an <input> element.
Example: if step="3", legal numbers could be -3, 0, 3, 6, etc.
Tip: The step attribute can be used together with the max and min attributes to
create a range of legal values.
The step attribute works with the following input types: number, range, date,
datetime, datetime-local, month, time and week.
Example
An input field with a specified legal number intervals: