0% found this document useful (0 votes)
34 views11 pages

UNIT2 (Ii Part)

Uploaded by

rahulkumarnote9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views11 pages

UNIT2 (Ii Part)

Uploaded by

rahulkumarnote9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT-2

HTML Table
HTML table tag is used to display data in tabular form ( in the forms of rows columns). There can
be many columns in a row.
HTML tables are used to manage the layout of the page e.g. header section, navigation bar, body
content, footer section etc
The HTML tables are created using the <table>……..</table> tag in which the <tr> tag is used to
create table rows and <td> tag is used to create data cells. The elements under <td> are regular and
left aligned by default.

<!DOCTYPE html>
<html>

<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>
This will produce the following result −

Row 1, Column 1 Row 1, Column 2


Row 2, Column 1 Row 2, Column 2

Here, the 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 we will put our top row as table heading, otherwise we can
use <th> element in any row.
Headings, which are defined in <th> tag are centered and bold by default.

<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
</head>
<body>
<table border = "1">
<tr>
1
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>abc </td>
<td>5000</td>
</tr>
<tr>
<td>xyz</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
This will produce the following result −
Name Salary
abc 5000
xyz 7000

Attributes of Table Tag:

1. Tables Backgrounds:
We can set table background using one of the following two ways −
 bgcolor attribute − we can set background color for whole table or just for one cell.
 background attribute − we can set background image for whole table or just for one cell.
2. Border color (bordercolor): We can also set border color also using bordercolor attribute.
Note − The bgcolor, background, and bordercolor attributes deprecated in HTML5. Do not use these
attributes.
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
</head>
<body>
<table border = "1" bgcolor=”yellow” bordercolor=”red”>
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>abc </td>
<td>5000</td>
</tr>
<tr>
<td>xyz</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
2
This will produce the following result −

Name Salary

abc 5000

xyz 7000

Example of using background attribute. Here we will use an image available in /images
directory:
<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>

3. Table Height and Width:


We can set a table width and height using width and height attributes. We can specify table width or
height in terms of pixels or in terms of percentage of available screen area.

<!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>
3
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>

Table Caption: (<caption>…….</caption>)

The caption tag will serve as a title or explanation for the table and it shows up at the top of the
table. This tag is deprecated in newer version of HTML/XHTML

<!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>
This is the caption
row 1, column 1 row 1, column 2
row 2, column 1 row 2, column 2

Cellpadding and Cellspacing Attributes:


There are two attributes called cellpadding and cellspacing which we will use to adjust the white
space in our table cells.
The cellspacing attribute defines space between table cells.
The cellpadding represents the distance between cell borders and the content within a cell.
<!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>
4
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</body>

</html>
This will produce the following result –

Name Salary

abc 5000

xyz 7000

Colspan and Rowspan Attributes:


We will use colspan attribute if we want to merge two or more columns into a single column.
Similar way we will use rowspan if we want to merge two or more rows.
<!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>

5
This will produce the following result –

Column 1 Column 2 Column 3


Row 1 Cell 2 Row 1 Cell 3
Row 1 Cell 1
Row 2 Cell 2 Row 2 Cell 3
Row 3 Cell 1

Table Header, Body, and Footer:


Tables can be divided into three portions − a header, a body, and a foot.
The head and foot are rather similar to headers and footers in a word-processed document that
remain the same for every page, while the body is the main content holder of the table.
The three elements for separating the head, body, and foot of a table are −
 <thead> − to create a separate table header.
 <tbody> − to indicate the main body of the table.
 <tfoot> − to create a separate table footer.
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>
<!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>
</body>

</html>

6
This will produce the following result –

This is the head of the table


This is the foot of the table
Cell 1 Cell 2 Cell 3 Cell 4

7
Images in HTML

Images are very important to beautify as well as to depict many complex concepts in simple way on
our web page.
HTML img tag is used to display image on the web page. HTML img tag is an empty tag that
contains attributes only, closing tags are not used in HTML image element.

Inserting an Image:

We can insert any image in our web page by using <img> tag. Following is the simple syntax to use
this tag.
<img src = "Image URL" ... attributes-list/>
<!DOCTYPE html>
<html>

<head>
<title>Using Image in Webpage</title>
</head>

<body>
<p>Simple Image Insert</p>
<img src = "test.jpg" alt = "Test Image" />
</body>

</html>

 We can use PNG, JPEG or GIF image. Image name is always case sensitive.

Set Image Location:

Usually we keep all the images in a separate directory. So let's keep HTML file test.htm in our home
directory and create a subdirectory images inside the home directory where we will keep our image
test.jpg.
Assuming our image location is "image/test.jpg"
<!DOCTYPE html>
<html>

<head>
<title>Using Image in Webpage</title>
</head>

<body>
<p>Simple Image Insert</p>
<img src = "/html/images/test.jpg" alt = "Test Image" />
</body>

</html>
Attributes of <img> tag:

1. src attribute:
It is a necessary attribute that describes the source or path of the image. It instructs the browser
where to look for the image on the server.
The location of image may be on the same directory or another server.
8
2. Set Image Width/Height:

We can set image width and height based on our requirement using width and height attributes. We
can specify width and height of the image in terms of either pixels or percentage of its actual size.
<!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.jpg" alt = "Test Image" width = "150" height = "100"/>
</body>
</html>

3. Set Image Border:

By default, image will have a border around it, you can specify border thickness in terms of pixels
using border attribute. A thickness of 0 means, no border around the picture.
<!DOCTYPE html>
<html>
<head>
<title>Set Image Border</title>
</head>
<body>
<p>Setting image Border</p>
<img src = "/html/images/test.jpg" alt = "Test Image" border = "3"/>
</body>
</html>
4. Set Image Alignment:

By default, image will align at the left side of the page, but we can use align attribute to set it in the
center or right.

!DOCTYPE html>
<html>

<head>
<title>Set Image Alignment</title>
</head>
<body>
<p>Setting image Alignment</p>
<img src = "/html/images/test.jpg" alt = "Test Image" border = "3" align = "right"/>
</body>
</html>

5. Set alternate Text


The alt attribute is a mandatory attribute which specifies an alternate text for an image, if the image
cannot be displayed.

9
Linking a Documents

A webpage can contain various links that take you directly to other pages and even specific parts of a
given page. These links are known as hyperlinks.
Hyperlinks allow visitors to navigate between Web sites by clicking on words, phrases, and images.
Thus we can create hyperlinks using text or images available on a webpage,

The HTML anchor tag: A link is specified using HTML tag <a>. This tag is called anchor tag and
anything between the opening <a> tag and the closing </a> tag becomes part of the link and a user
can click that part to reach to the linked document. Following is the simple syntax to use <a> tag.

href attribute of HTML anchor tag:

The href attribute is used to define the address of the file to be linked. In other words, it points out
the destination page.
The syntax of HTML anchor tag is given below.

<a href = "..........."> Link Text </a>


The browser distinguishes Hyperlinks form normal text by displaying them in different color or font.

Types of Hyperlinks:

Hyperlinks can be of two types:


1. links to an external documents
2. links(jumps) to a specific place within the same documents.

1. Linking to the External Documents:

<a href = “details.html” > Visit my Vome Page </a>

Here Visit my Home Page becomes hyperlinks and links to another document, details.html, which is
present in the current working directory. If the file is not present in the current working directory , a
relative or absolute path can be specified.

By default hyperlinks takes a user to beginning of the new web page. At the time, it might be
necessary to jump to particular location within the new web page. To enable jump to a specific
location on a web page named anchor can be set up. Anchors targets hyperlinks to a specific location
point on a web page.

Jumping to particular location on a web page can be done in to two steps:

Step 1:
Mark the location to be jumped to i.e. identify the location in a web page to jump to by giving the
location name.
Using the Name attribute of the <a> tag does this :

Syntax:
<a Name =”Location_Name”>
Ex: <a Name =”point1”>

Step 2:
While jumping to a specific web page and specific location on a web page , in addition to the name
of the web page to be jump to the name of the location on the web page to go to is required.
Hence the web page to jump to , require filename.html, together with the name of the location to
jump to in the HTML file.
10
This is done as follows:
Syntax:
<a href = ”filename.html” #Location_Name”>……….</a>
Ex:
<a href = ”details.html” #point1”> Visit my Home Page</a>

2. Internal document linking:


Sometimes a jump a is required to a different location in the same document. Since the jump has to
be targeted to specific location, on same document. We have to identify the location with a name and
then jump to that location using the name.
The only difference is that the file name is now will be the current file name.
Syntax:

<a Name =”Location_Name”>


<a href = #Location_Name”>……….</a>

The absence of filename before # symbol indicates that a jump is required within the same
document.

11

You might also like