HTML Tutorial

Tables

Displaying tabular data with rows and columns

When to use Tables?

Wait! Tables should only be used for Data (like a schedule, a price list, or a set of scores). In the old days, developers used tables for page layout, but that is a major "Don't." Today, we use Flexbox or Grid for layout.
html
<table>
  <tr>
    <th>Language</th>
    <th>Release Year</th>
  </tr>
  <tr>
    <td>JavaScript</td>
    <td>1995</td>
  </tr>
</table>

The Table Hierarchy

  1. <table>: The main wrapper.
  2. <thead>: The header section (optional but recommended).
  3. <tbody>: The body section where the main data lives.
  4. <tr>: Table Row.
  5. <th>: Table Header (Bold and centered by default).
  6. <td>: Table Data (A single cell).

Merging Cells

  • colspan: Merges columns horizontally.
  • rowspan: Merges rows vertically.
html
<td colspan="2">Double Width Cell</td>

Summary

TermDescription
trTable Row
thTable Header (Title of the column)
tdTable Data (The actual value)
borderThe line around the cells (Usually set in CSS)
captionA title for the whole table
Best ForSchedules, spreadsheets, and statistical data
Logic"Columnar data presentation"
SafetyKeep tables simple to ensure they work on mobile screens
ModernUse CSS display: block to stack tables on small screens
StandardUse <thead> and <tbody> for better accessibility
IdentityThe "Spreadsheet" view of the web