HTML Tables

Tables are very useful to arrange in HTML and they are used very frequently by almost all web developers. Tables are just like spreadsheets and they are made up of rows and columns.
You will create a table in HTML/XHTML by using <table> tag. Inside <table> element the table is written out row by row. A row is contained inside a <tr> tag . which stands for table row. And each cell is then written inside the row element using a <td> tag . which stands for table data.
Example:
<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>

This will produce following result:


NOTE: In the above example border is an attribute of <table> and it will put border across all the cells. If you do not need a border then you can use border="0".
For creation of a table in HTML document, you must know the following tags :- <table></table><tr></tr><th></th><td></td>
Table Tag <table> </table> - this is main table, which is responsible for drawing a table on the web page. But in HTML, as we know it is a tag language all the rows and columns are created using different tags in this <table> tag.
Table Row  Tag <tr> </tr> :-  This <tr> tag is used to insert a new row in the table. After inserting a blank row we can insert either a header data or simple table row data with the help of <th> or <td> tags respectively.
Table Heading - The <th> Element:
Table heading can be defined using <th> element. This will create a line with bold text, which will be shown as a table header.
Table Data :- The <td> Element :-  as the <th> tag create the column headings in a table, <td></td> tag is used to create or add table data into it.  So all(table, tr, th, td)the tags are necessary to define a table in our HTML document.
There is a very simple example of the HTML table. But we can create more beautiful tables using CSS code, which we will learn in another series of blogs.
For Example:-
<table border="1"> <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>

This will produce following result. You can see its making heading as a bold one:


NOTE: Each cell must, however, have either a <td> or a <th> element in order for the table to display correctly even if that element is empty.
Attributes of a TABLE Tags
Table Cellpadding and Cellspacing:
There are two attribiutes called cellpadding and cellspacing which you will use to adjust the space in your table cell. Cellspacing defines the width of the border or in another term we can say space between cells, while cellpadding represents the distance between cell borders and the content within. For example : - I had included the following code in the previous HTML code file and execute it on browser. Please see the difference in both the similar table but having different cell space and padding.
<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>

This will produce following result:
 

Colspan and Rowspan Attributes: -  some time we require the merging of two or more cells in one cell to represent some data. As in the editor tools we have an option for it but if we want to do it in HTML then we have to include Colspan (column cells merge) and Rowspan (row cells merge) attributes. Using these attributes we can create very complex table also.
For example:
<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>

This will produce following result:
 



Formatting of tables in HTML :-
Tables Backgrounds :- we can set the background color of our table according to our webpage theme.
There are 2 different ways You can set table background using of the following two ways:
·         Using bgcolor attribute - You can set background color for whole table or just for          one cell.
·         Using background attribute - You can set background image for whole table or just         for one cell.

Here is an example of using background attribute:
<table border="1" background="Home Page Image for RCCodemantra.jpg"> <tr>
<th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr>
<tr><td rowspan="2">Row 1 Cell 1</td>
<td bgcolor="red">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" background="/images/pattern1.gif"> Row 3 Cell 1

</td></tr> </table>





BorderColor :- You can set border color also using bordercolor attribute.
<table border="5" background="Home Page Image for RCCodemantra.jpg" bordercolor=blue> <tr>
<th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr>
<tr><td rowspan="2">Row 1 Cell 1</td>
<td bgcolor="red">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" background="/images/pattern1.gif"> Row 3 Cell 1
</td></tr> </table>





Table height and width:
You can control the height and width of the table using width and height attributes. You can specify table width or height in terms of integer value or in terms of percentage of available screen area. Following is the example:
<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>
This will produce following result:







Table Caption:
The caption tags will serve as a title or explanation and show up at the top of the table. This tag is deprecated in newer version of HTML/XHTML.
<table border="5" background="Home Page Image for RCCodemantra.jpg" bordercolor=blue> <tr>
<th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr>
<tr><td rowspan="2">Row 1 Cell 1</td>
<td bgcolor="red">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" background="pattern1.gif"> Row 3 Cell 1
</td></tr> </table>
<table border="1" width="400" height="150"> <tr>
<td>Row 1, Column 1</td> <td>Row 1, Column 2</td> </tr>
<tr>
<table border="1">
<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, Column 2</td> </tr>
</table>

This will produce following result:

A 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 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>
<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>
<tr>
...more rows here containing four cells... </tr>
</tbody> <tbody> <tr>
<td>Cell 1</td> <td>Cell 2</td> <td>Cell 3</td> <td>Cell 4</td> </tr>
<tr> ...more rows here containing four cells... </tr>
</tbody> </table>

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>.
<table border="1"> <tr>
<td>
<table border="1"> <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>
</td> <td>
<ul>
<li>This is another cell</li> <li>Using list inside this cell</li> </ul>
</td> </tr> <tr>
<td>Row 2, Column 1</td> <td>Row 2, Column 2</td> </tr>
</table>
This will produce following result:







This was very basic for tables in HTML, but we can do many more formatting using CSS, which I will cover in my CSS tutorial blog.









No comments:

Post a Comment