Unit II - Web Programming
Unit II - Web Programming
Contents
HTML Tag Reference, Global Attributes, Event Handlers, Document Structure Tags,
Formatting Tags, Text Level formatting, Block Level formatting, List Tags, Hyperlink
tags, Image and Image maps, Table tags, Form Tags, Frame Tags, Executable content
tags. Imagemaps , Tables as a design tool, Forms : Creating Forms. Style Sheets: What
are style sheets?, Why are style sheets valuable? Different approaches to style sheets,
Using Multiple approaches, Linking to style information in a separate file, Setting up
style information.
HTML stands for Hypertext Markup Language, and it is the most widely used language to write
Web Pages.
Hypertext refers to the way in which Web pages (HTML documents) are linked together. Thus the
link available on a webpage are called Hypertext.
As its name suggests, HTML is a Markup Language which means you use HTML to simply "mark
up" a text document with tags that tell a Web browser how to structure it to display.
Originally, HTML was developed with the intent of defining the structure of documents like
headings, paragraphs, lists, and so forth to facilitate the sharing of scientific information between
researchers.
Now, HTML is being widely used to format web pages with the help of different tags available in
HTML language.
HTML Tags
As told earlier, HTML is a markup language and makes use of various tags to format the content.
These tags are enclosed within angle braces <Tag Name>. Except few tags, most of the tags have
their corresponding closing tags. For example <html> has its closing tag </html> and <body> tag
has its closing tag </body> tag etc.
1
<!DOCTYPE...> This tag defines the document type and HTML version.
<html> This tag encloses the complete HTML document and mainly comprises of
document header which is represented by <head>...</head> and
document body which is represented by <body>...</body> tags.
<head> This tag represents the document's header which can keep other HTML
tags like <title>, <link> etc.
<title> The <title> tag is used inside the <head> tag to mention the document
title.
<body> This tag represents the document's body which keeps other HTML tags like
<h1>, <div>, <p> etc.
To learn HTML, you will need to study various tags and understand how they behave while
formatting a textual document. Learning HTML is simple as users have to learn the usage of
different tags in order to format the text or images to make a beautiful webpage.
World Wide Web Consortium (W3C) recommends to use lowercase tags starting from HTML 4.
<body>
Document body related tags
</body>
</html>
We will study all the header and body tags in subsequent chapters, for now let's see what is
document declaration tag.
<!DOCTYPE html>
There are many other declaration types which can be used in HTML document depending on what
version of HTML is being used. We will see more details on this while discussing <!DOCTYPE...> tag
along with other HTML tags.
<bdi> Represents text that must be isolated from its surrounding for
bidirectional text formatting. It allows embedding a span of
text with a different, or unknown, directionality
GLOBAL ATTRIBUTES
Attribute Description
EVENT HANDLERS
HTML 4 added the ability to let events trigger actions in a browser, like starting
a JavaScript when a user clicks on an element.
Below are the global event attributes that can be added to HTML elements to
define event actions.
Events triggered for the window object (applies to the <body> tag):
Form Events
Events triggered by actions inside a HTML form (applies to almost all HTML
elements, but is most used in form elements):
13
Page
onfocus script Fires the moment when the element gets focus
Keyboard Events
Attribute Value Description
14
Page
Mouse Events
Events triggered by a mouse, or similar user actions:
Clipboard Events
Attribute Value Description
16
Page
Media Events
Events triggered by medias like videos, images and audio (applies to all HTML
elements, but is most common in media elements, like <audio>, <embed>,
<img>, <object>, and <video>).
Misc Events
Page
Attribute Value Description
<!DOCTYPE html>
<html>
<head>
<title>This is document title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>Document content goes here.....</p>
</body>
</html>
Either you can use Try it option available at the top right corner of the code box to check the result
of this HTML code, or let's save it in an HTML file test.htm using your favorite text editor. Finally
open it using a web browser like Internet Explorer or Google Chrome, or Firefox etc. It must show
20
Bold Text
Anything that appears within <b>...</b> element, is displayed in bold as shown
below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Bold Text Example</title>
</head>
<body>
<p>The following word uses a <b>bold</b> typeface.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<title>Italic Text Example</title>
</head>
<body>
<p>The following word uses a <i>italicized</i> typeface.</p>
</body>
</html>
Underlined Text
Anything that appears within <u>...</u> element, is displayed with underline as
shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Underlined Text Example</title>
</head>
<body>
<p>The following word uses a <u>underlined</u> typeface.</p>
</body>
22
</html>
Page
Strike Text
Anything that appears within <strike>...</strike> element is displayed with
strikethrough, which is a thin line through the text as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Strike Text Example</title>
</head>
<body>
<p>The following word uses a <strike>strikethrough</strike> typeface.</p>
</body>
</html>
Monospaced Font
The content of a <tt>...</tt> element is written in monospaced font. Most of the
fonts are known as variable-width fonts because different letters are of different
widths (for example, the letter 'm' is wider than the letter 'i'). In a monospaced font,
however, each letter has the same width.
Example
<!DOCTYPE html>
<html>
<head>
<title>Monospaced Font Example</title>
</head>
23
<body>
Page
Superscript Text
The content of a <sup>...</sup> element is written in superscript; the font size
used is the same size as the characters surrounding it but is displayed half a
character's height above the other characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Superscript Text Example</title>
</head>
<body>
<p>The following word uses a <sup>superscript</sup> typeface.</p>
</body>
</html>
superscript
The following word uses a typeface.
Subscript Text
The content of a <sub>...</sub> element is written in subscript; the font size used
is the same as the characters surrounding it, but is displayed half a character's height
beneath the other characters.
Example
24
<!DOCTYPE html>
Page
<html>
<head>
<title>Subscript Text Example</title>
</head>
<body>
<p>The following word uses a <sub>subscript</sub> typeface.</p>
</body>
</html>
Inserted Text
Anything that appears within <ins>...</ins> element is displayed as inserted text.
Example
<!DOCTYPE html>
<html>
<head>
<title>Inserted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del> <ins>wine</ins></p>
</body>
</html>
Deleted Text
Anything that appears within <del>...</del> element, is displayed as deleted text.
Example
25
<!DOCTYPE html>
Page
<html>
<head>
<title>Deleted Text Example</title>
</head>
<body>
<p>I want to drink <del>cola</del> <ins>wine</ins></p>
</body>
</html>
Larger Text
The content of the <big>...</big> element is displayed one font size larger than the
rest of the text surrounding it as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Larger Text Example</title>
</head>
<body>
<p>The following word uses a <big>big</big> typeface.</p>
</body>
</html>
Smaller Text
The content of the <small>...</small> element is displayed one font size smaller
26
Grouping Content
The <div> and <span> elements allow you to group together several elements to
create sections or subsections of a page.
For example, you might want to put all of the footnotes on a page within a <div>
element to indicate that all of the elements within that <div> element relate to the
footnotes. You might then attach a style to this <div> element so that they appear
using a special set of style rules.
Example
<!DOCTYPE html>
<html>
<head>
<title>Div Tag Example</title>
</head>
<body>
<div id="menu" align="middle" >
<a href="/index.htm">HOME</a> |
27
<a href="/about/contact_us.htm">CONTACT</a> |
Page
<a href="/about/index.htm">ABOUT</a>
</div>
The <span> element, on the other hand, can be used to group inline elements only.
So, if you have a part of a sentence or paragraph which you want to group together,
you could use the <span> element as follows
Example
<!DOCTYPE html>
<html>
<head>
<title>Span Tag Example</title>
</head>
<body>
<p>This is the example of <span style="color:green">span tag</span> and the <span
style="color:red">div tag</span> alongwith CSS</p>
</body>
</html>
This is the example of span tag and the div tag alongwith CSS
LIST TAGS
HTML offers web authors three ways for specifying lists of information. All lists must
28
<ul> - An unordered list. This will list items using plain bullets.
<ol> - An ordered list. This will use different schemes of numbers to list your items.
<dl> - A definition list. This arranges your items in the same way as they are
arranged in a dictionary.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Unordered List</title>
</head>
<body>
<ul>
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ul>
</body>
</html>
Beetroot
Ginger
Potato
Radish
You can use type attribute for <ul> tag to specify the type of bullet you like. By default it
Page
Example
Following is an example where we used <ul type="square">
<!DOCTYPE html>
<html>
<head>
<title>HTML Unordered List</title>
</head>
<body>
<ul type="square">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ul>
</body>
</html>
Beetroot
Ginger
Potato
Radish
Example
Following is an example where we used <ul type="disc"> :
<!DOCTYPE html>
30
<html>
Page
<head>
<title>HTML Unordered List</title>
</head>
<body>
<ul type="disc">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ul>
</body>
</html>
Beetroot
Ginger
Potato
Radish
Example
Following is an example where we used <ul type="circle"> :
<!DOCTYPE html>
<html>
<head>
<title>HTML Unordered List</title>
</head>
<body>
<ul type="circle">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
31
<li>Radish</li>
Page
</ul>
</body>
</html>
o Beetroot
o Ginger
o Potato
o Radish
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Ordered List</title>
</head>
<body>
<ol>
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ol>
</body>
</html>
1. Beetroot
Page
2. Ginger
3. Potato
4. Radish
Example
Following is an example where we used <ol type="1">
<!DOCTYPE html>
<html>
<head>
<title>HTML Ordered List</title>
</head>
<body>
<ol type="1">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ol>
</body>
</html>
1. Beetroot
Page
2. Ginger
3. Potato
4. Radish
Example
Following is an example where we used <ol type="I">
<!DOCTYPE html>
<html>
<head>
<title>HTML Ordered List</title>
</head>
<body>
<ol type="I">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ol>
</body>
</html>
I. Beetroot
II. Ginger
III. Potato
IV. Radish
Example
Following is an example where we used <ol type="i">
<!DOCTYPE html>
<html>
34
<head>
Page
i. Beetroot
ii. Ginger
iii. Potato
iv. Radish
Example
Following is an example where we used <ol type="A">
<!DOCTYPE html>
<html>
<head>
<title>HTML Ordered List</title>
</head>
<body>
<ol type="A">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
35
</ol>
Page
</body>
</html>
This will produce following result:
A. Beetroot
B. Ginger
C. Potato
D. Radish
Example
Following is an example where we used <ol type="a">
<!DOCTYPE html>
<html>
<head>
<title>HTML Ordered List</title>
</head>
<body>
<ol type="a">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ol>
</body>
</html>
a. Beetroot
b. Ginger
c. Potato
36
d. Radish
Page
The start Attribute
You can use start attribute for <ol> tag to specify the starting point of numbering you
need. Following are the possible options:
Example
Following is an example where we used <ol type="i" start="4" >
<!DOCTYPE html>
<html>
<head>
<title>HTML Ordered List</title>
</head>
<body>
<ol type="i" start="4">
<li>Beetroot</li>
<li>Ginger</li>
<li>Potato</li>
<li>Radish</li>
</ol>
</body>
</html>
37
iv. Beetroot
v. Ginger
vi. Potato
vii. Radish
<dt> - A term
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Definition List</title>
</head>
<body>
<dl>
<dt><b>HTML</b></dt>
<dd>This stands for Hyper Text Markup Language</dd>
<dt><b>HTTP</b></dt>
<dd>This stands for Hyper Text Transfer Protocol</dd>
</dl>
</body>
</html>
HTML
This stands for Hyper Text Markup Language
HTTP
This stands for Hyper Text Transfer Protocol
HYPERLINK TAGS
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.
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 on subfolder addresses, you might generate two
requests to the server. Many servers will automatically add a forward slash to
the address, and then create a new request.
39
Local Links
Page
The example above used an absolute URL (A full web address).
A local link (link to the same web site) is specified with a relative URL (without
http://www....).
Example
<a href="html_images.asp">HTML Images</a>
Example
< style>
a:link {color:green; 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>
_top - Opens the linked document in the full body of the window
framename - Opens the linked document in a named frame
Page
This example will open the linked document in a new browser window/tab:
Example
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!
</a>
Tip: If your webpage is locked in a frame, you can use target="_top" to break
out of the frame:
Example
<a href="http://www.w3schools.com/html/" target="_top">HTML5 tutorial!
</a>
Example
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial"
style="width:42px;height:42px;border:0;">
</a>
Note: border:0; is added to prevent IE9 (and earlier) from displaying a border
around the image (when the image is a link).
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:
41
Page
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>
Insert Image
You can insert any image in your web page by using <img> tag. Following is the simple
syntax to use this tag.
The <img> tag is an empty tag, which means that it can contain only list of attributes and
it has no closing tag.
Example
To try following example, let's keep our HTML file test.htm and image file test.png in the
same directory:
<!DOCTYPE html>
<html>
<head>
<title>Using Image in Webpage</title>
</head>
<body>
<p>Simple Image Insert</p>
42
</body>
</html>
You can use PNG, JPEG or GIF image file based on your comfort but make sure you specify
correct image file name in src attribute. Image name is always case sensitive.
The alt attribute is a mandatory attribute which specifies an alternate text for an image, if
the image cannot be displayed.
Example
Assuming our image location is "/html/image/test.png", try the following example:
<!DOCTYPE html>
<html>
<head>
<title>Using Image in Webpage</title>
</head>
<body>
<p>Simple Image Insert</p>
<img src="/html/images/test.png" alt="Test Image" />
</body>
</html>
Example
Page
<!DOCTYPE html>
<html>
<head>
<title>Set Image Width and Height</title>
</head>
<body>
<p>Setting image width and height</p>
<img src="/html/images/test.png" alt="Test Image" width="150" height="100"/>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<title>Set Image Border</title>
</head>
<body>
<p>Setting image Border</p>
<img src="/html/images/test.png" alt="Test Image" border="3"/>
</body>
</html>
Example
44
<!DOCTYPE html>
Page
<html>
<head>
<title>Set Image Alignment</title>
</head>
<body>
<p>Setting image Alignment</p>
<img src="/html/images/test.png" alt="Test Image" border="3" align="right"/>
</body>
</html>
Description
The HTML <map> tag is used for defining an image map along with <img> tag.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML map Tag</title>
</head>
<body>
<img src="/images/html.gif" alt="HTML Map" border="0" usemap="#html"/>
This will produce following result, find the image map on bottom right:
45
Page
Global Attributes
This tag supports all the global attributes described in - HTML Attribute Reference
Specific Attributes
The HTML <map> tag also supports following additional attributes:
Event Attributes
This tag supports all the event attributes described in - HTML Events Reference
Browser Support
Chrome Firefox IE Opera Safari Android
TABLE TAGS
The HTML tables allow web authors to arrange data like text, images, links, other
tables, etc. into rows and columns of cells.
The HTML tables are created using the <table> tag in which the <tr> tag is used to
create table rows and <td> tag is used to create data cells.
Example
<!DOCTYPE html>
46
<html>
Page
<head>
<title>HTML Tables</title>
</head>
<body>
<table border="1">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
Here border is an attribute of <table> tag and it is used to put a border across all the
cells. If you do not need a border then you can use border="0".
Table Heading
Table heading can be defined using <th> tag. This tag will be put to replace <td>
tag, which is used to represent actual data cell. Normally you will put your top row as
table heading as shown below, otherwise you can use <th> element in any row.
Example
<!DOCTYPE html>
<html>
47
<head>
Page
Salar
Name
y
Ramesh
5000
Raman
Shabbir
7000
Hussein
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Cellpadding</title>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="5">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
Salar
Name
y
Ramesh
5000
Raman
Shabbir
7000
Hussein
49
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Colspan/Rowspan</title>
</head>
<body>
<table border="1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr><td rowspan="2">Row 1 Cell 1</td><td>Row 1 Cell 2</td><td>Row 1 Cell 3</td></tr>
<tr><td>Row 2 Cell 2</td><td>Row 2 Cell 3</td></tr>
<tr><td colspan="3">Row 3 Cell 1</td></tr>
</table>
</body>
</html>
Tables Backgrounds
Page
You can set table background using one of the following two ways:
bgcolor attribute - You can set background color for whole table or just for one cell.
background attribute - You can set background image for whole table or just for one
cell.
You can also set border color also using bordercolor attribute.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Background</title>
</head>
<body>
<table border="1" bordercolor="green" bgcolor="yellow">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr><td rowspan="2">Row 1 Cell 1</td><td>Row 1 Cell 2</td><td>Row 1 Cell 3</td></tr>
<tr><td>Row 2 Cell 2</td><td>Row 2 Cell 3</td></tr>
<tr><td colspan="3">Row 3 Cell 1</td></tr>
</table>
</body>
</html>
2 3
Page
Row 3 Cell 1
Here is an example of using background attribute. Here we will use an image
available in /images directory.
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Background</title>
</head>
<body>
<table border="1" bordercolor="green" background="/images/test.png">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr><td rowspan="2">Row 1 Cell 1</td><td>Row 1 Cell 2</td><td>Row 1 Cell 3</td></tr>
<tr><td>Row 2 Cell 2</td><td>Row 2 Cell 3</td></tr>
<tr><td colspan="3">Row 3 Cell 1</td></tr>
</table>
</body>
</html>
This will produce following result. Here background image did not apply to table's
header.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Width/Height</title>
</head>
<body>
<table border="1" width="400" height="150">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
Table Caption
The caption tag will serve as a title or explanation for the table and it shows up at the
53
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Caption</title>
</head>
<body>
<table border="1" width="100%">
<caption>This is the caption</caption>
<tr>
<td>row 1, column 1</td><td>row 1, columnn 2</td>
</tr>
<tr>
<td>row 2, column 1</td><td>row 2, columnn 2</td>
</tr>
</table>
</body>
</html>
The three elements for separating the head, body, and foot of a table are:
54
A table may contain several <tbody> elements to indicate different pages or groups of
data. But it is notable that <thead> and <tfoot> tags should appear before <tbody>
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
</head>
<body>
<table border="1" width="100%">
<thead>
<tr>
<td colspan="4">This is the head of the table</td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4">This is the foot of the table</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</tbody>
</table>
55
</body>
Page
</html>
This will produce following result:
Nested Tables
You can use one table inside another table. Not only tables you can use almost all the
tags inside table data tag <td>.
Example
Following is the example of using another table and other tags inside a table cell.
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
</head>
<body>
<table border="1" width="100%">
<tr>
<td>
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
56
<td>Shabbir Hussein</td>
Page
<td>7000</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Name Salary
Ramesh Raman 5000
Shabbir Hussein 7000
FORM TAGS
HTML Forms are required when you want to collect some data from the site visitor. For
example during user registration you would like to collect information such as name,
email address, credit card, etc.
A form will take input from the site visitor and then will post it to a back-end
application such as CGI, ASP Script or PHP script etc. The back-end application will
perform required processing on the passed data based on defined business logic inside
the application.
There are various form elements available like text fields, textarea fields, drop-down
menus, radio buttons, checkboxes, etc.
The HTML <form> tag is used to create an HTML form and it has following syntax:
Form Attributes
57
Apart from common attributes, following is a list of the most frequently used form
Page
attributes:
Attribu
Description
te
Method to be used to upload data. The most frequently used are GET and POST
Method
methods.
Specify the target window or frame where the result of the script will be displayed. It
Target
takes values like _blank, _self, _parent etc.
You can use the enctype attribute to specify how the browser encodes the data
before it sends it to the server. Possible values are:
mutlipart/form-data - This is used when you want to upload binary data in the
form of files like image, word file etc.
Note: You can refer to Perl & CGI for a detail on how form data upload works.
Checkboxes Controls
Hidden Controls
Page
Clickable Buttons
Single-line text input controls - This control is used for items that require only one
line of user input, such as search boxes or names. They are created using HTML
<input> tag.
Password input controls - This is also a single-line text input but it masks the
character as soon as a user enters it. They are also created using HTMl <input> tag.
Multi-line text input controls - This is used when the user is required to give details
that may be longer than a single sentence. Multi-line input controls are created using
HTML <textarea> tag.
Example
Here is a basic example of a single-line text input used to take first name and last
name:
<!DOCTYPE html>
<html>
<head>
<title>Text Input Control</title>
</head>
<body>
<form >
First name: <input type="text" name="first_name" />
59
<br>
Page
First name:
Last name:
Attributes
Following is the list of attributes for <input> tag for creating text field.
Attribut
Description
e
type Indicates the type of input control and for text input control it will be set to text.
Used to give a name to the control which is sent to the server to be recognized and
name
get the value.
value This can be used to provide an initial value inside the control.
size Allows to specify the width of the text-input control in terms of characters.
maxleng Allows to specify the maximum number of characters a user can enter into the text
th box.
Example
Here is a basic example of a single-line password input used to take user password:
60
<!DOCTYPE html>
Page
<html>
<head>
<title>Password Input Control</title>
</head>
<body>
<form >
User ID : <input type="text" name="user_id" />
<br>
Password: <input type="password" name="password" />
</form>
</body>
</html>
User ID :
Password:
Attributes
Following is the list of attributes for <input> tag for creating password field.
Attribut
Description
e
Indicates the type of input control and for password input control it will be set to
type
password.
Used to give a name to the control which is sent to the server to be recognized and
name
get the value.
value This can be used to provide an initial value inside the control.
size Allows to specify the width of the text-input control in terms of characters.
maxleng Allows to specify the maximum number of characters a user can enter into the text
th box.
61
Page
Example
Here is a basic example of a multi-line text input used to take item description:
<!DOCTYPE html>
<html>
<head>
<title>Multiple-Line Input Control</title>
</head>
<body>
<form>
Description : <br />
<textarea rows="5" cols="50" name="description">
Enter description here...
</textarea>
</form>
</body>
</html>
Description :
Enter description here...
Attributes
Following is the list of attributes for <textarea> tag.
Attribut
Description
e
62
Used to give a name to the control which is sent to the server to be recognized and
name
Page
Checkbox Control
Checkboxes are used when more than one option is required to be selected. They are
also created using HTML <input> tag but type attribute is set to checkbox.
Example
Here is an example HTML code for a form with two checkboxes:
<!DOCTYPE html>
<html>
<head>
<title>Checkbox Control</title>
</head>
<body>
<form>
<input type="checkbox" name="maths" value="on"> Maths
<input type="checkbox" name="physics" value="on"> Physics
</form>
</body>
</html>
Maths Physics
Attributes
Following is the list of attributes for <checkbox> tag.
Attribut
Description
e
Indicates the type of input control and for checkbox input control it will be set to
63
type
checkbox.
Page
Used to give a name to the control which is sent to the server to be recognized and
name
get the value.
Example
Here is example HTML code for a form with two radio buttons:
<!DOCTYPE html>
<html>
<head>
<title>Radio Box Control</title>
</head>
<body>
<form>
<input type="radio" name="subject" value="maths"> Maths
<input type="radio" name="subject" value="physics"> Physics
</form>
</body>
</html>
Maths Physics
Attributes
Following is the list of attributes for radio button.
64
Page
Attribut
Description
e
Indicates the type of input control and for checkbox input control it will be set to
type
radio.
Used to give a name to the control which is sent to the server to be recognized and
name
get the value.
value The value that will be used if the radio box is selected.
Example
Here is example HTML code for a form with one drop down box
<!DOCTYPE html>
<html>
<head>
<title>Select Box Control</title>
</head>
<body>
<form>
<select name="dropdown">
<option value="Maths" selected>Maths</option>
<option value="Physics">Physics</option>
</select>
</form>
65
</body>
Page
</html>
This will produce following result:
Maths
Attributes
Following is the list of important attributes of <select> tag:
Attribut
Description
e
Used to give a name to the control which is sent to the server to be recognized and
name
get the value.
multiple If set to "multiple" then allows a user to select multiple items from the menu.
Attribute Description
value The value that will be used if an option in the select box box is selected.
Specifies that this option should be the initially selected value when the page
selected
loads.
Example
Here is example HTML code for a form with one file upload box:
66
<!DOCTYPE html>
Page
<html>
<head>
<title>File Upload Box</title>
</head>
<body>
<form>
<input type="file" name="fileupload" accept="image/*" />
</form>
</body>
</html>
Attributes
Following is the list of important attributes of file upload box:
Attribut
Description
e
Used to give a name to the control which is sent to the server to be recognized and
name
get the value.
Button Controls
There are various ways in HTML to create clickable buttons. You can also create a
clickable button using <input> tag by setting its type attribute to button. The type
attribute can take the following values:
Type Description
submi
This creates a button that automatically submits a form.
t
reset This creates a button that automatically resets form controls to their initial values.
butto This creates a button that is used to trigger a client-side script when the user clicks that
67
n button.
Page
image This creates a clickable button but we can use an image as background of the button.
Example
Here is example HTML code for a form with three types of buttons:
<!DOCTYPE html>
<html>
<head>
<title>File Upload Box</title>
</head>
<body>
<form>
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
<input type="button" name="ok" value="OK" />
<input type="image" name="imagebutton" src="/html/images/logo.png" />
</form>
</body>
</html>
Submit Reset
Example
Here is example HTML code to show the usage of hidden control:
<!DOCTYPE html>
<html>
<head>
<title>File Upload Box</title>
</head>
<body>
<form>
<p>This is page 10</p>
<input type="hidden" name="pagename" value="10" />
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
</form>
</body>
</html>
This is page 10
10 Submit Reset
FRAME TAGS
HTML frames are used to divide your browser window into multiple sections where
each section can load a separate HTML document. A collection of frames in the
browser window is known as a frameset. The window is divided into frames in a
similar way the tables are organized: into rows and columns.
Disadvantages of Frames
There are few drawbacks with using frames, so it's never recommended to use frames
in your webpages:
69
Some smaller devices cannot cope with frames often because their screen is not big
enough to be divided up.
Page
Sometimes your page will be displayed differently on different computers due to
different screen resolution.
The browser's back button might not work as the user hopes.
There are still few browsers that do not support frame technology.
Creating Frames
To use frames on a page we use <frameset> tag instead of <body> tag. The
<frameset> tag defines how to divide the window into frames. The rows attribute of
<frameset> tag defines horizontal frames and cols attribute defines vertical frames.
Each frame is indicated by <frame> tag and it defines which HTML document shall
open into the frame.
Example
Following is the example to create three horizontal frames:
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<frameset rows="10%,80%,10%">
<frame name="top" src="/html/top_frame.htm" />
<frame name="main" src="/html/main_frame.htm" />
<frame name="bottom" src="/html/bottom_frame.htm" />
<noframes>
<body>
Your browser does not support frames.
</body>
</noframes>
</frameset>
</html>
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<frameset cols="25%,50%,25%">
<frame name="left" src="/html/top_frame.htm" />
<frame name="center" src="/html/main_frame.htm" />
<frame name="right" src="/html/bottom_frame.htm" />
<noframes>
<body>
Your browser does not support frames.
</body>
</noframes>
</frameset>
</html>
Attribute Description
specifies how many columns are contained in the frameset and the size of each
column. You can specify the width of each column in one of four ways:
Absolute values in pixels. For example to create three vertical frames, use
cols="100, 500,100".
As relative widths of the browser window. For example to create three vertical
frames, use cols="3*,2*,1*". This is an alternative to percentages. You can use
relative widths of the browser window. Here the window is divided into sixths:
the first column takes up half of the window, the second takes one third, and the
third takes one sixth.
This attribute works just like the cols attribute and takes the same values, but it is
used to specify the rows in the frameset. For example to create two horizontal
rows
frames, use rows="10%, 90%". You can specify the height of each row in the same
way as explained above for columns.
This attribute specifies the width of the border of each frame in pixels. For
border
example border="5". A value of zero means no border.
72
er between frames. This attrubute takes value either 1 (yes) or 0 (no). For example
frameborder="0" specifies no border.
This attribute specifies the amount of space between frames in a frameset. This
framespaci
can take any integer value. For example framespacing="10" means there should
ng
be 10 pixels spacing between each frames.
Attribute Description
This attribute is used to give the file name that should be loaded in the frame. Its
src value can be any URL. For example, src="/html/top_frame.htm" will load an HTML
file available in html directory.
This attribute allows you to give a name to a frame. It is used to indicate which
frame a document should be loaded into. This is especially important when you
name
want to create links in one frame that load pages into an another frame, in which
case the second frame needs a name to identify itself as the target of the link.
This attribute specifies whether or not the borders of that frame are shown; it
framebord
overrides the value given in the frameborder attribute on the <frameset> tag if
er
one is given, and this can take values either 1 (yes) or 0 (no).
This attribute allows you to specify the width of the space between the left and
marginwidt
right of the frame's borders and the frame's content. The value is given in pixels.
h
For example marginwidth="10".
This attribute allows you to specify the height of the space between the top and
marginheig
bottom of the frame's borders and its contents. The value is given in pixels. For
ht
example marginheight="10".
By default you can resize any frame by clicking and dragging on the borders of a
noresize frame. The noresize attribute prevents a user from being able to resize the frame.
For example noresize="noresize".
This attribute controls the appearance of the scrollbars that appear on the frame.
73
scrolling This takes values either "yes", "no" or "auto". For example scrolling="no" means it
should not have scroll bars.
Page
This attribute allows you to provide a link to another page containing a long
longdesc description of the contents of the frame. For example
longdesc="framedescription.htm"
So you must place a <body> element inside the <noframes> element because the
<frameset> element is supposed to replace the <body> element, but if a browser
does not understand <frameset> element then it should understand what is inside the
<body> element which is contained in a <noframes> element.
You can put some nice message for your user having old browsers. For example
Sorry!! your browser does not support frames. as shown in the above example.
Let's see following example where a test.htm file has following code:
<!DOCTYPE html>
<html>
<head>
<title>HTML Target Frames</title>
</head>
<frameset cols="200, *">
<frame src="/html/menu.htm" name="menu_page" />
<frame src="/html/main.htm" name="main_page" />
<noframes>
<body>
Your browser does not support frames.
74
</body>
Page
</noframes>
</frameset>
</html>
Here we have created two columns to fill with two frames. The first frame is 200 pixels
wide and will contain the navigation menubar implemented by menu.htm file. The
second column fills in remaining space and will contain the main part of the page and
it is implemented by main.htm file. For all the three links available in menubar, we
have mentioned target frame as main_page, so whenever you click any of the links
in menubar, available link will open in main_page.
<!DOCTYPE html>
<html>
<body bgcolor="#4a7d49">
<a href="https://www.google.com" target="main_page">Google</a>
<br /><br />
<a href="https://www.microsoft.com" target="main_page">Microsoft</a>
<br /><br />
<a href="https://news.bbc.co.uk" target="main_page">BBC News</a>
</body>
</html>
<!DOCTYPE html>
<html>
<body bgcolor="#b5dcb3">
<h3>This is main page and content from any link will be displayed here.</h3>
<p>So now click any link and see the result.</p>
</body>
</html>
Option Description
Loads the page into the parent window, which in the case of a single frameset is
_parent
the main browser window.
_top Loads the page into the browser window, replacing any current frames.
targetfra
Loads the page into a named targetframe.
me
Executable Content
One of the most exciting recent developments in web technologies is the ability to deliver
applications directly to the user's browser. These typically small programs, which are known as
applets, perform simple tasks on the client computer, from responding to user mouse- or
keyboard-actions to spicing up your web page displays with multimedia-enabling software.
You may embed applets into your documents using a special programming language known as
JavaScript. Or you can load and execute Java-based, platform-independent applets over the
Internet. During execution, these programs may generate dynamic content, interact with the
user, validate form data, or even create windows and run entire applications independent of your
pages. The possibilities are endless, and they go far beyond the document model that was
76
In this chapter, we show you, with simple examples, how to embed and include executable
content -- scripts and applets -- in your documents. We won't, however, even begin to pretend to
teach you how to write and debug your own applets. This is a book about HTML and XTHML,
after all. Rather, get an expert opinion: turn to any of the many excellent texts from O'Reilly,
including JavaScript: The Definitive Guide, by David Flanagan, Java in a Nutshell, by David
Flanagan, and Exploring Java, Pat Niemeyer and Josh Peck.
Applets also represent a way of extending a browser's features without forcing users to purchase
or otherwise acquire a new browser, as is the case when developers implement new tag and
attribute extensions to HTML. Nor do users have to acquire and install a special application, as
is required for helper or plug-in applications. This means that once users have a browser that
supports applets (Netscape and Internet Explorer do), you can deliver applets immediately,
including display and multimedia innovations.
Java-based applets -- web page-referenced programs retrieved from a network server and
executed on the user's client computer -- actually are a subset of what the HTML 4 and XHTML
standards call inclusions. As with images, the browser first loads the HTML document, then
examines it for inclusions -- additional, separate, and discrete content that is to be handled by
the client browser. A GIF image is one type of inclusion. A .wav sound file is another; an
MPEG movie is another; a Java-based clock program is another.
The HTML 4 and XHTML standards generally calls the inclusion contents objects. In fact, in
your document you may identify and load nearly any object file over the network through a
universal <object> tag, which we discuss in detail later in this chapter. Section 12.2.1, "The
<object> Tag"
Once downloaded, the standards dictate that the browser somehow render the object, by internal
or external mechanisms. The popular graphical browsers, for instance, have integrated software
for displaying GIF and JPEG images. Otherwise, plug-ins and other helper applications may
provide the necessary rendering mechanism.
With applet-based objects, the browser sets aside a portion of the document display space. You
may control the size and position of this display area; the applet controls what is presented
77
inside.
Page
The applet object is software, an executable program. Accordingly, besides providing a display
space, the browser, in tandem with the client computer environment and resources, provides the
applet runtime environment -- typically Java.
During execution, the applet has access to a restricted environment within the user's computer.
For instance, applets have access to the mouse and keyboard and may receive input from the
user. They can initiate network connections and retrieve data from other servers on the Internet.
In sum, applets are full-fledged programs, complete with a variety of input and output
mechanisms, along with a full suite of network services.
Several applets may be placed in a single document; they all execute in parallel and may
communicate with each other. While the browser may limit their access to its computer system,
applets have complete control of their virtual environment within the browser.
There are several advantages of applets, not the least of which is providing more compelling
user interfaces within a web page. For instance, an applet might create a unique set of menus,
choices, text fields, and similar user-input tools different from those available through the
browser. When the user clicks a button within the applet's interaction/display region, the applet
might respond by displaying results within the region, signaling another applet, or even by
loading a completely new page into the browser.
We don't mean to imply that the only use of applets is to enhance the user interface. An applet is
a full-fledged program that can perform any number of computational and user-interactive tasks
on the client computer. An applet might implement a real-time video display, perform circuit
simulation, engage the user in a game, provide a chat interface, and so on.
An applet is nothing more than another tool you may use to produce compelling and useful web
pages. Keep in mind that an applet uses computational resources on the client to run and
therefore places a load on the user's computer. It can degrade system performance.
Similarly, if an applet uses a lot of network bandwidth to accomplish its task (a real-time video
feed, for example), it may make other network communication unbearably slow. While such
applications are fun, they do little more than annoy your target audience.
To use an applet correctly, balance the load between the browser and the server. For each page,
decide which tasks are best left to the server (forms processing, index searches, and the like)
and which tasks are better suited for local processing (user interface enhancements, real-time
data presentation, small animations, input validation, and so on). Divide the processing
accordingly. Remember that many users have slower network connections and computers than
78
you do and design your applets to satisfy the majority of your audience.
Page
Used the right way, applets seamlessly enhance your pages and provide a satisfying experience
for your audience. Used improperly, applets are just another annoying bandwidth waster,
alienating your users and hurting your pages.
Writing applets
Creating applets is a programming task, not usually a job for the HTML or XHTML author, and
certainly well beyond the scope of this book. For details, we recommend you consult any of the
many applet programming texts that have recently appeared on bookshelves everywhere,
including those from O'Reilly & Associates.
Today, one language dominates the applet programming world: Java. Developed by Sun
Microsystems of Mountain View, California, Java supports an object-oriented programming
style wherein classes of applets can be used and reused to build complex applications.
By invention, applets built from the same language should run with any browser that supports
them. In reality, certain Microsoft implementation decisions had caused some valid Java applets
to fail when running on earlier versions of Internet Explorer. Microsoft has fixed these problems
with Internet Explorer Version 5 and Java will remain a universal programming language for
the Web. In any case, the conscientious Java programmer should keep abreast of the latest
technology and create applets that are certifiably 100% pure Java. Microsoft, in particular, is
trying to get programmers to use proprietary extensions to Java that will work on only
Microsoft platforms and is refusing to support key parts of the standard. We recommend
avoiding any vendor extensions to Java that deviate from the standard Java 1.1 version.
You can shield yourself from platform dependencies by using the Java Plugin from Sun; see
http://java.sun.com/products/plugin. There are versions for both Internet Explorer and Netscape
Navigator. Currently, the plugin is the only way to get support for the latest version of Java
(Java 2 SDK 1.3).[66]
[66] The plugin has achieved some acceptance for running Java 2 applets in intranet (i.e.,
corporate network) environments, but we have yet to see an applet on the public Internet that
required Java 2.
We should take this opportunity to also mention ActiveX, an alternative applet programming
technology available from Microsoft. ActiveX is proprietary, closely coupled with various
versions of Microsoft Windows, and works only when used with Internet Explorer. ActiveX
applets will run on versions of Internet Explorer targeted to various versions of Windows, but a
single ActiveX applet will not run on these different versions without recompilation. This is in
contrast with Java applets, where a single Java applet can be written and compiled once and
immediately run on a broad range of browsers and operating systems.
ActiveX also presents an unacceptably high security risk to any user whose browser supports
79
Cascading Style Sheets (CSS) describe how documents are presented on screens, in
print, or perhaps how they are pronounced. W3C has actively promoted the use of
style sheets on the Web since the Consortium was founded in 1994.
Cascading Style Sheets (CSS) provide easy and effective alternatives to specify
various attributes for the HTML tags. Using CSS, you can specify a number of style
properties for a given HTML element. Each property has a name and a value,
separated by a colon (:). Each property declaration is separated by a semi-colon (;).
Example
First let's consider an example of HTML document which makes use of <font> tag and
associated attributes to specify text color and font size:
<!DOCTYPE html>
<html>
<head>
<title>HTML CSS</title>
</head>
<body>
<p><font color="green" size="5">Hello, World!</font></p>
</body>
</html>
We can re-write above example with the help of Style Sheet as follows:
<!DOCTYPE html>
<html>
<head>
<title>HTML CSS</title>
</head>
<body>
80
</body>
</html>
Hello, World!
You can use CSS in three ways in your HTML document:
External Style Sheet - Define style sheet rules in a separate .css file and then
include that file in your HTML document using HTML <link> tag.
Internal Style Sheet - Define style sheet rules in header section of the HTML
document using <style> tag.
Inline Style Sheet - Define style sheet rules directly along-with the HTML elements
using style attribute.
Let's see all the three cases one by one with the help of suitable examples.
Example
Consider we define a style sheet file style.css which has following rules:
.red{
color: red;
}
.thick{
font-size:20px;
}
.green{
color:green;
81
}
Page
Here we defined three CSS rules which will be applicable to three different classes
defined for the HTML tags. I suggest you should not bother about how these rules are
being defined because you will learn them while studying CSS. Now let's make use of
the above external CSS file in our following HTML document:
<!DOCTYPE html>
<html>
<head>
<title>HTML External CSS</title>
<link rel="stylesheet" type="text/css" href="/html/style.css">
</head>
<body>
<p class="red">This is red</p>
This is red
This is thick
This is green
those rules in header section of the HTML document using <style> tag.
Page
Rules defined in internal style sheet overrides the rules defined in an external CSS file.
Example
Let's re-write above example once again, but here we will write style sheet rules in the
same HTML document using <style> tag:
<!DOCTYPE html>
<html>
<head>
<title>HTML Internal CSS</title>
<style type="text/css">
.red{
color: red;
}
.thick{
font-size:20px;
}
.green{
color:green;
}
</style>
</head>
<body>
<p class="red">This is red</p>
This is red
This is thick
This is green
Rules defined inline with the element overrides the rules defined in an external CSS
file as well as the rules defined in <style> element.
Example
Let's re-write above example once again, but here we will write style sheet rules along
with the HTML elements using style attribute of those elements.
<!DOCTYPE html>
<html>
<head>
<title>HTML Inline CSS</title>
</head>
<body>
<p style="color:red;">This is red</p>
This is red
This is thick
This is green
85
Page