A table can contain an infinite number of table rows. Each table row is essentially a table element itself, with an opening and closing tag (<tr> </tr>). Table columns are also considered child elements of HTML tables, and like table rows, an HTML table may contain an infinite number of table data cells (<td> <tr>).
Table rows and columns are container elements that house other HTML elements such as text links, images, and lists, as we've seen in previous examples. Below, we've applied a background color to the table example in order to help distinguish the different table elements.
<table border="1"> <tr title="You are looking at Row 1" bgcolor="silver"> <td>Row 1 Cell 1</td> <td>Row 1 Cell 2</td> </tr> <tr title="You are looking at Row 2" bgcolor="aqua"> <td>Row 2 Cell 1</td> <td>Row 2 Cell 2</td> </tr> </table>
Row 1 Cell 1
Row 2 Cell 1
Row 1 Cell 2
Row 2 Cell 2
Use rowspan to span multiple rows merging together table rows and colspan to span across multiple columns.
<table border="1"> <tr> <td><b>Column 1</b></td> <td><b>Column 2</b></td> <td><b>Column 3</b></td> </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>
With the cellpadding and cellspacing attributes, you will be able to adjust the spacing between table cells. Setting the cellpadding attribute determines how much space will exist between a table cell border and the elements contained within it, whereas cellspacing determines how much space will exist between each table cell. Color has been added to the table below to emphasize these attributes.
<table border="1" cellspacing="10" bgcolor="rgb(0,255,0)"> <tr> <td><b>Column 1</b></td> <td><b>Column 2</b></td> </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>
Column 1
Row 1 Cell 1
Row 2 Cell 1
Column 2
Row 1 Cell 2
Row 2 Cell 2
And now we will change the cellpadding of the table and remove the cellspacing from the previous example. This should clearly demonstrate the difference between cellpadding andcellspacing.
<table border="1" cellpadding="10" bgcolor="rgb(0,255,0)"> <tr> <td><b>Column 1</b></td> <td><b>Column 2</b></td> </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>
Column 1
Row 1 Cell 1
Row 2 Cell 1
Column 2
Row 1 Cell 2
Row 2 Cell 2