HTML Tables


Tables

HTML tables allow web developers to arrange data into rows and columns.

A table represents information in a grid format. Examples of tables include financial reports, TV schedules, and sports results.

  • The <table> element is used to create a table. The contents of the table are written out row by row.

  • <tr> Indicate the start of each row using the opening <tr> tag. (The <tr>  stands for table row.) It is followed by one or more <td> elements (one for each cell in that row). At the end of the row you use a closing tr> tag.
  • <td> Each cell of a table is represented using a <td> element. (The td stands for table data.) At the end of each cell you use a closing td>  tag.

Example:

<table>
 <tr>
 <td>1</td>
 <td>2</td>
 </tr>
 <tr>
 <td>10</td>
 <td>20</td>
 </tr>
 <tr>
 <td>100</td>
 <td>200</td>
 </tr>
</table>



Table Headings

The <th>  element is used just like the  <td>  element but its purpose is to represent the heading for either a column or a row. (The <th> stands for table heading.) Even if a cell has no content, you should still use a <th>   or <td>   element to represent the presence of an empty cell otherwise the table will not render correctly. (The first cell in the first row of this example shows an empty cell.) Using elements for headings helps people who use screen readers, improves the ability for search engines to index your pages, and also enables you to control the appearance of tables better when you start to use CSS. You can use the scope attribute on the element to indicate whether it is a heading for a column or a row. It can take the values: row to indicate a heading for a row or col to indicate a heading for a column. Browsers usually display the content of a element in bold and in the middle of the cell.

Example:

<table>
 <tr>
 <th></th>
 <th scope="col">Mon</th>
 <th scope="col">Tue</th>
 </tr>
 <tr>
 <th scope="row">Date:</th>
 <td>10</td>
 <td>23</td>
 </tr>
 <tr>
 <th scope="row">Total sales:</th>
 <td>$300</td>
 <td>$450</td>
 </tr>
</table>



Spanning Columns

The colspan attribute can be used on a <th> or <td> element and indicates how many columns that cell should run across. 

Example:

<table>
 <tr>
 <th></th>
 <th>9am</th>
 <th>10am</th>
 <th>11am</th>
 <th>12am</th>
 </tr>
 <tr>
 <th>Monday</th>
 <td colspan="2">Geography</td>
 <td>Math</td>
 <td>Art</td>
 </tr>
 <tr>
 <th>Tuesday</th>
 <td colspan="3">Gym</td>
 <td>Home Ec</td>
 </tr>
</table>




Spanning Rows

You may also need entries in a table to stretch down across more than one row. The rowspan attribute can be used on a  or   element to indicate how many rows a cell should span down the table.

Example:

<table>
 <tr>
 <th></th>
 <th>1</th>
 <th>2</th>
 </tr>
 <tr>
 <th>6pm - 7pm</th>
 <td rowspan="2">Travl</td>
 <td>News</td>
 </tr>
 <tr>
 <th>7pm - 8pm</th>
 <td>Sport</td>
 <td>History</td>
 </tr>
</table>


Long Tables

There are three elements that help distinguish between the main content of the table and the first and last rows (which can contain different content). These elements help people who use screen readers and also allow you to style these sections in a different manner than the rest of the table.

  • The headings of the table should sit inside the  <thead> element. 
  • The body should sit inside the  <tbody>   element.
  • The footer belongs inside the  <tfoot> element. 

By default, browsers rarely treat the content of these elements any differently than other elements however designers often use CSS styles to change their appearance.

Example:

<table>
 <thead>
 <tr>
 <th>Date</th>
 <th>Income</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <th>1st January</th>
 <td>36</td>
 </tr>
 <tr>
 <th>2nd January</th>
 <td>48</td>
 </tr>
 <tr>
 <th>31st January</th>
 <td>129</td>
 <td>64</td>
 </tr>
 </tbody>
 <tfoot>
 <tr>
 <td></td>
 <td>7824</td>
 <td>1241</td>
 </tr>
 </tfoot>
</table>


Summary of HTML Tables

  • The <table> element is used to add tables to a web page.
  • A table is drawn out row by row. Each row is created with the   <tr> element.
  • Inside each row there are a number of cells represented by the   <td>  or  <th> element.
  • You can make cells of a table span more than one row or column using the rowspan and colspan attributes.
  • For long tables you can split the table into a <thead> ,<tbody> , and tfoot> .




Leave a comment
No Cmomments yet