HTML TABLES

The HTML tables are used to arrange data like text, images, link etc. into rows and columns of cells.

The HTML tables are created using the <table> tag in which the <tr> tag is used to define table rows and <td> tag is used to create data cells. A table header is defined with the <th> tag. By default, table headings are bold and centered.

<!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 gives us the result

8-1

USING <th> TAG

<th> replaces <td> tag, in your top row as table heading as shown below, otherwise you can use <th> element in any row.

<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
</head>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Subject</th>
</tr>
<tr>
<td>Amruta</td>
<td>HTML</td>
</tr>
<tr>
<td>Saurabh</td>
<td>Selenium</td>
</tr>
</table>
</body>
</html>

This will produce following result:

using th

ATTRIBUTES FOR TABLE

1.BORDER:

In the example above border is used to put a border across all the cells. If you do not need a border then you can use border=”0″.

A border is set using the CSS border property.

If you do not specify a border for the table, it will be displayed without borders.

If you want the borders to collapse into one border, add the CSS border-collapse property.

table, th, td {

border: 1px solid black;

border-collapse: collapse;}

 

2.Cellpadding and Cellspacing:

These two attribiutes are used to adjust the white space in your table cells.

The cellspacing attribute defines the width of the border, while cellpadding represents the distance between cell borders and the content within a cell.

3.Colspan and Rowspan

You will use colspan attribute if you want to merge two or more columns into a single column. Similar way you will use rowspan if you want to merge two or more rows.

Example(colspan)

<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
<style>
table, th, td {

border: 1px solid black;

border-collapse: collapse;}
</style>
</head>
<body>
<table style="width:100%;" >
 <tr>
 <th >Name</th>
 <th colspan="2">Telephone</th>
 </tr>
 <tr>
 <td>Mohan Sharma</td>
 <td>55577854</td>
 <td>55577855</td>
 </tr>
</table>

</body>
</html>

colspan

Example: (rowspan)

<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
<style>
table, th, td {

border: 1px solid black;

border-collapse: collapse;}
</style>
</head>
<body>
<table style="width:50%">
  <tr>
    <th>Name:</th>
    <td>Mohan Sharma</td>
  </tr>
  <tr>
    <th rowspan="2">Telephone:</th>
    <td>55577854</td>
  </tr>
  <tr>
    <td>55577855</td>
  </tr>
</table>

</body>
</html>

This gives the result:

rowspan

4. Height and Width

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

5.Background Color

This can be used to change the background of table to a particular colour or an image. You can set table background using one of the following two ways:

  • bgcolorattribute – You can set background color for whole table or just for one cell.
  • backgroundattribute – You can set background image for whole table or just for one cell.

You can also set border color also using bordercolor attribute.

For more styling if you want alternate rows to have different colors then you can first use “id” attribute

<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
<style>
table#t01 tr:nth-child(even) {
    background-color: AliceBlue ;
}
table#t01 tr:nth-child(odd) {
    background-color: Beige ;
}
table#t01 th {
    color: black;
    background-color: AntiqueWhite  ;

}
table, th, td {

border: 1px solid black;

border-collapse: collapse;
width:50%
}

</style>
</head>
<body>

<table id="t01">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
<tr>
    <td>Mohan</td>
    <td>Sharma</td> 
    <td>50</td>
  </tr>
<tr>
    <td>Risha</td>
    <td>Khan</td> 
    <td>9</td>
  </tr>
</table>
</body>
</html>

alternate

6.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="50%">
<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>

This will produce following result:

headers and footers

PS: For any questions, queries, feedback and comments feel free to write us at support@qatechhub.com and amruta@qatechhub.com

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: