Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Back to HTML
Understanding HTML Tables

Understanding HTML Tables

HTMLBy Muhammad Fareed
htmlunderstandingtables

Short answer: use an HTML table to display data that has rows and columns, such as prices, schedules or comparison charts. A table is built from <table>, rows (<tr>), header cells (<th>) and data cells (<td>). Do not use tables for page layout; use CSS for that.

A basic table

<table>
  <caption>Monthly plans</caption>
  <thead>
    <tr>
      <th scope="col">Plan</th>
      <th scope="col">Price</th>
      <th scope="col">Users</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Free</th>
      <td>$0</td>
      <td>1</td>
    </tr>
    <tr>
      <th scope="row">Team</th>
      <td>$12</td>
      <td>10</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="3">Prices exclude tax</td>
    </tr>
  </tfoot>
</table>

The table elements

ElementPurpose
<table>The whole table
<caption>A title that describes the table
<thead>, <tbody>, <tfoot>Group header, body and footer rows
<tr>A table row
<th>A header cell (bold and centered by default)
<td>A data cell
<colgroup>, <col>Style or group columns

Spanning cells

<tr>
  <th colspan="2">Contact</th>      <!-- spans two columns -->
</tr>
<tr>
  <td rowspan="2">Office</td>      <!-- spans two rows -->
  <td>Pune</td>
</tr>
<tr>
  <td>Delhi</td>
</tr>

Use spans sparingly. Complex merged cells are hard for screen readers and for you to maintain.

Making tables accessible

  • Use <th> for headers, not styled <td>.
  • Add scope="col" or scope="row" so assistive technology links cells to their headers.
  • Add a <caption> that says what the table is about.
  • For tables with several header levels, use id on headers and headers on cells.
  • Keep structure simple: one header row and regular rows.

More on this in HTML accessibility best practices.

Styling with CSS

table {
  border-collapse: collapse;
  width: 100%;
}
th, td {
  border: 1px solid #ddd;
  padding: 0.5rem 0.75rem;
  text-align: left;
}
thead th {
  background: #f3f4f6;
}
tbody tr:nth-child(even) {
  background: #fafafa;
}

border-collapse: collapse removes the double borders between cells. The old HTML attributes border, cellpadding and align are obsolete; use CSS.

Responsive tables

Wide tables overflow on phones. A simple fix is to put the table in a scrollable wrapper:

<div class="table-wrap">
  <table>...</table>
</div>
.table-wrap {
  overflow-x: auto;
}

For more control, see responsive design with media queries.

Tables vs CSS layout

Use a table forUse CSS (Flexbox or Grid) for
Data in rows and columnsPage structure and columns of content
Comparison chartsCard grids and navigation bars
Schedules and reportsAligning form fields and buttons

See CSS Grid and Flexbox for layout.

Common mistakes

  • Using tables for layout.
  • Skipping header cells or the caption.
  • Rows with a different number of cells than the others.
  • Putting block elements outside <td> or <th> inside a table.
  • Forgetting that a table without <tbody> still gets one added by browsers, which can affect CSS selectors.

New to markup? Read what HTML is, and structure the rest of your page with semantic elements.

FAQs

What is the difference between th and td?

th is a header cell that labels a row or column. td holds data.

Do I need thead and tbody?

They are optional but recommended: they add meaning, help styling, and let browsers repeat headers when printing long tables.

How do I center text in a table cell?

Use CSS: text-align: center; on the cell.

How do I make table rows clickable?

Put a link or button in a cell. Avoid making whole rows clickable with JavaScript only, because keyboard users cannot reach them.