HTML and CSS
HTML and CSS
The text between <title> and </title> provides a title for the
document
Only the <body> area (the white area) is displayed by the browser.
HTML Documents
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
HTML Headings
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
HTML Paragraphs
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
HTML Links
<a href="http://www.w3schools.com">This is a link</a>
HTML Images
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142"
>
HTML Attributes
o
When you move the mouse over the element, the title will be displayed as a tooltip.
HTML links are defined with the <a> tag. The link address is specified in
the href attribute:
Example
<a href="http://www.w3schools.com">This is a link</a>
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.
The alt attribute specifies an alternative text to be used, when an image cannot be
displayed.
The value of the attribute can be read by screen readers. This way, someone
"listening" to the webpage, e.g. a blind person, can "hear" the element.
Example
<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">
Try it Yourself
Using quotes are the most common. Omitting quotes can produce errors.
At W3Schools we always use quotes around attribute values.
Double style quotes are the most common in HTML, but single style can also be
used.
In some situations, when the attribute value itself contains double quotes, it is
necessary to use single quotes:
<p title='John "ShotGun" Nelson'>
Or vice versa:
Chapter Summary
The HTML width and height attributes provide size information for images
HTML Headings
Use HTML headings for headings only. Don't use headings to make text BIG or bold.
Search engines use your headings to index the structure and content of your web
pages.
Users skim your pages by its headings. It is important to use headings to show the
document structure.
h1 headings should be main headings, followed by h2 headings, then the less
important h3, and so on.
<head>
<title>My First HTML</title>
<meta charset="UTF-8">
</head>
<body>
.
.
.
Try it Yourself
Meta data means data about data. HTML meta data is data about the HTML document.
The HTML <title> element is meta data. It defines the HTML document's title.
The title will not be displayed in the document, but might be displayed in the
browser tab.
The HTML <link> element is used to define external CSS style sheets.
HTML Paragraphs
Browsers automatically add some white space before and after a paragraph.
HTML Display
You cannot be sure how HTML will be displayed.
Large or small screens, and resized windows will create different results.
With HTML, you cannot change the output by adding extra spaces or extra lines in
your HTML code.
The browser will remove extra spaces and extra lines when the page is displayed.
Any number of spaces, and any number of new lines, count as only one space.
Example
<p>
This paragraph
contains a lot of lines
in the source code,
but the browser
ignores it.
</p>
<p>
This paragraph
contains
a lot of spaces
in the source
code,
but the
browser
ignores it.
</p>
Try it Yourself
Stricter versions of HTML, like XHTML, do not allow you to skip the end tag.
Setting the style of an HTML element, can be done with the style attribute.
The HTML style attribute has the following syntax:
style="property:value;"
The property is a CSS property. The value is a CSS value.
The color property defines the text color for an HTML element:
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
The font-size property defines the text size for an HTML element:
Example
<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>
Try it Yourself
The text-align property defines the horizontal text alignment for an HTML element:
Example
<h1 style="text-align:center;">Centered Heading</h1>
<p>This is a paragraph.</p>
Try it Yourself
Chapter Summary
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
<p>This text is normal.</p>
<p><em>This text is emphasized</em>.</p>
Try it Yourself
However, there is a difference in the meaning of these tags: <b> and <i> defines bold and
but <strong> and <em> means that the text is "important".
Example
<h2>HTML <mark>Marked</mark> Formatting</h2>
Try it Yourself
subscripted
text.
Example
<p>This is <sub>subscripted</sub> text.</p>
Try it Yourself
superscripted
text.
Example
<p>This is <sup>superscripted</sup> text.</p>
HTML <q> for Short Quotations
The HTML <q> element defines a short quotation.
Browsers usually insert quotation marks around the <q> element.
Example
<p>WWF's goal is to: <q>Build a future where people live in harmony with
nature.</q></p>
Try it Yourself
Note: There is an exclamation point (!) in the opening tag, but not in the closing tag.
Comments are not displayed by the browser, but they can help document your
HTML.
With comments you can place notifications and reminders in your HTML:
Example
<!-- This is a comment -->
<p>This is a paragraph.</p>
<!-- Remember to add more information here -->
Try it Yourself
Comments are also great for debugging HTML, because you can comment out HTML
lines of code, one at a time, to search for errors:
Example
<!-- Do not display this at the moment
<img border="0" src="pic_mountain.jpg" alt="Mountain">
-->
Try it Yourself
Try it Yourself
color:green;
}
CSS Fonts
The CSS color property defines the text color to be used for the HTML element.
The CSS font-family property defines the font to be used for the HTML element.
The CSS font-size property defines the text size to be used for the HTML element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p {
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Try it Yourself
Every 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;
}
Try it Yourself
The CSS padding property defines a padding (space) inside the border:
Example
p{
border: 1px solid black;
padding: 10px;
}
Try it Yourself
The CSS margin property defines a margin (space) outside the border:
Example
p{
border: 1px solid black;
padding: 10px;
margin: 30px;
}
Try it Yourself
The id Attribute
All the examples above use CSS to style HTML elements in a general way.
To define a special style for one special element, first add an id attribute to the
element:
<p id="p01">I am different</p>
then define a style for the element with the specific id:
Example
#p01 {
color: blue;
}
Try it Yourself
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
The link text does not have to be text. It can be an HTML image or any other HTML element
Without a trailing slash on subfolder addresses, you might generate two requests to the se
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
Target Value
Description
_blank
_self
Opens the linked document in the same frame as it was clicked (this is d
_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
border:0 is added to prevent IE9 (and earlier) from displaying a border around the image.
When the link is clicked, the page will scroll to the location with the bookmark.
Example
First, create a bookmark with the id attribute:
<h2 id="tips">Useful Tips Section</h2>
Then, add a link to the bookmark ("Useful Tips Section"), from within the same
page:
<a href="#tips">Visit the Useful Tips Section</a>
Or, add a link to the bookmark ("Useful Tips Section"), from another page:
Example
<a href="html_tips.html#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
<!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, the
the image loads.
The <img> tag is empty, it contains attributes only, and does not have a
closing tag.
The src attribute specifies the URL (web address) of the image:
The alt attribute specifies an alternate text for an image, if the image cannot
be displayed.
The alt attribute provides alternative information for an image if a user for
some reason cannot view it (because of slow connection, an error in the src
attribute, or if the user uses a screen reader).
Example
Try it Yourself
The alt attribute is required. A web page will not validate correctly without it.
Screen readers are useful to people who are blind, visually impaired, or
learning disabled.
You can use the style attribute to specify the width and height of an image.
Example
Try it Yourself
Alternatively, you can use width and height attributes. Here, the values are
specified in pixels by default:
Example
Try it Yourself
Both the width, height, and style attributes are valid in the latest HTML5
standard.
We suggest you use the style attribute. It prevents styles sheets from
changing the original size of images:
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.
Example
Try it Yourself
Actually, you can access images from any web address in the world:
Example
Number
First Name
Last Name
Eve
Jackson
John
Doe
Adam
Johnson
Jill
Smith
Example
<table 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
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.
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Try it Yourself
The border attribute is on its way out of the HTML standard! It is better to use CSS.
To add borders, use the CSS border property:
Example
table, th, td {
border: 1px solid black;
}
Try it Yourself
Remember to define borders for both the table and the table cells.
Cell padding specifies the space between the cell content and its borders.
If you do not specify a padding, the table cells will be displayed without padding.
To set the padding, use the CSS padding property:
Example
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
}
Try it Yourself
Example
th {
text-align: left;
}
Try it Yourself
</tr>
</table>
Try it Yourself
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
Try it Yourself
The <caption> tag must be inserted immediately after the <table> tag.
}
table#t01 tr:nth-child(odd) {
background-color: #fff;
}
table#t01 th {
color: white;
background-color: black;
}
Try it Yourself
Chapter Summary
Use the CSS border-spacing property to set the spacing between cells
HTML Lists
Previous
Next Chapter
Example of an unordered list and an ordered list in HTML:
Unordered List:
Item
Item
Item
Item
Ordered List:
1. First item
2. Second item
3. Third item
4. Fourth item
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
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
Uppercase Roman Numbers:
<ol type="I">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Try it Yourself
Lowercase Roman Numbers:
<ol type="i">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
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
Every HTML element has a default display value depending on what type of element
it is. The default display value for most elements is block or inline.
Block-level Elements
A block-level element always starts on a new line and takes up the full width
available (stretches out to the left and right as far as it can).
The <div> element is a block-level element.
<div>
<h1> - <h6>
<p>
<form>
Inline Elements
An inline element does not start on a new line and only takes up as much width as
necessary.
This is an inline <span> element inside a paragraph.
Examples of inline elements:
<span>
<a>
<img>
Example
<div style="background-color:black; color:white; padding:20px;">
<h2>London</h2>
<p>London is the capital city of England. It is the most populous city in the United
Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
</div>
Try it Yourself
Example
<!DOCTYPE html>
<html>
<head>
<style>
div.cities {
background-color: black;
color: white;
margin: 20px 0 20px 0;
padding: 20px;
}
</style>
</head>
<body>
<div class="cities">
<h2>London</h2>
<p>London is the capital 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
London
London is the capital 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.
</head>
<body>
<h1>My <span class="note">Important</span> Heading</h1>
<p>This is some <span class="note">important</span> text.</p>
</body>
</html>
Try it Yourself
HTML Forms
Previous
Next 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 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
GET is best suited to short amounts of data. Size limitations are set in your browser.
Last name:
Mouse
Submit
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 p
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.
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
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
<datalist>
<keygen>
<output>
By default, browsers do not display unknown elements. New elements will not destroy your
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
The public key could be used to generate a client certificate to authenticate the
user in the future.
Example
A form with a keygen field:
<form action="action_page.php">
Username: <input type="text" name="user">
Encryption: <keygen name="security">
<input type="submit">
</form>
Try it Yourself
Example
Perform a calculation and show the result in an <output> element:
<form action="action_page.asp"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0
<input type="range" id="a" name="a" value="50">
100 +
<input type="number" id="b" name="b" value="50">
=
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
Try it Yourself
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.
Last 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
Radio buttons let a user select ONLY ONE of a limited number of choices:
Example
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
Try it Yourself
This is how the HTML code above will be displayed in a browser:
Male
Female
Other
I have a bike
I have a car
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" value="30
">
</form>
Try it Yourself
Example
<form>
Birthday:
<input type="date" name="bday">
</form>
Try it Yourself
You can add restrictions to the input:
Example
<form>
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31"><br>
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02"><br>
</form>
Try it Yourself
The <input type="color"> is used for input fields that should contain a color.
Depending on browser support, a color picker can show up in the input field.
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 instead.
Example
<form>
Birthday (date and time):
<input type="datetime-local" name="bdaytime">
</form>
Try it Yourself
The <input type="email"> is used for input fields that should contain an e-mail
address.
Depending on browser support, the e-mail address can be automatically validated
when submitted.
Some smartphones recognize the email type, and adds ".com" to the keyboard to
match email input.
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
The value Attribute
The value attribute specifies the initial value for an input field:
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
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
Example
Let the "First name" input field automatically get focus when the page loads:
First name:<input type="text" name="fname" autofocus>
Try it Yourself
The form attribute specifies one or more forms an <input> element belongs to.
Tip: To refer to more than one form, use a space-separated list of form ids.
Example
An input field located outside the HTML form (but still a part of the form):
<form action="action_page.php" id="form1">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
Last name: <input type="text" name="lname" form="form1">
Try it Yourself
Example
An HTML form with two submit buttons, with different actions:
<form action="action_page.php">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formaction="demo_admin.asp"
value="Submit as admin">
</form>
Try it Yourself
Example
Send form-data that is default encoded (the first submit button), and encoded as
"multipart/form-data" (the second submit button):
<form action="demo_post_enctype.asp" method="post">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
<input type="submit" formenctype="multipart/form-data"
value="Submit as Multipart/form-data">
</form>
Try it Yourself
Example
The second submit button overrides the HTTP method of the form:
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
The formtarget attribute overrides the target attribute of the <form> element.
The formtarget attribute can be used with type="submit" and type="image".
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 flicke
Example
Define an image as the submit button, with height and width attributes:
<input type="image" src="img_submit.gif" alt="Submit" width="48" height="48">
Try it Yourself
Example
An <input> element with pre-defined values in a <datalist>:
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
Try it Yourself
Example
<input> elements with min and max values:
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31">
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02">
Example
A file upload field that accepts multiple values:
Select images: <input type="file" name="img" multiple>
Try it Yourself
Example
An input field that can contain only three letters (no numbers or special characters):
Country code: <input type="text" name="country_code" pattern="[A-Za-z]
{3}" title="Three letter country code">
Try it Yourself
Example
An input field with a placeholder text:
<input type="text" name="fname" placeholder="First name">
Try it Yourself
Example
A required input field:
Username: <input type="text" name="usrname" required>
Try it Yourself
Example
An input field with a specified legal number intervals:
<input type="number" name="points" step="3">
Try it Yourself