Tables

Introduction

The HTML tags to create a table are as follows:
HTML MarkupMeaning
<table> </table>Declares a table
<caption> </caption>Defines the table's caption - provides a short description
<tr> </tr>Begins a table row
<th> </th>Begins a cell containing header text
<td> </td>Begins a cell containing regular text
The following attributes/style attributes can be added to the table elements:
Table elementCSSMeaning
<table>, <th> or <td>border, e.g.
td {border: 1px solid black;}
To specify a border for the table (or row or cell). Can be used to replace the table attribute <table border="1">.
<table>, <tr>, <th> or <td>padding, e.g.
td {padding: 25px;}
To specify the gap to be placed around the table contents (or row or cell).
background-color, e.g.
table {background-color: aqua;}
To set the background colour for the table (or row or cell).
width, e.g.
table {width: 80%;}
To specify the element is 80% of window width.
width, e.g.
table {width: 500px;}
To specify the element is 500 pixels wide.
<th>,<td>text-align, e.g.
td {text-align: right;}
To set the text alignment left, right, center or justify. Note: by default, text is aligned to the left.
vertical-align, e.g.
td {vertical-align: top;}
To set the vertical alignment top, bottom or middle. Note: by default, tables cells use middle vertical alignment.

Row(s) of cells

A Table in html is constructed horizontally, by creating rows of data cells as follows:

Displayed by browserHTML markup required
Data cell 1 Data cell 2
<table>
  <tr>
   <td> Data cell 1 </td>
   <td> Data cell 2 </td>
  </tr>
 </table>

We can use CSS to add a table border and some padding as follows.

When we use CSS to add a border by default the table borders are separated border-collapse: separate;.

In the lecture examples, the default display is changed with border-collapse: collapse; so that the borders collapse into a single border.

Example: border-collapse: separate

Displayed by browserCSS/HTML markup required
Data cell 1 Data cell 2
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}

td { 
 padding: 5px;
}
<table>
  <tr>
   <td> Data cell 1 </td>
   <td> Data cell 2 </td>
  </tr>
 </table>

You can add more rows to your table by adding a new <tr> tag followed by your table cells and content, and closing the row using the </tr> tag:

Displayed by browserCSS/HTML markup required
Data cell 1 Data cell 2
Data cell 3 Data cell 4
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}

td { 
 padding: 5px;
}
<table>
  <tr>   
   <td> Data cell 1 </td> 
   <td> Data cell 2 </td>
  </tr>
  <tr>
   <td> Data cell 3 </td>
   <td> Data cell 4 </td>
  </tr>
 </table>

Heading to a column

The tag <th> may be used instead of <td> if the cell is a header to a column of cells. For example:

Displayed by browserCSS/HTML markup required
Mnemonic Expansion
HTML Hyper Text
Markup Language
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}

th, td { 
 padding: 5px;
}
<table>
  <tr>
   <th> Mnemonic </th>
   <th> Expansion </th>
  </tr>
  <tr>
   <td> HTML </td>
   <td> Hyper Text <br> Markup Language </td>
  </tr>
 </table>

Alignment

You can align your content horizontally and vertically using CSS style attributes. For example:

Displayed by browserHTML markup required
An example to demonstrate alignment
Top left Top center Top right
Middle left Middle center Middle right
Bottom left Bottom center Bottom right
<table border="1" style="width:250px; height:250px">
 <caption>An example to demonstrate alignment</caption>
  <tr style="vertical-align:top">
     <td>Top left </td>
     <td style="text-align:center">Top center </td>
     <td style="text-align:right">Top right </td>
  </tr>
  <tr style="vertical-align:middle">
     <td>Middle left </td>
     <td style="text-align:center">Middle center </td>
     <td style="text-align:right">Middle right </td>
  </tr>
  <tr style="vertical-align:bottom">
     <td >Bottom left </td>
     <td style="text-align:center">Bottom center </td>
     <td style="text-align:right">Bottom right </td>
  </tr>
 </table> 

Spanning rows and columns

You can add the attributes rowspan and colspan to the HTML tags <td> and <th to form data cells which span more than one row or column. For example:

Displayed by browserCSS/HTML markup required
Exam Coursework
60% Tutorials 30%
Coursework 10%
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}

th, td { 
  padding: 5px;
}
<table>
  <tr>
     <th>Exam</th>
     <th colspan="2">Coursework </th>
  </tr>
  <tr>
     <td rowspan="2"> 60%</td>
     <td>Tutorials</td>
     <td> 30%</td>
  </tr>
  <tr>
     <td> Coursework</td>
     <td> 10%</td>
  </tr>
 </table> 

Colouring table cells

You can add the attributes rowspan and colspan to the HTML tags <td> and <th to form data cells which span more than one row or column. For example:

Displayed by browserCSS/HTML markup required
Type Percentage
Exam 60%
Coursework 40%
th, td { 
   padding: 5px; 
}

th { 
   text-align: left; 
}

.a { 
   background-color: #FFFFD0; 
}

.b { 
   background-color: #FFD0FF; 
}

.c { 
   background-color: #D0FFFF; 
}
<table>
  <tr class="a">
   <th>Type</th> 
   <th>Percentage</th>
  </tr>
  <tr class="b">
   <td> Exam</td>
   <td> 60%</td>
  </tr>
  <tr class="c">
   <td> Coursework</td>
   <td> 40%</td>
  </tr>
 </table>  

Table to format text

You can also use tables to format text on Web pages: For example:

Displayed by browserCSS/HTML markup required
Column 1 Column 2
This is my first column of text. I can write as much as I want.... This will be my second column. The text will be automatically formatted to fit in second columns...
th, td { 
   padding: 10px; 
}

th { 
   text-align: left; 
}
<table>
    <tr>
       <th>Column 1</th>
       <th>Column 2</th>
    </tr>
    <tr>
       <td> This is my first column of text. I can write 
       as much as I want....
       </td>
       <td> This will be my second column. The text will
       be automatically formatted to fit in second columns...
       </td>
    </tr>
 </table> 

However, in a later lecture we will use CSS to create columns.

Grouping Table elements

While <th> and <tr> tags represents a single header and a row cell respectively in a table. You can use the <thead> tag to organize and group table headers (<th>) into a distinct section for better semantic structure, accessibility, and styling.
Using <thead>, <tbody> and <tfoot> tags allows you to apply styles specifically to areas of the table you want without affecting other elements.
Screen readers and other assistive technologies rely on semantic tags to understand the structure of the table.

Displayed by Browser HTML Markup Required
Product Price
Apple £1
Banana £0.50
Orange £0.75
Total £2.25

    <table>
      <thead style="background-color: #fcf3e4; font-weight: bold;">
        <tr>
          <th>Product</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Apple</td>
          <td>£1</td>
        </tr>
        <tr>
          <td>Banana</td>
          <td>£0.50</td>
        </tr>
        <tr>
          <td>Orange</td>
          <td>£0.75</td>
        </tr>
      </tbody>
      <tfoot style="background-color: #f9f9f9; font-weight: bold;">
        <tr>
          <td>Total</td>
          <td>£2.25</td>
        </tr>
      </tfoot>
    </table>
            

Table Accessibility

While semantic HTML tags like <th>, <tr>, and <td> represent individual header and data cells in a table, ARIA roles and attributes can enhance accessibility and provide additional context for assistive technologies. The following provide an overview of the key tags and attributes for accessible HTML5 tables: