HTML & Javascript: Naresh
HTML & Javascript: Naresh
Web Page
Web is a collection of Files known as web pages. These web pages can contain hyperlinks to link other
web pages. A hyperlink can be any text or image which when clicked would display another web page.
There may be one or more pages in the home page, which is the initial web page present in a web site.
Introduction to HTML
HTML stands for Hyper Text Markup Language, which is an application of Standard Generalized
Markup Language (SGML). It is a simple language used to define and describe the layout of a web
page. HTML also supports Multimedia and document links.
What is H T M L?
Hyper is the opposite of linear. It used to be that computer programs had to move in a
linear fashion. This before this, this before this, and so on. HTML does not hold to that
pattern and allows the person viewing the World Wide Web page to go anywhere, any
time they want.
Text is what you will use. Real, honest to goodness English letters.
Mark up is what you will do. You will write in plain English and then mark up what you
wrote. More to come on that in the next Primer.
Language because they needed something that started with "L" to finish HTML and
Hypertext Markup Louie didn't flow correctly. Because it's a language, really -- but the
language is plain English.
You will write the HTML document on the word processor, or Notepad, WordPad, or Simple Text. When
you are finished creating the HTML document, you'll then open the document in a browser, like
Netscape Navigator. The browser will interpret the HTML commands for you and display the Web page.
HTML documents must be text only. When you save an HTML document, you must save only the text,
nothing else.
1. Choose a name. Anything. If you have a PC not running Windows 95, you are limited to
eight letters, however.
2. Add a suffix. For all HTML documents, you will add either ".htm" or ".html".
Points to remember
The markup tags tell the Web browser how to display the
page
HTML Tags
• HTML tags are surrounded by the two characters < and >
• The first tag in a pair is the start tag, the second tag is the end tag
• The text between the start and end tags is the element content
• HTML tags are not case sensitive, <b> means the same as <B>
Example: -
<HTML>
Here is all the rest of the document, including any elements.
</HTML>
Example :-
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
Hello friends
Welcome to HTML.
This is my first html document.
</body>
</html>
Tag Attributes
Tags can have attributes. Attributes can provide additional information about the HTML elements
on your page.
This tag defines the body element of your HTML page: <body>. With an added bgcolor attribute,
you can tell the browser that the background color of your page should be red, like this:
<body bgcolor="red">.
Attributes always come in name/value pairs like this: name="value".
Attributes are always added to the start tag of an HTML element.
Attribute values should always be enclosed in quotes. Double style quotes are the most common,
but single style quotes are also allowed.
In some rare situations, like when the attribute value itself contains quotes, it is necessary to use
single quotes:
name='John "ShotGun" Nelson'
Attributes of <BODY>
BGCOLOR
The BGCOLOR attribute, allows setting of the background colour for the document.
Example :-
<BODY BGCOLOR="#rrggbb">
Document here
</BODY>
BACKGROUND
The BACKGROUND attribute can be used to point to an image file that will be tiled across the
browser window, to provide a background for the document. Specifying :
Example :-
<BODY BACKGROUND="URL or path/filename.gif">
Document here
</BODY>
TEXT
This attribute is used to control the colour of all the normal text in the document.
Example :-
<BODY TEXT="#rrggbb">
Document here
</BODY>
</BODY>
Headings
HTML defines six levels of heading. A Heading element implies all the font changes, paragraph
breaks before and after, and white space necessary to render the heading.
Headings are defined with the <h1> to <h6> tags. <h1> defines the largest heading. <h6> defines
the smallest heading.
<h1>This is heading1</h1>
<h2>This is heading2</h2>
<h3>This is heading3</h3>
<h4>This is heading4</h4>
<h5>This is heading5</h5>
<h6>This is heading6</h6>
ALIGN attribute
The ALIGN = left | center | right attribute has been added to the <H1> through to <H6> elements.
Example :-
<H1 ALIGN=center>Hello, this is a heading</H1>
HTML automatically adds an extra blank line before and after a heading.
Paragraphs
The Paragraph element indicates a paragraph. Typically, paragraphs are surrounded by a vertical
space of one line or half a line. With some browsers, the first line in a paragraph is indented.
Paragraphs are defined with the <p> tag.
<p>This is a paragraph</p>
<p>This is another paragraph</p>
ALIGN attribute
Basically, ALIGN = left | center | right attributes have been added to the <P> element.
Example :-
<P ALIGN=LEFT> ... </P>
All text within the paragraph will be aligned to the left side of the page layout.
This setting is equal to the default <P> element.
<P ALIGN=CENTER> ... </P>
All text within the paragraph will be aligned to the centre of the page.
<P ALIGN=RIGHT> ... </P>
All text within the paragraph will be aligned to the right side of the page.
HTML automatically adds an extra blank line before and after a paragraph.
Line Breaks
The Line Break element specifies that a new line must be started at the given point. A new line
indents the same as that of line-wrapped text.
The <br> tag is used when you want to end a line, but don't want to start a new paragraph. The
<br> tag forces a line break wherever you place it.
Example :-
<p>This <br> is a para<br>graph with line breaks</p>
The <br> tag is an empty tag. It has no closing tag.
Horizontal Rule
A Horizontal Rule element is a divider between sections of text such as a full width horizontal rule
or equivalent graphic.
Horizontal Rule defined with the <hr> tag
SIZE attribute
The SIZE attributes lets the author give an indication of how thick they wish the horizontal rule to
be. A pixel value should be given.
<HR SIZE=number>
WIDTH attribute
With the WIDTH attribute, the author can specify an exact width in pixels, or a relative width
measured in percent of document width.
<HR WIDTH=number | percent>
ALIGN attribute
Now that horizontal rules do not have to be the width of the page it possible to specify the
horizontal alignment of the rule.
<HR ALIGN=left | right | center>
COLOR attribute
Internet Explorer allows the specifying of the hard rule colour. Accepted values are any of the
Explorer supported colour names, or any acceptable rrggbb hex triplet.
<HR COLOR=name|#rrggbb>
NOSHADE attribute
For those times when a solid bar is required, the NOSHADE attribute specifies that the horizontal
rule should not be shaded at all.
<HR NOSHADE>
Comments in HTML
The comment tag is used to insert a comment in the HTML source code. A comment will be
ignored by the browser. You can use comments to explain your code, which can help you when
you edit the source code at a later date.
Example :-
<!-- This is a comment -->
Points to remember
Tag Description
<html> Defines an HTML document
<head> Defines the document's header
<title> Specifies the title of the document
<body> Defines the document's body
<h1> to <h6> Defines header 1 to header 6
<p> Defines a paragraph
<br> Inserts a single line break
<hr> Defines a horizontal rule
<!--> Defines a comment
Example :-
The instructions <B>must be read</B> before continuing.
Example :-
This is normal text, with <BIG>this bit</BIG> being big text.
Example :-
This is the main text, with <SUB>this bit</SUB> being subscript.
Example :-
This is the main text, with <SUP>this bit</SUP> being superscript.
NOTE: - The actual element detailed in HTML specifications, is <S> ... </S>, which is also
supported by the three browsers
Example :-
This text would be <STRIKE>struck through</STRIKE>
Points to remember
Tag Description
<b> Defines bold text
<big> Defines big text
<em> Defines emphasized text
<i> Defines italic text
<small> Defines small text
<strong> Defines strong text
<sub> Defines subscripted text
<sup> Defines superscripted text
<s> Defines horizontal line striking through the text
<strike> Defines horizontal line striking through the text
<u> Defines underlined text
J.Pavan Chendraa Reddy -9-
NARESH i Technologies HTML Hand Book
WIDTH attribute
The <PRE> element may be used with the optional WIDTH attribute, which is a Level 1 feature.
The WIDTH attribute specifies the maximum number of characters for a line and allows the HTML
user agent to select a suitable font and indentation. If the WIDTH attribute is not present, a width
of 80 characters is assumed. Where the WIDTH attribute is supported, widths of 40, 80 and 132
characters should be presented optimally, with other widths being rounded up.
• The <P> element should not be used. If found, it should be rendered as a move to the
beginning of the next line.
• Elements that define paragraph formatting (headings, address, etc.) must not be used.
• The horizontal tab character (encoded in US-ASCII and ISO-8859-1 as decimal 9) must
be interpreted as the smallest positive nonzero number of spaces which will leave the
number of characters so far on the line as a multiple of 8. Its use is not recommended
however.
Example :-
<PRE WIDTH="80">
This is an example line.
</PRE>
Example: -
This sentence contains an <CODE>example of code</CODE>.
Example :-
The text inside the <KBD>KBD element, would typically</KBD> render as
monospaced font.
Example :-
A sequence of <SAMP>literal characters</SAMP> commonly renders as
monospaced font.
Example :-
Text between the <TT> typetype elements</TT> should be rendered in fixed
width typewriter font.
Example :-
Some might say<LISTING>that sunshine</LISTING> follows thunder
<PLAINTEXT> Tag
The <PLAINTEXT> element can be used to represent formatted text. As such, it is similar to the
<XMP> and <LISTING> element. However, the <PLAINTEXT> element should be an open
element, with no closing element. Only Netscape supports this element according to any HTML
specification. Internet Explorer and Mosaic will both allow the use of a </PLAINTEXT> closing
element. Netscape will treat the closing element as straight text.
Typically, it will render as fixed width font with white space separating it from other text.
Example :-
We live<PLAINTEXT>as we breathe alone
The Address element specifies such information as address, signature and authorship, often at
the top or bottom of a document.
Typically, an Address is rendered in an italic typeface and may be indented. It carries an implied
paragraph break before and after the text enclosed.
Example :-
<ADDRESS>
Mr. Cosmic Kumquat<BR>
SSL Trusters Inc.<BR>
1234 Squeamish Ossifrage Road<BR>
Anywhere<BR>
NY 12345<BR>
U.S.A.
</ADDRESS>
Example :-
I think the poem ends
<BLOCKQUOTE>
<P>Soft you now, the fair Ophelia. Nymph, in thy orisons, be all my sins
remembered.
</BLOCKQUOTE>
but I am not sure.
Example :-
This sentence, containing a <CITE>citation reference</CITE>
The <DFN> element can be used to mark the Defining Instance of a term. For example, the first
time some text is mentioned in a paragraph. Typically, it will render italicised.
Example :-
The <DFN>Internet Explorer</DFN> is Microsoft's Web browser.
Points to remember
Tag Description
<code> Defines computer code text
<kbd> Defines keyboard text
<samp> Defines sample computer code
<tt> Defines teletype text
<var> Defines a variable
<pre> Defines preformatted text
<listing> Deprecated. Use <pre> instead
<plaintext> Deprecated. Use <pre> instead
<xmp> Deprecated. Use <pre> instead
Tag Description
<abbr> Defines an abbreviation
<acronym> Defines an acronym
<address> Defines an address element
<bdo> Defines the text direction
<blockquote> Defines a long quotation
<q> Defines a short quotation
<cite> Defines a citation
Exercise1 :-
Text formatting
This example demonstrates how you can format text in an HTML document.
<html>
<body>
<b>This text is bold</b>
<br>
<strong>
This text is strong
</strong>
<br>
<big>
This text is big
</big>
<br>
<em>
This text is emphasized
</em>
<br>
<i>
This text is italic
</i>
<br>
<small>
Exercise2 :-
Preformatted text
This example demonstrates how you can control the line breaks and spaces with the pre tag.
<html>
<body>
<pre>
This is
preformatted text.
It preserves both spaces
and line breaks.
</pre>
<p>The pre tag is good for displaying computer code:</p>
<pre>
for i = 1 to 10
print i
next i
</pre>
</body>
</html>
Exercise3 :-
<html>
<body>
<code>Computer code</code>
<br>
<kbd>Keyboard input</kbd>
<br>
<tt>Teletype text</tt>
<br>
<samp>Sample text</samp>
<br>
<var>Computer variable</var>
<br>
<p>
<b>Note:</b> These tags are often used to display computer/programming code.
</p>
</body>
</html>
Exercise4 :-
Address
This example demonstrates how to write an address in an HTML document.
<html>
<body>
<address>
Donald Duck<br>
BOX 555<br>
Disneyland<br>
USA
</address>
</body>
</html>
Exercise5 :-
<html>
<body>
<abbr title="United Nations">UN</abbr>
<br>
<acronym title="World Wide Web">WWW</acronym>
<p>The title attribute is used to show the spelled-out version when holding the mouse pointer
over the acronym or abbreviation.</p>
<p>This only works for the acronym element in IE 5.</p>
<p>This works for both the abbr and acronym element in Netscape 6.2.</p>
</body>
</html>
Exercise6 :-
Text direction
This example demonstrates how to change the text direction.
<html>
<body>
<p>
If your browser supports bi-directional override (bdo), the next line will be written from the right to
the left (rtl):
</p>
<bdo dir="rtl">
Here is some Hebrew text
</bdo>
</body>
</html>
Exercise7 :-
Quotations
This example demonstrates how to handle long and short quotations.
<html>
<body>
Here comes a long quotation:
<blockquote>
This is a long quotation. This is a long quotation. This is a long quotation. This is a long quotation.
This is a long quotation.
</blockquote>
Here comes a short quotation:
<q>
This is a short quotation
</q>
<p>
With the block quote element, the browser inserts line breaks and margins, but the q element
does not render as anything special.
</p>
</body>
</html>
Exercise8 :-
<html>
<body>
<p>
a dozen is
<del>twenty</del>
<ins>twelve</ins>
pieces
</p>
<p>
Most browsers will overstrike deleted text and underline inserted text.
</p>
<p>
Some older browsers will display deleted or inserted text as plain text.
</p>
</body>
</html>
Example :-
<img src="boat.gif" alt="Big Boat">
Example :-
<img src="boat.gif" alt="Big Boat" width=50 height=50>
ALIGN=texttop aligns itself with the top of the tallest text in the line (this is usually but not always
the same as ALIGN=top).
ALIGN=middle aligns the baseline of the current line with the middle of the image.
ALIGN=absmiddle aligns the middle of the current line with the middle of the image.
ALIGN=baseline aligns the bottom of the image with the baseline of the current line.
ALIGN=bottom aligns the bottom of the image with the baseline of the current line.
ALIGN=absbottom aligns the bottom of the image with the bottom of the current line.
Syntax :-
< IMG ALIGN= left | right | top | texttop | middle | absmiddle | baseline | bottom | absbottom >
Exercise 1 : -
Insert images
This example demonstrates how to display images in your Web page.
<html>
<body>
<p>
An image:
<img src="constr4.gif"
width="144" height="50">
</p>
<p>
A moving image:
<img src="hackanm.gif"
width="48" height="48">
</p>
<p>
Note that the syntax of inserting a moving image is no different from that of a non-moving image.
</p>
</body>
</html>
Exercise 2 : -
Insert images from different locations
This example demonstrates how to display images from another folder or another server in your
Web page.
<html>
<body>
<p>
An image from another folder:
<img src="/images/netscape.gif"
width="33" height="32">
</p>
<p>
An image from W3Schools:
<img src="http://www.w3schools.com/images/ie.gif" width="73" height="68">
</p>
</body>
</html>
Exercise 3 : -
Background image
This example demonstrates how to add a background image to an HTML page.
<html>
<body background="background.jpg">
<h3>Look: A background image!</h3>
<p>Both gif and jpg files can be used as HTML backgrounds.</p>
<p>If the image is smaller than the page, the image will repeat itself.</p>
</body>
</html>
Exercise 4 : -
Aligning images
This example demonstrates how to align an image within the text.
<html>
<body>
<p>
An image
<img src="hackanm.gif"
align="bottom" width="48" height="48">
in the text
</p>
<p>
An image
<img src ="hackanm.gif"
align="middle" width="48" height="48">
in the text
</p>
<p>
An image
<img src ="hackanm.gif"
align="top" width="48" height="48">
in the text
</p>
<p>Note that bottom alignment is the default alignment</p>
<p>
An image
<img src ="hackanm.gif"
width="48" height="48">
in the text
</p>
<p>
<img src ="hackanm.gif"
width="48" height="48">
An image before the text
</p>
<p>
An image after the text
<img src ="hackanm.gif"
width="48" height="48">
</p>
</body>
</html>
Exercise 5 : -
Let the image float
This example demonstrates how to let an image float to the left or right of a paragraph.
<html>
<body>
<p>
<img src ="hackanm.gif"
align ="left" width="48" height="48">
A paragraph with an image. The align attribute of the image is set to "left". The image will float to
the left of this text.
</p>
<p>
<img src ="hackanm.gif"
align ="right" width="48" height="48">
A paragraph with an image. The align attribute of the image is set to "right". The image will float to
the right of this text.
</p>
</body>
</html>
Exercise 6 : -
<html>
<body>
<p>
<img src="hackanm.gif"
width="20" height="20">
</p>
<p>
<img src="hackanm.gif"
width="45" height="45">
</p>
<p>
<img src="hackanm.gif"
width="70" height="70">
</p>
<p>
You can make a picture larger or smaller changing the values in the "height" and "width"
attributes of the
img tag.
</p>
</body>
</html>
Exercise 7 : -
<html>
<body>
<img src="goleft.gif" alt="Go Left" width="32" height="32">
<p>
Text-only browsers cannot display images and will only display the text that is specified in the
"alt" attribute for the image. Here, the "alt"-text is "Go Left".</p>
<p>
Note that if you hold the mouse pointer over the image, most browsers will display the "alt"-text.
</p>
</body>
</html>
The <a> tag is used to create an anchor to link from, the href attribute is used to address the
document to link to, and the words between the open and close of the anchor tag will be
displayed as a hyperlink.
Syntax :-
<a href="url">Text to be displayed</a>
The name attribute is used to create a named anchor. When using named anchors we can create
links that can jump directly into a specific section on a page, instead of letting the user scroll
around to find what he/she is looking for.
Syntax :-
<a name="label">Text to be displayed</a>
Browser windows can now have names associated with them. Links in any window can refer to
another window by name. With the target attribute, you can define where the linked document will
be opened.
Syntax :-
<A HREF="url.html" TARGET="window_name">Link text</A>
Exercise 1 : -
Create hyperlinks
This example demonstrates how to create links in an HTML document.
<html>
<body>
<p>
<a href="lastpage.htm">
This text</a> is a link to a page on
this Web site.
</p>
<p>
<a href="http://www.microsoft.com/">
This text</a> is a link to a page on
the World Wide Web.
</p>
</body>
</html>
Exercise 2 : -
An image as a link
This example demonstrates how to use an image as a link.
<html>
<body>
<p>
You can also use an image as a link:
<a href="lastpage.htm">
<img border="0" src="buttonnext.gif" width="65" height="38">
</a>
</p>
</body>
</html>
Exercise 3 : -
<html> <body>
<a href="lastpage.htm" target="_blank">Last Page</a>
<p>
If you set the target attribute of a link to "_blank",
the link will open in a new window.
</p>
</body> </html>
Exercise 4 : -
<html>
<body>
<p>
<a href="#C4">
See also Chapter 4.
</a>
</p>
<p>
<h2>Chapter 1</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 2</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 3</h2> <p>This chapter explains ba bla bla</p>
<a name="C4"><h2>Chapter 4</h2></a>
<p>This chapter explains ba bla bla</p>
<h2>Chapter 5</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 6</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 7</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 8</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 9</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 10</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 11</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 12</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 13</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 14</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 15</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 16</h2> <p>This chapter explains ba bla bla</p>
<h2>Chapter 17</h2> <p>This chapter explains ba bla bla</p>
</body>
</html>
Exercise 5 : -
<html>
<body>
<p>Locked in a frame?</p>
<a href="http://www.nareshit.com/"
target="_top">Click here!</a>
</body>
</html>
Exercise 6 : -
<html>
<body>
<p>
This is a mail link:
<a href="mailto:someone@microsoft.com?subject=Hello%20again">
Send Mail</a>
</p>
<p>
<b>Note:</b> Spaces between words should be replaced by %20 to <b>ensure</b> that the
browser will display your text properly.
</p>
</body>
</html>
Exercise 7 : -
<html>
<body>
<p>
This is another mailto link:
<a href="mailto:someone@microsoft.com?
cc=someoneelse@microsoft.com&bcc=andsomeoneelse2@microsoft.com&subject=Summer
%20Party&body=You%20are%20invited%20to%20a%20big%20summer%20party!">Send mail!
</a>
</p>
<p>
<b>Note:</b> Spaces between words should be replaced by %20 to <b>ensure</b> that the
browser will display your text properly.
</p>
</body>
</html>
<area> Tag
An arbitrary number of AREA tags may be specified. If two areas intersect, the one which
appears first in the map definition takes precedence in the overlapping region. For example, a
button bar in a document might use a 160 pixel by 60 pixel image and appear like this:
<MAP NAME="buttonbar">
<AREA SHAPE="RECT" COORDS="10,10,49,49" HREF="about_us.html">
<AREA SHAPE="RECT" COORDS="60,10,99,49" HREF="products.html">
<AREA SHAPE="RECT" COORDS="110,10,149,49" HREF="index.html">
<AREA SHAPE="RECT" COORDS="0,0,159,59" NOHREF>
</MAP>
Any region of the image that is not defined by an AREA tag is assumed to be NOHREF.
NOTE: - The TARGET attribute can be used within the <AREA> element, allowing the use of
Client side image maps within framed documents.
Points to remember
Tag Description
<img> Defines an image
<map> Defines an image map
<area> Defines a clickable area inside an image map
Tag Description
<a> Defines an anchor
Exercise 1 : -
<html>
<body>
<p>
You can also use an image as a link:
<a href="lastpage.htm">
<img border="0" src="buttonnext.gif" width="65" height="38">
</a>
</p>
</body>
</html>
Exercise 2 : -
<html>
<body>
<p>
Click on one of the planets to watch it closer:
</p>
<img src="planets.gif"
width="145" height="126"
usemap="#planetmap">
<map id="planetmap" name="planetmap">
<area shape="rect"
coords="0,0,82,126"
alt="Sun"
href="sun.htm">
<area shape="circle"
coords="90,58,3"
alt="Mercury"
href="mercur.htm">
<area shape="circle"
coords="124,58,8"
alt="Venus"
href="venus.htm">
</map>
<p>
<b>Note:</b> We use both an <b>id</b> and a <b>name</b> attribute in the map tag
because some versions of Netscape don't understand the id attribute.</p>
</body>
</html>
Exercise 3 : -
<html>
<body>
<p>
Move the mouse over the image, and look at the status bar to see how the
coordinates change.
</p>
<p>
<a href="tryhtml_ismap.htm">
<img src="planets.gif"
ismap width="146" height="126">
</a>
</p>
</body>
</html>
Tables
Example :-
Example :-
<TABLE BORDER=1 WIDTH=400 HEIGHT=200>
<TABLE BORDER=1 WIDTH=40% HEIGHT=20%>
If this attribute appears in any table cell (<TH> or <TD>) it means the lines within this cell cannot
be broken to fit the width of the cell. Be cautious in use of this attribute as it can result in
excessively wide cells.
This attribute can appear in any table cell (<TH> or <TD>) and it specifies how many columns of
the table this cell should span. The default COLSPAN for any cell is 1.
This attribute can appear in any table cell (<TH> or <TD>) and it specifies how many rows of the
table this cell should span. The default ROWSPAN for any cell is 1. A span that extends into
rows that were never specified with a <TR> will be truncated.
Example: -
<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
Example: -
<table border="1">
<tr>
<th>Heading</th>
<th>Another Heading</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
Points to remember
Tag Description
<table> Defines a table
<th> Defines a table header
<tr> Defines a table row
<td> Defines a table cell
<caption> Defines a table caption
<colgroup> Defines groups of table columns
<col> Defines the attribute values for one or more columns in a table
<thead> Defines a table head
<tbody> Defines a table body
<tfoot> Defines a table footer
Exercise 1 :-
Tables
This example demonstrates how to create tables in an HTML document.
<html>
<body>
<p>
Each table starts with a table tag.
Each table row starts with a tr tag.
Each table data starts with a td tag.
</p>
<h4>One column:</h4>
<table border="1">
<tr>
<td>100</td>
</tr>
</table>
</body>
</html>
Exercise 2 :-
Table borders
This example demonstrates different table borders.
<html>
<body>
<h4>With a normal border:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With a thick border:</h4>
<table border="8">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With a very thick border:</h4>
<table border="15">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
Exercise 3 :-
Table with no border
This example demonstrates a table with no borders.
<html>
<body>
<h4>This table has no borders:</h4>
<table>
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
<h4>And this table has no borders:</h4>
<table border="0">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</body>
</html>
Exercise 4 :-
Headings in a table
This example demonstrates how to display table headers.
<html>
<body>
<h4>Table headers:</h4>
<table border="1">
<tr>
<th>Name</th>
<th>Telephone</th>
<th>Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
<h4>Vertical headers:</h4>
<table border="1">
<tr>
<th>First Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 854</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 855</td>
</tr>
</table>
</body>
</html>
Exercise 5 :-
Empty cells
This example demonstrates how to use " " to handle cells that have no content.
<html>
<body>
<table border="1">
<tr>
<td>Some text</td>
<td>Some text</td>
</tr>
<tr>
<td></td>
<td>Some text</td>
</tr>
</table>
<p>
As you can see, one of the cells has no border. That is because it is empty. Try to insert a space
in the cell. Still it has no border.
</p>
<p>
The trick is to insert a no-breaking space in the cell.
</p>
<p>No-breaking space is a character entity. If you don't know what a character entity is, read the
chapter about it.
</p>
<p>
</p>
</body>
</html>
Exercise 6 :-
Exercise 7 :-
Table cells that span more than one row/column
This example demonstrates how to define table cells that span more than one row or one
column.
<html>
<body>
<h4>Cell that spans two columns:</h4>
<table border="1">
<tr>
<th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
Exercise 8 :-
Tags inside a table
This example demonstrates how to display elements inside other elements.
<html>
<body>
<table border="1">
<tr>
<td>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
</td>
<td>This cell contains a table:
<table border="1">
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td>D</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>This cell contains a list
<ul>
<li>apples</li>
<li>bananas</li>
<li>pineapples</li>
</ul>
</td>
<td>HELLO</td>
</tr>
</table>
</body>
</html>
Exercise 9 :-
Cell padding
This example demonstrates how to use cellpadding to create more white space between the cell
content and its borders.
<html>
<body>
<h4>Without cellpadding:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With cellpadding:</h4>
<table border="1"
cellpadding="10">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
Exercise 10 :-
Cell spacing
This example demonstrates how to use cellspacing to increase the distance between the cells.
<html>
<body>
<h4>Without cellspacing:</h4>
<table border="1">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With cellspacing:</h4>
<table border="1"
cellspacing="10">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
Exercise 11 :-
Add a background color or a background image to a table
This example demonstrates how to add a background to a table.
<html>
<body>
Exercise 12 :-
Add a background color or a background image to a table cell
This example demonstrates how to add a background to one or more table cells.
<html>
<body>
<h4>Cell backgrounds:</h4>
<table border="1">
<tr>
<td bgcolor="red">First</td>
<td>Row</td>
</tr>
<tr>
<td
background="bgdesert.jpg">
Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
Exercise 13 :-
Align the content in a table cell
This example demonstrates how to use the "align" attribute to align the content of cells, to create
a "nice-looking" table.
<html>
<body>
<table width="400" border="1">
<tr>
<th align="left">Money spent on....</th>
<th align="right">January</th>
<th align="right">February</th>
</tr>
<tr>
<td align="left">Clothes</td>
<td align="right">$241.10</td>
<td align="right">$50.20</td>
</tr>
<tr>
<td align="left">Make-Up</td>
<td align="right">$30.00</td>
<td align="right">$44.45</td>
</tr>
<tr>
<td align="left">Food</td>
<td align="right">$730.40</td>
<td align="right">$650.00</td>
</tr>
<tr>
<th align="left">Sum</th>
<th align="right">$1001.50</th>
<th align="right">$744.65</th>
</tr>
</table>
</body>
</html>
Exercise 14 :-
The frame attribute
This example demonstrates how to use the "frame" attribute to control the borders around the
table.
<html>
<body>
<p>
If you see no frames around the tables in these examples, your browser is too old, or does not
support it.
</p>
<h4>With frame="border":</h4>
<table frame="border">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="box":</h4>
<table frame="box">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="void":</h4>
<table frame="void">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="above":</h4>
<table frame="above">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="below":</h4>
<table frame="below">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="hsides":</h4>
<table frame="hsides">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="vsides":</h4>
<table frame="vsides">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="lhs":</h4>
<table frame="lhs">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
<h4>With frame="rhs":</h4>
<table frame="rhs">
<tr>
<td>First</td>
<td>Row</td>
</tr>
<tr>
<td>Second</td>
<td>Row</td>
</tr>
</table>
</body>
</html>
Forms
The ENCTYPE attribute specifies the format of the submitted data in case the protocol does not
impose a format itself.
Note: - When the ACTION attribute is set to an HTTP URL, the METHOD attribute must be set to
an HTTP method as defined by the HTTP method specification in the IETF draft HTTP standard.
The default METHOD is GET, although for many applications, the POST method may be
preferred. With the post method, the ENCTYPE attribute is a MIME type specifying the format of
the posted data; by default, is application/x-www-form-urlencoded.
Input
<INPUT> Tag
The Input element represents a field whose contents may be edited by the user.
Note that the form itself is not visible. Also note that in most browsers, the width of the text field is
20 characters by default.
HIDDEN : No field is presented to the user, but the content of the field is sent with the submitted
form. This value may be used to transmit state information about client/server interaction.
PASSWORD is the same as the TEXT attribute, except that text is not displayed as it is entered.
TEXTAREA is used for multiple-line text-entry fields. Use in conjunction with the SIZE and
MAXLENGTH attributes.
SUBMIT is a button that when pressed submits the form. You can use the VALUE attribute to
provide a non- editable label to be displayed on the button.
RESET is a button that when pressed resets the form's fields to their specified initial values. The
label to be displayed on the button may be specified just as for the SUBMIT button.
BUTTON is a button when pressed it doesn’t perform any action by default. We need to write
script to specify what to do when button pressed.
RADIO is used for attributes that accept a single value from a set of alternatives. Each radio
button field in the group should be given the same name. Only the selected radio button in the
group generates a name/value pair in the submitted data. Radio buttons require an explicit
VALUE attribute.
CHECKBOX : Used for simple Boolean attributes, or for attributes that can take multiple values at
the same time. Checkboxes are used when you want the user to select one or more options of a
limited number of choices.
IMAGE : The image itself is specified by the SRC attribute, exactly as for the Image element. An
image field upon which you can click with a pointing device, causing the form to be immediately
submitted. The co-ordinates of the selected point are measured in pixel units from the upper-left
corner of the image, and are returned (along with the other contents of the form) in two
name/value pairs. The x-co-ordinate is submitted under the name of the field with .x appended,
and the y- co-ordinate is submitted under the name of the field with .y appended. Any VALUE
attribute is ignored.
Points to remember
Tag Description
<form> Defines a form for user input
<input> Defines an input field
<textarea> Defines a text-area (a multi-line text input control)
<label> Defines a label to a control
<fieldset> Defines a fieldset
<legend> Defines a caption for a fieldset
<select> Defines a selectable list (a drop-down box)
<optgroup> Defines an option group
<option> Defines an option in the drop-down box
<button> Defines a push button
<isindex> Deprecated. Use <input> instead
Example 1 :-
Text fields
<html> <body>
<form>
First name:
<input type="text" name="firstname">
<br>
Last name:
<input type="text" name="lastname">
</form>
</body> </html>
Example 2 :-
Password fields
<html> <body>
<form>
Username:
<input type="text" name="user">
<br>
Password:
<input type="password" name="password">
</form>
<p>
Note that when you type characters in a password field, the browser displays asterisks or bullets
instead of the characters.
</p>
</body> </html>
Example 3 :-
Checkboxes
<html>
<body>
<form>
I have a bike:
<input type="checkbox" name="Bike">
<br>
I have a car:
<input type="checkbox" name="Car">
</form>
</body>
</html>
Example 4 :-
Radiobuttons
<html> <body>
<form>
Male:
<input type="radio" checked="checked"
name="Sex" value="male">
<br>
Female:
<input type="radio"
name="Sex" value="female">
</form>
<p>
When a user clicks on a radio-button, the button becomes checked, and all other buttons with the
same name become unchecked
</p>
</body> </html>
Example 5 :-
<html> <body>
<form>
<select name="cars">
<option value="volvo">Volvo
<option value="saab">Saab
<option value="fiat">Fiat
<option value="audi">Audi
</select>
</form>
</body> </html>
Example 6 :-
Example 7 :-
Create a button
<html>
<body>
<form>
<input type="button" value="Hello world!">
</form>
</body>
</html>
Example 8 :-
Fieldset around data
<html>
<body>
<fieldset>
<legend>
Health information:
</legend>
<form>
Height <input type="text" size="3">
Weight <input type="text" size="3">
</form>
</fieldset>
<p>
If there is no border around the input form, your browser is too old.
</p>
</body>
</html>
Example 9 :-
Form with input fields and a submit button
<html> <body>
<form name="input" action="html_form_action.asp" method="get">
Type your first name:
<input type="text" name="FirstName" value="Mickey" size="20">
<br>Type your last name:
<input type="text" name="LastName" value="Mouse" size="20">
<br>
<input type="submit" value="Submit">
</form>
<p>
If you click the "Submit" button, you will send your input to a new page called
html_form_action.asp.
</p>
</body> </html>
Example 10 :-
<html> <body>
<form name="input" action="html_form_action.asp" method="get">
I have a bike:
<input type="checkbox" name="Bike" checked="checked"><br>
I have a car:
<input type="checkbox" name="Car">
<br>
<input type="submit" value="Submit">
</form>
<p>
If you click the "Submit" button, you send your input to a new page called
html_form_action.asp.
</p>
</body> </html>
Example 11 :-
Form with radio buttons
<html>
<body>
Male:
<input type="radio" name="Sex" value="Male" checked="checked">
<br>
Female:
<input type="radio" name="Sex" value="Female">
<br>
<input type ="submit" value ="Submit">
</form>
<p>
If you click the "Submit" button, you will send your input to a new page called
html_form_action.asp.
</p>
</body>
</html>
Example 12 :-
<html>
<body>
<form action="MAILTO:someone@w3schools.com" method="post" enctype="text/plain">
</form>
</body>
</html>
Frames
With frames, you can display more than one HTML document in the same browser window. Each
HTML document is called a frame, and each frame is independent of the others.
The disadvantages of using frames are:
o The web developer must keep track of more HTML documents
o It is difficult to print the entire page
<FRAMESET> Tag
The <frameset> tag defines how to divide the window into frames each frameset defines a set of
rows or columns. The values of the rows/columns indicate the amount of screen area each
row/column will occupy.
<FRAMESET ROWS="100,*,100">
<FRAME> Tag
This tag defines a single frame in a frameset. It has 6 possible attributes.
This will effect the resizability of other frames. The NORESIZE attribute is optional; by default all
frames are resizable.
Example 1 :-
Vertical frameset
<html>
<frameset cols="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
</html>
Example 2 :-
Horizontal frameset
<html>
<frameset rows="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
</html>
Example 3 :-
How to use the <noframes> tag
<html>
<frameset cols="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
<noframes>
<body>Your browser does not
handle frames!</body>
</noframes>
</frameset>
</html>
Example 4 :-
Mixed frameset
<html>
<frameset rows="50%,50%">
<frame src="frame_a.htm">
<frameset cols="25%,75%">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
</frameset>
</html>
Example 5 :-
Frameset with noresize="noresize"
<html>
<frameset rows="50%,50%">
<frame noresize="noresize" src="frame_a.htm">
<frameset cols="25%,75%">
<frame noresize="noresize" src="frame_b.htm">
<frame noresize="noresize" src="frame_c.htm">
</frameset>
</frameset>
</html>
Example 6 :-
Navigation frame
<html>
<frameset cols="120,*">
<frame src="tryhtml_contents.htm">
<frame src="frame_a.htm"
name="showframe">
</frameset>
</html>
Example 7 :-
Inline frame
<html>
<body>
<iframe src="default.asp"></iframe>
<p>Some older browsers don't support iframes.</p>
<p>If they don't, the iframe will not be visible.</p>
</body>
</html>
Example 8 :-
Jump to a specified section within a frame
<html>
<frameset cols="20%,80%">
<frame src="frame_a.htm">
<frame src="link.htm#C10">
</frameset>
</html>
Example 9 :-
Jump to a specified section with frame navigation
<html>
<frameset cols="180,*">
<frame src="content.htm">
<frame src="link.htm" name="showframe">
</frameset>
</html>