Back to HTML Notes
Topic #146

colspan

Learn how to use the colspan attribute to merge table cells horizontally, creating headers or data rows that span multiple columns.

What it is

The colspan attribute is used within a <td> (table data) or <th> (table header) element. It specifies how many columns that cell should occupy. By default, every cell occupies exactly one column. When you set colspan="n", the cell stretches across n columns, effectively merging them into a single visual unit.

This is essential for creating complex table layouts where certain information applies to a group of columns rather than individual ones. The related attribute rowspan performs a similar function but merges cells vertically.

Why it matters

  • Logical Grouping: Allows you to create super-headers that categorize sub-columns (e.g., "Q1" spanning "Jan", "Feb", "Mar").
  • Accessibility: Properly structured tables with spans help screen readers understand the relationship between headers and data.
  • Layout Efficiency: Reduces the need for nested tables or excessive CSS hacks to achieve merged-cell appearances.
  • Visual Clarity: Prevents repetitive labeling in large datasets by grouping common attributes.

Syntax or steps

  1. Identify the row containing the cell you want to expand.
  2. Add the colspan attribute to the <td> or <th> tag.
  3. Set the value to an integer representing the number of columns to span.
  4. Ensure the total number of columns in that specific row matches the table's overall column count when accounting for spans.

Example

<table border="1">
  <thead>
    <tr>
      <th rowspan="2">Name</th>
      <th colspan="3">Scores</th>
    </tr>
    <tr>
      <th>Math</th>
      <th>Science</th>
      <th>History</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Alice</td>
      <td>95</td>
      <td>88</td>
      <td>92</td>
    </tr>
    <tr>
      <td>Bob</td>
      <td colspan="3">Absent</td>
    </tr>
  </tbody>
</table>

In this example, the first header row uses colspan="3" on the "Scores" header to cover Math, Science, and History. In the second data row, Bob's entry uses colspan="3" on the "Absent" cell, meaning no separate cells are needed for his three subjects because they are merged into one wide cell.

Common mistakes

  • Mismatched Column Counts: If a row has fewer cells than the total column width (even after accounting for spans), the browser may render empty gaps or misalign subsequent rows. Always ensure the sum of colspan values plus regular cells equals the total number of columns defined by the widest row.
  • Using colspan on Non-Cell Elements: This attribute only works on <td> and <th>. Applying it to <tr> or <table> will have no effect.
  • Confusing colspan with width: colspan changes the logical structure of the grid; CSS width only changes pixel dimensions. Do not use colspan just to make a cell look wider if it doesn't actually cover other columns.

When to use it

Scenario Use colspan Alternative
Header groups multiple sub-columns Yes N/A
A single value applies to all columns in a row Yes Repeat the value in each cell (verbose)
Just making a cell visually wider No CSS width or min-width

Practice

Guided Exercise: Create a table with 4 columns. Make the first row contain a single header that spans all 4 columns. Fill the remaining rows with standard data.

Challenge: Modify your previous table so that the last row contains a footer note that spans the first two columns, while the last two columns remain empty or contain different data. Check if the table renders correctly without broken borders.

Quick check

Question: If a table has 5 columns total, and a row contains one cell with colspan="2" and three regular cells, does this row fit the table structure?

Answer: Yes. The calculation is 2 (from colspan) + 1 + 1 + 1 = 5 columns, which matches the table width.

Summary

The colspan attribute allows horizontal merging of table cells, enabling clearer data grouping and efficient layout structures. Correct usage requires careful attention to the total column count per row to maintain table integrity.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

colspan – FAQs

Quick answers about learning colspan in HTML.

This free note from Coding Now Tech Institute explains colspan in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including colspan, is 100% free with no signup required.
With focused practice, most students grasp colspan in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now