Tables arrange data in rows and columns, such as a timetable, a marks list or a price chart.

Basic Table Tags

A table is created with <table>. Each row is a <tr> (table row). Each cell is a <td> (table data), or a <th> (table heading), which is bold and centred by default. A <caption> gives the title of the table and is placed just after <table>. The rows can be grouped into <thead> (heading rows), <tbody> (body rows) and <tfoot> (footer rows, such as totals), which helps browsers, styling and screen readers.

Definition: Table. An HTML structure created with the table element that arranges data in rows (tr) and cells (td or th), used for tabular information such as timetables and price lists.

Attributes and Merging Cells

The old attributes border, cellpadding (space inside cells), cellspacing (space between cells), width and align still work but are better done with CSS. A cell can span several columns with colspan="n" or several rows with rowspan="n", which merges cells. For example, a "Lunch Break" cell can span all the periods of a column in a timetable.

Styling Tables

With CSS, the border property draws borders, border-collapse: collapse joins double borders into single lines, padding gives space in cells, and nth-child(even) can colour alternate rows to make long tables easier to read.

Good Practice

Tables should be used only for tabular data, not for page layout, which is done with div and CSS. Every table should have heading cells so that the meaning of each column is clear.

Accessible Tables

A table is read cell by cell by screen readers, so it must be clear which heading each cell belongs to. The scope attribute on a heading cell, scope="col" or scope="row", tells assistive software whether the heading applies to a column or a row. Complex tables with merged cells can use the headers attribute to link each cell to its heading cells by id. Keeping tables simple, with one heading row and one heading column, is the best way to make them easy for everyone to understand, on large screens and on phones.

Table 1.4: Table tags

Tag Meaning Note
<table> Table Container
<tr> Table row Holds cells
<th> Heading cell Bold, centred
<td> Data cell Normal cell
<caption> Table title Just after <table>

Example 1.4: A class timetable

Problem: Create part of a class timetable with days as rows and periods as columns, a heading row, a caption, and a lunch break that spans all the days.

Solution:

  1. Write <table border="1"> with a <caption>.
  2. The first row uses <th> for "Day" and the periods.
  3. Each day is a <tr> with <td> cells for subjects.
  4. The lunch cell in the first day's row has rowspan equal to the number of days.

Result: The table shows the timetable with a merged lunch column.

<table border="1" cellpadding="6">
  <caption>BCA Semester II Timetable</caption>
  <tr><th>Day</th><th>9:00</th><th>10:00</th>
      <th>11:00</th><th>12:00</th></tr>
  <tr><td>Monday</td><td>Data Structures</td><td>Java</td>
      <td rowspan="3">Lunch</td><td>Web Tech</td></tr>
  <tr><td>Tuesday</td><td>OS</td><td>Maths</td><td>Java Lab</td></tr>
  <tr><td>Wednesday</td><td>Java</td><td>OS</td><td>DS Lab</td></tr>
  <tr><td>Saturday</td><td colspan="4">Sports and Activities</td></tr>
</table>

Common mistakes

  • Using a table to lay out a whole page.
  • Making rows with different numbers of cells without colspan or rowspan.
  • Leaving out <th> headings.

Key takeaways

  • <table>, <tr>, <th>, <td> and <caption> build a table.
  • colspan merges columns; rowspan merges rows.
  • <thead>, <tbody> and <tfoot> group rows.
  • Use tables for data, not layout.

Practice questions

  1. What is the difference between <th> and <td>?
  2. How is a cell merged across three columns?
  3. Where is the <caption> tag placed?

Answers to the practice questions

  1. <th> is a heading cell, bold and centred; <td> is a data cell.
  2. With colspan="3".
  3. Immediately after the opening <table> tag.

Sources

  • Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit I