UNIT2 (Ii Part)
UNIT2 (Ii Part)
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 −
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
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>
<!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>
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
<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
</html>
5
This will produce the following result –
<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 –
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.
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>
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>
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.
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.
Types of Hyperlinks:
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.
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>
The absence of filename before # symbol indicates that a jump is required within the same
document.
11