Be able to insert a table including table header, table rows, table data

Resources | Subject Notes | Information Communication Technology ICT

21 Website Authoring: Inserting Tables

Objective

Be able to insert a table including table header, table rows, table data.

Creating a Table

To create a table in HTML, we use the

tag. A table is structured into rows and columns. We define the structure using for the table header, for the table body, for table rows, and
for table data cells.

HTML Structure for a Table

Here's the basic structure:

<table>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
    </tr>
    <tr>
      <td>Data 3</td>
      <td>Data 4</td>
    </tr>
  </tbody>
</table>

Example Table

Let's create a simple table to display student names and grades:

<table> <thead> <tr> <th>Student Name</th> <th>Grade</th> </tr> </thead> <tbody> <tr> <td>Alice</td> <td>A</td> </tr> <tr> <td>Bob</td> <td>B</td> </tr> <tr> <td>Charlie</td> <td>C</td> </tr> </tbody> </table>

Explanation of Tags

  • <table>: Defines the HTML table.
  • <thead>: Represents the table header. Typically contains the column names.
  • <tbody>: Represents the table body, containing the actual data.
  • <tr>: Defines a table row.
  • <th>: Defines a table header cell. Used in the <thead>.
  • <td>: Defines a table data cell. Used in the <tbody>.

Adding More Data

You can add more rows and columns to the table by repeating the <tr> and <td> tags as needed. Ensure that the number of <td> elements in each row matches the number of <th> elements in the first row of the <thead>.

Example with more rows

<table> <thead> <tr> <th>Name</th> <th>Age</th> <th>City</th> </tr> </thead> <tbody> <tr> <td>David</td> <td>25</td> <td>London</td> </tr> <tr> <td>Eve</td> <td>30</td> <td>Paris</td> </tr> <tr> <td>Frank</td> <td>40</td> <td>Tokyo</td> </tr> </tbody> </table>

Figure

Suggested diagram: A simple table with header row and data rows.

<table> <thead> <tr> <th>Column 1</th> <th>Column 2</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> </tr> </tbody> </table>