Aral Code

Lists & Tables Lesson 23 of 42

HTML Tables Part 2 — colspan, rowspan, thead, tbody and tfoot

Merge table cells with colspan and rowspan, group rows with thead, tbody and tfoot, and build a real class schedule and grade sheet.

Simple tables are just rows of equal cells. But real-world tables — like a school schedule where "Lunch Break" stretches across all days, or a report card where one heading covers two quarters — need cells that cover more than one column or row. In this lesson you'll learn how to merge cells and organize big tables.

colspan — a cell that spans columns

colspan="3" makes one cell as wide as three columns. It's like merging cells horizontally in Excel or Google Sheets.

Example Lunch break across all days
<table border="1" cellpadding="8">
  <tr>
    <th>Time</th>
    <th>Mon</th>
    <th>Tue</th>
    <th>Wed</th>
  </tr>
  <tr>
    <td>10:00 – 11:00</td>
    <td>Math</td>
    <td>English</td>
    <td>Science</td>
  </tr>
  <tr>
    <td>11:00 – 12:00</td>
    <td colspan="3" style="text-align:center;">🍛 LUNCH BREAK</td>
  </tr>
  <tr>
    <td>12:00 – 1:00</td>
    <td>TLE</td>
    <td>MAPEH</td>
    <td>AP</td>
  </tr>
</table>

Notice the lunch row has only two <td> elements: the time, and one cell with colspan="3". Together they still add up to 4 columns. That's the rule to remember: the spans in every row must add up to the same total.

rowspan — a cell that spans rows

rowspan="2" makes a cell as tall as two rows. The cell "reaches down" into the next row, so in that next row you leave out one cell.

Example A teacher who handles two periods
<table border="1" cellpadding="8">
  <tr>
    <th>Period</th>
    <th>Subject</th>
    <th>Teacher</th>
  </tr>
  <tr>
    <td>1st</td>
    <td>Computer Programming</td>
    <td rowspan="2">Ma'am Garcia</td>
  </tr>
  <tr>
    <td>2nd</td>
    <td>Computer Programming (Lab)</td>
  </tr>
  <tr>
    <td>3rd</td>
    <td>Earth Science</td>
    <td>Sir Mendoza</td>
  </tr>
</table>

The second row has only two cells, because the "Ma'am Garcia" cell from the row above already fills the third column.

Using both together

Report cards often need headings that span two levels. Here "Quarter" covers two columns, while "Subject" and "Final" each cover two rows:

Example A two-level header
<table border="1" cellpadding="8">
  <tr>
    <th rowspan="2">Subject</th>
    <th colspan="2">Quarter</th>
    <th rowspan="2">Final</th>
  </tr>
  <tr>
    <th>1</th>
    <th>2</th>
  </tr>
  <tr>
    <td>Filipino</td>
    <td>89</td>
    <td>92</td>
    <td>91</td>
  </tr>
  <tr>
    <td>Science</td>
    <td>85</td>
    <td>88</td>
    <td>87</td>
  </tr>
</table>

thead, tbody and tfoot

Big tables are easier to manage when you group the rows into three sections:

  • <thead> — the header rows
  • <tbody> — the main data rows
  • <tfoot> — summary rows, like totals

They don't change the look by default, but they give structure. They let you style each part separately with CSS, they help screen readers, and when a long table is printed on several pages, browsers can repeat the header on each page.

Example A canteen sales report
<style>
  table { border-collapse: collapse; font-family: Arial, sans-serif; min-width: 320px; }
  th, td { border: 1px solid #bbb; padding: 8px 12px; text-align: left; }
  thead { background: #0f5c4d; color: white; }
  tfoot { background: #fdf3dc; font-weight: bold; }
  td:last-child, th:last-child { text-align: right; }
</style>
<table>
  <caption>Canteen Sales – Monday</caption>
  <thead>
    <tr><th>Item</th><th>Qty</th><th>Sales</th></tr>
  </thead>
  <tbody>
    <tr><td>Turon</td><td>45</td><td>₱450</td></tr>
    <tr><td>Banana Cue</td><td>38</td><td>₱570</td></tr>
    <tr><td>Palabok</td><td>20</td><td>₱800</td></tr>
  </tbody>
  <tfoot>
    <tr><td colspan="2">Total</td><td>₱1,820</td></tr>
  </tfoot>
</table>

Zebra stripes for readability

In long tables, it's easy to lose your place when reading across a row. Alternating background colors ("zebra stripes") help a lot. CSS can do it with one rule:

<style>
  tbody tr:nth-child(even) { background-color: #f3f1ea; }
</style>

Add that line to the <style> in the canteen example above and add a few more rows to see the stripes.

Common mistakes

  • Putting colspan on <tr> instead of on <td> or <th> — spans only work on cells.
  • Forgetting the quotes: write colspan="3".
  • Using merged cells just for decoration. Every merge should make the data easier to understand.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which attribute makes a cell cover 3 columns?
2. A table has 4 columns. One row has a cell with colspan="2". How many cells should that row have in total?
3. You put rowspan="2" on a cell. Where do you remove a cell?
4. Which section is best for a "Total" row?

By Sir Reyel · Last updated 2026-12-04