HTML Notes PDF
HTML Notes PDF
Basic Tutorials
By Akshay Bhagade
Contents
HTML Introduction
----------------------------------------------------------------------------------- 2
HTML Quotation and Citation Elements
----------------------------------------------------- 17
HTML Colors
------------------------------------------------------------------------------------------ 19
HTML Links-------------------------------------------------------------------------------------------- 35
HTML Images
----------------------------------------------------------------------------------------- 40
HTML Tables
------------------------------------------------------------------------------------------ 43
HTML Lists--------------------------------------------------------------------------------------------- 48
HTML Block and Inline Elements
--------------------------------------------------------------- 52
HTML The class Attribute
------------------------------------------------------------------------- 53
HTML The id Attribute
----------------------------------------------------------------------------- 57
My Header--------------------------------------------------------------------------------------------- 57
HTML Iframes
----------------------------------------------------------------------------------------- 59
HTML File Paths
-------------------------------------------------------------------------------------- 61
HTML Head-------------------------------------------------------------------------------------------- 62
HTML Symbols
--------------------------------------------------------------------------------------- 65
HTML Forms
------------------------------------------------------------------------------------------- 69
HTML Form Elements
------------------------------------------------------------------------------ 74
HTML Input Types
----------------------------------------------------------------------------------- 78
HTML Input Attributes
----------------------------------------------------------------------------- 86
1
By Akshay Bhagade
HTML Introduction
What is HTML?
HTML is the standard markup language for creating Web pages.
2
By Akshay Bhagade
Example Explained
The <!DOCTYPE declaration defines this document to be HTML5
The element
html> is the root element of an HTML page
<html>
The element
<head> contains meta information about the document
The element specifies a title for the document
<title>
The element contains the visible page content
<body>
The <h1>element defines a large heading
The <p>element defines a paragraph
HTML Tags
HTML tags are element names surrounded by angle brackets:
3
By Akshay Bhagade
It must only appear once, at the top of the page (before any HTML tags).
HTML Headings
HTML headings are defined with the <h1>
to <h6> tags.
<h1> defines the most important heading.
<h6> defines the least important heading:
4
By Akshay Bhagade
HTML Paragraphs
HTML paragraphs are defined with the <p> tag:
HTML Links
HTML links are defined with the <a> tag:
5
By Akshay Bhagade
HTML Images
HTML images are defined with the <img> tag.
The source file (src), alternative text ( alt), width, and height are provided as
attributes:
HTML Lists
HTML lists are defined with the <ul> (unordered/bullet list) or
the <ol>(ordered/numbered list) tag, followed by <li> tags (list items):
6
By Akshay Bhagade
Empty elements can be "closed" in the opening tag like this: <br />.
HTML5 does not require empty elements to be closed. But if you want stricter
validation, or if you need to make your document readable by XML parsers, you
must close all HTML elements properly.
7
By Akshay Bhagade
8
By Akshay Bhagade
HTML uses elements like <b> and <i> for formatting output, like bold or italic text.
9
By Akshay Bhagade
HTML Attributes
All HTML elements can have attributes
Attributes provide additional information about an element
Attributes are always specified in the start tag
Attributes usually come in name/value pairs like: name="value"
Example
<a href="https://www.....com">This is a link</a>
Example
<img src="img_girl.jpg">
Example
<img src="img_girl.jpg" width="500" height="600">
The width and height are is specified in pixels by default; so width="500" means
500 pixels wide.
You will learn more about images in our HTML Images chapter.
10
By Akshay Bhagade
The value of the alt attribute can be read by screen readers. This way, someone
"listening" to the webpage, e.g. a vision impaired person, can "hear" the element.
Example
<img src="img_girl.jpg" alt="Girl with a jacket">
The alt attribute is also useful if the image cannot be displayed (e.g. if it does not
exist):
Example
<p style="color:red">This is a paragraph.</p>
You will learn more about styling later in this tutorial, and in our CSS Tutorial.
<!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
The first two letters specify the language (en). If there is a dialect, add two more
letters (US).
11
By Akshay Bhagade
Example
<p title="I'm a tooltip"> This
is a paragraph.
</p>
The title attribute can be written with uppercase or lowercase like title or TITLE.
Bad
<a href=https://www.....com>
Good
<a href="https://www.....com">
recommends quotes in HTML, and demands quotes for stricter document types like
XHTML.
Sometimes it is necessary to use quotes. This example will not display the title
attribute correctly, because it contains a space:
Example
<p title=About >
Using quotes are the most common. Omitting quotes can produce errors.
At we always use quotes around attribute values.
12
By Akshay Bhagade
In some situations, when the attribute value itself contains double quotes, it is
necessary to use single quotes:
Or vice versa:
13
By Akshay Bhagade
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 any extra spaces and extra lines when the page is
displayed:
Problem
This poem will display on a single line:
The text inside a <pre> element is displayed in a fixed-width font (usually Courier),
and it preserves both spaces and line breaks:
14
By Akshay Bhagade
<tagname style="property:value;">
Background Color
The CSS background-color property defines the background color for an HTML
element.
Example
<body style="background-color:powderblue;">
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
15
By Akshay Bhagade
Text Color
The CSS 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>
Fonts
The CSS 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>
16
By Akshay Bhagade
Text Size
The CSS 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>
Text Alignment
The CSStext-align property defines the horizontal text alignment for an HTML
element:
Example
<h1 style="text-align:center;">Centered Heading</h1> <p
style="text-align:left;">left paragraph.</p>
17
By Akshay Bhagade
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>
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>
Example
<p>The <abbr title="World Health Organization">WHO</abbr> was founded in
1948.</p>
18
By Akshay Bhagade
Example
<address>
Written by John Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</address>
italic.
Example
<p><cite>The Scream</cite> by Edvard Munch. Painted in 1893.</p>
Example
<bdo dir="rtl">This text will be written from right to left</bdo>
HTML Colors
HTML colors are specified using predefined color names, or RGB, HEX, HSL,
RGBA, HSLA values.
Color Names
In HTML, a color can be specified by using a color name:
Tomato
19
By Akshay Bhagade
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
Background Color
You can set the background color for HTML elements:
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad
minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip
ex ea commodo consequat.
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1> <p
style="background-color:Tomato;">Lorem ipsum...</p>
20
By Akshay Bhagade
Text Color
You can set the color of text:
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.
Example
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Border Color
You can set the color of borders:
Hello World
Hello World
Hello World
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1> <h1
style="border:2px solid Violet;">Hello World</h1>
Color Values
In HTML, colors can also be specified using RGB values, HEX values, HSL values,
RGBA values, and HSLA values:
#ff6347
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
RGB Value
In HTML, a color can be specified as an RGB value, using this formula:
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest
value (255) and the others are set to 0.
To display black, set all color parameters to 0, like this: rgb(0, 0, 0).
To display white, set all color parameters to 255, like this: rgb(255, 255, 255).
22
By Akshay Bhagade
Example
rgb(255, 0, 0)
rgb(0, 0, 255)
rgb(255, 165, 0)
Example
rgb(0, 0, 0)
23
By Akshay Bhagade
HEX Value
In HTML, a color can be specified using a hexadecimal value in the form:
#rrggbb
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff
(same as decimal 0-255).
For example, #ff0000 is displayed as red, because red is set to its highest value
(ff) and the others are set to the lowest value (00).
Example
#ff0000
#0000ff
#3cb371
24
By Akshay Bhagade
#ee82ee
#ffa500
#6a5acd
Shades of gray are often defined using equal values for all the 3 light sources:
Example
#000000
#3c3c3c
#787878
#b4b4b4
#f0f0f0
#ffffff
25
By Akshay Bhagade
HSL Value
In HTML, a color can be specified using hue, saturation, and lightness (HSL) in the
form:
Saturation is a percentage value, 0% means a shade of gray, and 100% is the full
color.
Lightness is also a percentage, 0% is black, 50% is neither light or dark, 100% is
white
Example
26
By Akshay Bhagade
Saturation
Saturation can be described as the intensity of a color.
50% is 50% gray, but you can still see the color.
Example
27
By Akshay Bhagade
Example
Example
28
By Akshay Bhagade
RGBA Value
RGBA color values are an extension of RGB color values with an alpha channel -
which specifies the opacity for a color.
29
By Akshay Bhagade
HSLA Value
HSLA color values are an extension of HSL color values with an alpha channel -
which specifies the opacity for a color.
The most common way to add CSS, is to keep the styles in separate CSS files.
However, here we will use inline and internal styling, because this is easier to
demonstrate, and easier for you to try it yourself.
30
By Akshay Bhagade
Tip: You can learn much more about CSS in our CSS Tutorial.
Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.
This example sets the text color of the <h1> element to blue:
Example
<h1 style="color:blue;">This is a Blue Heading</h1>
Internal CSS
An internal CSS is used to define a style for a single HTML page.
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
External CSS
An external style sheet is used to define the style for many HTML pages.
31
By Akshay Bhagade
With an external style sheet, you can change the look of an entire web
site, by changing one file!
To use an external style sheet, add a link to it in the <head> section of the HTML
page:
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>
An external style sheet can be written in any text editor. The file must not contain
any HTML code, and must be saved with a .css extension.
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p{
color: red;
}
CSS Fonts
The CSScolor property defines the text color to be used.
The CSSfont-family
property defines the font to be used.
32
By Akshay Bhagade
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>
CSS Border
The CSS border property defines a border around an HTML element:
Example
p{
border: 1px solid powderblue;
}
CSS Padding
The CSSpaddingproperty defines a padding (space) between the text and the
border:
Example
p{
border: 1px solid powderblue;
padding: 30px;
}
33
By Akshay Bhagade
CSS Margin
The CSS marginproperty defines a margin (space) outside the border:
Example
p{
border: 1px solid powderblue;
margin: 50px;
}
The id Attribute
To define a specific style for one special element, add anid attribute to the
element:
then define a style for the element with the specific id:
Example
#p01 {
color: blue;
}
then define a style for the elements with the specific class:
Example
p.error {
color: red;
}
34
By Akshay Bhagade
External
References
External style sheets can be referenced with a full URL or with a path relative to the
current web page.
Example
<link rel="stylesheet" href="https://www.w3schools.com/html/styles.css">
This example links to a style sheet located in the html folder on the current web
site:
Example
<link rel="stylesheet" href="/html/styles.css">
This example links to a style sheet located in the same folder as the current page:
Example
<link rel="stylesheet" href="styles.css">
You can read more about file paths in the chapter HTML File Paths.
HTML Links
Links are found in nearly all web pages. Links allow users to click their way from
page to page.
When you move the mouse over a link, the mouse arrow will turn into a little hand.
Note: A link does not have to be text. It can be an image or any other HTML
element.
35
By Akshay Bhagade
Example
<a href="https://www.w3schools.com/html/">Visit our HTML tutorial</a>
The link text is the visible part (Visit our HTML tutorial).
Clicking on the link text will send you to the specified address.
Note: Without a forward slash at the end of subfolder addresses, you might
generate two requests to the server. Many servers will automatically add a forward
slash to the end of 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
https://www....).
Example
<a href="html_images.asp">HTML Images</a>
Example
<style>
a:link {
color: green;
36
By Akshay Bhagade
background-color: transparent;
text-decoration: none;
}
a:visited {
color: pink;
background-color: transparent;
text-decoration: none;
}
a:hover {
color: red;
background-color: transparent;
text-decoration: underline;
}
a:active {
color: yellow;
background-color: transparent;
text-decoration: underline;
}
</style>
This is a link
Example
<style>
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
</style>
37
By Akshay Bhagade
_blank - Opens the linked document in a new window or tab
_self - Opens the linked document in the same window/tab as it was clicked
(this is default)
_parent - Opens the linked document in the parent frame
_top - Opens the linked document in the full body of the window
framename - Opens the linked document in a named frame
This example will open the linked document in a new browser window/tab:
Example
<a href="https://www.w3schools.com/" target="_blank">Visit W3Schools!</a>
Tip: If your webpage is locked in a frame, you can use oftarget="_top" to break out
the frame:
Example
<a href="https://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>
Note: border:0; is added to prevent IE9 (and earlier) from displaying a border
around the image (when the image is a link).
Link Titles
The title attribute specifies extra information about an element. The information
is most often shown as a tooltip text when the mouse moves over the element.
Example
<a href="https://www.w3schools.com/html/" title="Go to W3Schools HTML
section">Visit our HTML Tutorial</a>
38
By Akshay Bhagade
Example
First, create a bookmark with the id attribute:
Then, add a link to the bookmark ("Jump to Chapter 4"), from within the same
page:
Or, add a link to the bookmark ("Jump to Chapter 4"), from another page:
Example
<a href="html_demo.html#C4">Jump to Chapter 4</a>
External Paths
External pages can be referenced with a full URL or with a path relative to the
current web page.
Example
<a href="https://www.w3schools.com/html/default.asp">HTML tutorial</a>
This example links to a page located in the html folder on the current web site:
Example
<a href="/html/default.asp">HTML tutorial</a>
This example links to a page located in the same folder as the current page:
Example
<a href="default.asp">HTML tutorial</a>
39
By Akshay Bhagade
HTML Images
<img src="img_girl.jpg" alt="Girl in a jacket">
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:
<img src="url">
Example
<img src="img_chania.jpg" alt="Flowers in Chania">
If a browser cannot find an image, it will display the value of the alt attribute:
Example
<img src="wrongname.gif" alt="Flowers in Chania">
Note: Thealt attribute is required. A web page will not validate correctly without
it.
However, we suggest using the style attribute. It prevents styles sheets from
changing the size of images:
40
By Akshay Bhagade
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
}
</style>
</head>
<body>
Image as a Link
To use an image as a link, put the <img>tag inside the <a> tag:
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML
tutorial" style="width:42px;height:42px;border:0;"> </a>
Note: border:0; is added to prevent IE9 (and earlier) from displaying a border
around the image (when the image is a link).
Image Floating
Use the CSSfloat property to let the image 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>
41
By Akshay Bhagade
Image Maps
The <map> tag defines an image-map. An image-map is an image with clickable
areas.
In the image below, click on the computer, the phone, or the cup of coffee:
Example
<img src="workplace.jpg" alt="Workplace" usemap="#workmap">
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="Computer" href="computer.htm">
<area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
<area shape="circle" coords="337,300,44" alt="Coffee" href="coffee.htm">
</map>
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> element contains a number of <area> tags, that define the clickable
areas in the image-map.
42
By Akshay Bhagade
HTML Tables
HTML Table Example
Each table row is defined with the <tr> tag. A table header is defined with
the <th> tag. By default, table headings are bold and centered. A table data/cell is
defined with the <td> tag.
43
By Akshay Bhagade
Example
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Note: The <td> elements are the data containers of the table.
They can contain all sorts of HTML elements; text, images, lists, other tables, etc.
Example
table, th, td {
border: 1px solid black;
}
Remember to define borders for both the table and the table cells.
Example
table, th, td {
border: 1px solid black;
44
By Akshay Bhagade
border-collapse: collapse; }
If you do not specify a padding, the table cells will be displayed without padding.
Example
th, td {
padding: 15px;
}
Example
th {
text-align: left;
}
To set the border spacing for a table, use the CSS border-spacing property:
Example
table {
border-spacing: 5px;
}
45
By Akshay Bhagade
Example
<table style="width:100%">
<tr>
<th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>55577854</td>
<td>55577855</td>
</tr>
</table>
Example
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th>
<td>55577854</td>
</tr>
<tr>
<td>55577855</td>
</tr>
</table>
Example
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
46
By Akshay Bhagade
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
Note: The <caption> tag must be inserted immediately after the <table> tag.
Example
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
47
By Akshay Bhagade
HTML Lists
Unordered HTML List
An unordered list starts with the <ul> tag. Each list item starts with the
<li> tag.
The list items will be marked with bullets (small black circles) by default:
Example
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Value Description
Example - Disc
<ul style="list-style-type:disc;">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
48
By Akshay Bhagade
Example
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Type Description
type="I" The list items will be numbered with uppercase roman numbers
type="i" The list items will be numbered with lowercase roman numbers
Numbers:
<ol type="1">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Example
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
49
By Akshay Bhagade
Example
<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li>
<li>Milk</li>
</ul>
Note: List items can contain new list, and other HTML elements, like images and
links, etc.
Example
<ol start="50">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
Example
50
By Akshay Bhagade
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 16px;
text-decoration: none;
}
li a:hover {
background-color: #111111;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
51
By Akshay Bhagade
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).
Example
<div>Hello World</div>
Inline Elements
An inline element does not start on a new line and only takes up as much width as
necessary.
Example
<span>Hello World</span>
52
By Akshay Bhagade
The <div> element has no required attributes, but style, class and id are
common.
When used together with CSS, the <div> element can be used to style blocks of
content:
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>
The <span> element has no required attributes, but style, class and id are
common.
When used together with CSS, the <span> element can be used to style parts of
the text:
Example
<h1>My <span style="color:red">Important</span> Heading</h1>
Here we have three <div> elements that point to the same class name:
53
By Akshay Bhagade
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 of England.</p>
</div>
<div class="cities">
<h2>Paris</h2>
<p>Paris is the capital of France.</p>
</div>
<div class="cities">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
</div>
</body>
</html>
Result:
London
London is the capital of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
54
By Akshay Bhagade
Example
<!DOCTYPE html>
<html>
<head>
<style>
span.note {
font-size: 120%;
color: red;
}
</style>
</head>
<body>
Example
Use CSS to style all elements with the class name "city":
<style>
.city {
background-color: tomato;
color: white;
padding: 10px;
}
</style>
<h2 class="city">London</h2>
<p>London is the capital of England.</p>
<h2 class="city">Paris</h2>
55
By Akshay Bhagade
<p>Paris is the capital of France.</p>
<h2 class="city">Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
Result:
London
London is the capital of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
Multiple Classes
HTML elements can have more than one class name, each class name must be
separated by a space.
Example
Style elements with the class name "city", also style elements with the class name
"main":
In the example above, the first<h2> element belongs to both the "city" class and
the "main" class.
Example
<h2 class="city">Paris</h2>
<p class="city">Paris is the capital of France</p>
56
By Akshay Bhagade
The id value can be used by CSS and JavaScript to perform certain tasks for the
element with the specific id value.
In CSS, to select an element with a specific id, write a hash (#) character, followed
by the id of the element:
Example
Use CSS to style an element with the id "myHeader":
<style>
#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
}
</style>
Result:
My Header
Tip: The id attribute can be used on any HTML element.
Note: The id value must contain at least one character, and must not contain
whitespace (spaces, tabs, etc.).
57
By Akshay Bhagade
Example
<style>
/* Style the element with the id "myHeader" */
#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
}
<h2 class="city">Paris</h2>
<p>Paris is the capital of France.</p>
<h2 class="city">Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
To make a bookmark, you must first create the bookmark, and then add a link to
it.
When the link is clicked, the page will scroll to the location with the bookmark.
Example
First, create a bookmark with the id attribute:
58
By Akshay Bhagade
Then, add a link to the bookmark ("Jump to Chapter 4"), from within the same
page:
Or, add a link to the bookmark ("Jump to Chapter 4"), from another page:
Example
<a href="html_demo.html#C4">Jump to Chapter 4</a>
Example
Use the id attribute to manipulate text with JavaScript:
<script>
function displayResult() {
document.getElementById("myHeader").innerHTML = "Have a nice day!";
}
</script>
HTML Iframes
Iframe Syntax
An HTML iframe is defined with the <iframe> tag:
<iframe src="URL"></iframe>
The src attribute specifies the URL (web address) of the inline frame page.
59
By Akshay Bhagade
Example
<iframe src="demo_iframe.htm" height="200" width="300"></iframe>
Example
<iframe src="demo_iframe.htm" height="100%" width="100%"></iframe>
Or you can use CSS to set the height and width of the iframe:
Example
<iframe src="demo_iframe.htm" style="height:200px;width:300px;"></iframe>
To remove the border, add the style attribute and use the CSS
border property:
Example
<iframe src="demo_iframe.htm" style="border:none;"></iframe>
With CSS, you can also change the size, style and color of the iframe's border:
Example
<iframe src="demo_iframe.htm" style="border:2px solid red;"></iframe>
The target attribute of the link must refer to thenameattribute of the iframe:
60
By Akshay Bhagade
Example
<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
Web pages
Images
Style sheets
JavaScripts
61
By Akshay Bhagade
Example
<img src="https://www.w3schools.com/images/picture.jpg" alt="Mountain">
In this example, the file path points to a file in the images folder located at the root
of the current web:
Example
<img src="/images/picture.jpg" alt="Mountain">
In this example, the file path points to a file in the images folder located in the
current folder:
Example
<img src="images/picture.jpg" alt="Mountain">
In this example, the file path points to a file in the images folder located in the
folder one level above the current folder:
Example
<img src="../images/picture.jpg" alt="Mountain">
HTML Head
The HTML <head> Element
The <head> element is a container for metadata (data about data) and is
placed between the <html> tag and the <body> tag.
HTML metadata is data about the HTML document. Metadata is not displayed.
Metadata typically define the document title, character set, styles, scripts, and
other meta information.
The following tags describe
metadata: <title>, <style>, <meta>, <link>, <script>, and <base>.
62
By Akshay Bhagade
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
The content of the document......
</body>
</html>
Example
<style>
body {background-color: powderblue;}
h1 {color: red;}
p {color: blue;}
</style>
Example
<link rel="stylesheet" href="mystyle.css">
63
By Akshay Bhagade
<meta charset="UTF-8">
Example of <meta>tags:
Example
<meta charset="UTF-8">
<meta name="description" content="Free Web tutorials">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="John Doe">
The viewport is the user's visible area of a web page. It varies with the device, and
will be smaller on a mobile phone than on a computer screen.
You should include the following <meta> viewport element in all your web pages:
64
By Akshay Bhagade
A <meta> viewport element gives the browser instructions on how to control the
page's dimensions and scaling.
The width=device-width part sets the width of the page to follow the screen-width
of the device (which will vary depending on the device).
The initial-scale=1.0 part sets the initial zoom level when the page is first loaded
by the browser.
Here is an example of a web page without the viewport meta tag, and the same
web page with the viewport <meta>tag:
This JavaScript writes "Hello JavaScript!" into an HTML element with id="demo":
Example
<script>
function myFunction {
document.getElementById("demo").innerHTML = "Hello JavaScript!";
}
</script>
Example
<base href="https://www.w3schools.com/images/" target="_blank">
HTML Symbols
HTML Symbol Entities
HTML entities were described in the previous chapter.
Many mathematical, technical, and currency symbols, are not present on a normal
keyboard.
To add such symbols to an HTML page, you can use an HTML entity name.
65
By Akshay Bhagade
If no entity name exists, you can use an entity number, a decimal, or hexadecimal
reference.
Example
<p>I will display €</p>
<p>I will display €</p>
<p>I will display €</p>
66
By Akshay Bhagade
67
By Akshay Bhagade
68
By Akshay Bhagade
HTML Forms
The <form> Element
The HTML <form>element defines a form that is used to collect user input:
<form>
.
form elements
.
</form>
Form elements are different types of input elements, like text fields, checkboxes,
radio buttons, submit buttons, and more.
Type Description
69
By Akshay Bhagade
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>
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="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
70
By Akshay Bhagade
This is how the HTML code above will be displayed in a browser:
Male
Female
Other
The form-handler is typically a server page with a script for processing input data.
First name:
Mickey
Last name:
Mouse
Submit
Normally, the form data is sent to a web page on the server when the user clicks
on the submit button.
In the example above, the form data is sent to a page on the server called
"/action_page.php". This page contains a server-side script that handles the form
data:
71
By Akshay Bhagade
<form action="/action_page.php">
If the action attribute is omitted, the action is set to the current page.
Example
<form action="/action_page.php" target="_blank">
Other legal values are "_parent", " _top", or a name representing the name of an
iframe.
Example
<form action="/action_page.php" method="get">
or:
Example
<form action="/action_page.php" method="post">
However, when GET is used, the submitted form data will be visible in the page
address field:
/action_page.php?firstname=Mickey&lastname=Mouse
Notes on GET:
72
By Akshay Bhagade
Appends form-data into the URL in name/value pairs
The length of a URL is limited (about 3000 characters)
Never use GET to send sensitive data! (will be visible in the URL)
Useful for form submissions where a user wants to bookmark the result
GET is better for non-secure data, like query strings in Google
Notes on POST:
POST has no size limitations, and can be used to send large amounts of data.
Form submissions with POST cannot be bookmarked
If the name attribute is omitted, the data of that input field will not be sent at all.
This example will only submit the "Last name" input field:
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>
Example
<form action="/action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
73
By Akshay Bhagade
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
Last name:
Mouse
Submit
Example
<input name="firstname" type="text">
If the type attribute is omitted, the input field gets the default type: "text".
All the different input types are covered in the next chapter.
Example
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
74
By Akshay Bhagade
<option value="audi">Audi</option>
</select>
Example
<option value="fiat" selected>Fiat</option>
Visible Values:
Example
<select name="cars" size="3">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<select name="cars" size="4" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
Example
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
75
By Akshay Bhagade
The rows attribute specifies the visible number of lines in a text area.
The cols attribute specifies the visible width of a text area.
You can also define the size of the text area by using CSS:
Example
<textarea name="message" style="width:200px; height:600px;">
The cat was playing in the garden.
</textarea>
Example
<button type="button" onclick="alert('Hello World!')">Click Me!</button>
Click Me!
Note: Always specify the type attribute for the button element. Different browsers
may use different default types for the button element.
<datalist>
<output>
Note: Browsers do not display unknown elements. New elements that are not
supported in older browsers will not "destroy" your web page.
76
By Akshay Bhagade
Users will see a drop-down list of the pre-defined options as they input data.
The list attribute of the <input> element, must refer to the id attribute of
the <datalist> element.
Example
<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>
Example
Perform a calculation and show the result in an <output>element:
<form action="/action_page.php"
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>
77
By Akshay Bhagade
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Example
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
78
By Akshay Bhagade
Example
<form>
User name:<br>
<input type="text" name="username"><br>
User password:<br>
<input type="password" name="psw">
</form>
The form-handler is typically a server page with a script for processing input data.
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>
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">
<input type="reset">
</form>
79
By Akshay Bhagade
If you change the input values and then click the "Reset" button, the form-data will
be reset to the default values.
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>
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
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>
I have a bike
I have a car
Example
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
80
By Akshay Bhagade
color
date
datetime-local
email
month
number
range
search
tel
time
url
week
New input types that are not supported by older web browsers, will behave
as <input type="text">.
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>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Birthday:
<input type="date" name="bday">
</form>
You can also use the minand maxattributes to add restrictions to dates:
81
By Akshay Bhagade
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>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Birthday (date and time):
<input type="datetime-local" name="bdaytime">
</form>
Example
<form>
E-mail:
<input type="email" name="email">
</form>
82
By Akshay Bhagade
Example
<form>
Select a file: <input type="file" name="myFile">
</form>
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>
The following example displays a numeric input field, where you can enter a value
from 1 to 5:
Example
<form>
Quantity (between 1 and 5):
<input type="number" name="quantity" min="1" max="5">
</form>
Input Restrictions
Here is a list of some common input restrictions:
Attribute Description
83
By Akshay Bhagade
You will learn more about input restrictions in the next chapter.
The following example displays a numeric input field, where you can enter a value
from 0 to 100, in steps of 10. The default value is 30:
Example
<form>
Quantity:
<input type="number" name="points" min="0" max="100" step="10" value="30">
</form>
84
By Akshay Bhagade
Example
<form>
<input type="range" name="points" min="0" max="10">
</form>
Example
<form>
Search Google:
<input type="search" name="googlesearch">
</form>
Example
<form>
Telephone:
<input type="tel" name="phone" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</form>
Depending on browser support, a time picker can show up in the input field.
85
By Akshay Bhagade
Example
<form>
Select a time:
<input type="time" name="usr_time">
</form>
Depending on browser support, the url field can be automatically validated when
submitted.
Some smartphones recognize the url type, and adds ".com" to the keyboard to
match url input.
Example
<form>
Add your homepage:
<input type="url" name="homepage">
</form>
Depending on browser support, a date picker can show up in the input field.
Example
<form>
Select a week:
<input type="week" name="week_year">
</form>
86
By Akshay Bhagade
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John">
</form>
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" readonly>
</form>
A disabled input field is unusable and un-clickable, and its value will not be sent
when submitting the form:
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" disabled>
</form>
Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" size="40">
</form>
87
By Akshay Bhagade
Example
<form action="">
First name:<br>
<input type="text" name="firstname" maxlength="10">
</form>
With a maxlength attribute, the input field will not accept more than the allowed
number of characters.
The maxlength attribute does not provide any feedback. If you want to alert the
user, you must write JavaScript code.
Note: Input restrictions are not foolproof, and JavaScript provides many ways to
add illegal input. To safely restrict input, it must be checked by the receiver (the
server) as well!
HTML5 Attributes
HTML5 added the following attributes for <input>:
autocomplete
autofocus
form
formaction
formenctype
formmethod
formnovalidate
formtarget
height and width
list
min and max
multiple
pattern (regexp)
placeholder
required
step
and the following attributes for <form>:
autocomplete
novalidate
88
By Akshay Bhagade
When autocomplete is on, the browser automatically completes the input values
based on values that the user has entered before.
Tip: It is possible to have autocomplete "on" for the form, and "off" for specific
input fields, or vice versa.
The autocomplete attribute works with <form> and the following <input> types:
text, search, url, tel, email, password, datepickers, range, and color.
Example
An HTML form with autocomplete on (and off for one input field):
Tip: In some browsers you may need to activate the autocomplete function for this to
work.
When present, novalidate specifies that the form data should not be validated when
submitted.
Example
Indicates that the form is not to be validated on submit:
89
By Akshay Bhagade
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
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="/action_page2.php"
90
By Akshay Bhagade
value="Submit as admin">
</form>
Example
Send form-data that is default encoded (the first submit button), and encoded as
"multipart/form-data" (the second submit button):
Example
formmeth
od
The second submit button overrides the HTTP method of the form:
91
By Akshay Bhagade
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>
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>
Always specify the size of images. If the browser does not know the size, the page
will flicker while images load.
92
By Akshay Bhagade
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>
Example
<input> elements with min and max values:
93
By Akshay Bhagade
The multiple attribute works with the following input types: email, and file.
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.
94
By Akshay Bhagade
Example
An input field with a placeholder text:
Example
A required input field:
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-local, month, time and week.
Example
An input field with a specified legal number intervals:
95