HTML colspan Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 2 Examples
Tables & Layout

Introduction

In HTML, the colspan attribute is used within a table to define the number of columns a cell should span horizontally. This attribute is particularly useful when you want to create table structures with merged or extended cells, providing a flexible way to organize and present tabular data.

What You’ll Learn

01

Merge Columns

Span 2, 3, or more.

02

td and th

Where colspan applies.

03

Integer Values

1 or greater.

04

vs rowspan

Horizontal vs vertical.

05

colSpan

DOM property.

06

Table Layout

Headers and groups.

Purpose of colspan

The primary purpose of the colspan attribute is to control the visual layout of tables by allowing a single cell to cover multiple columns. This can be beneficial for creating more visually appealing and readable tables.

💡
colspan vs rowspan

colspan merges cells horizontally across columns. rowspan merges cells vertically across rows. You can combine both on the same cell when needed.

📝 Syntax

Add colspan to a <td> or <th> element with a positive integer:

colspan.html
<td colspan="2">Spans two columns</td>
<th colspan="3">Section header</th>

Syntax Rules

  • Valid on <td> and <th> elements inside a <table>.
  • Value must be a positive integer ≥ 1.
  • A spanned cell occupies multiple column slots; omit extra cells in that row.
  • Each row’s effective column count must stay consistent across the table.

💎 Values

The colspan attribute accepts a numeric value, specifying the number of columns a cell should span. The value must be a positive integer greater than or equal to 1. If the value is set to 1, the cell spans only one column.

  • colspan="1" — Default; single column (no merge).
  • colspan="2" — Cell covers two adjacent columns.
  • colspan="3" — Cell covers three columns, common for section headers.
colspan-values.html
<tr>
  <th colspan="3">Quarterly Report</th>
</tr>
<tr>
  <td>Jan</td>
  <td>Feb</td>
  <td>Mar</td>
</tr>

⚡ Quick Reference

Use Casecolspan ValueNotes
Single cellcolspan="1"Default behavior
Merge two columnscolspan="2"Most common merge
Full-width header rowcolspan="N"N = total columns
JavaScript DOMcell.colSpan = 3Capital S in colSpan
Applicable elementstd, thInside table only
Vertical mergerowspanSeparate attribute

Applicable Elements

ElementSupported?Notes
<td>YesData cells
<th>YesHeader cells; pair with scope
<table>NoSet colspan on cells, not the table
<textarea>NoUse cols for textarea width
<div>NoTable-only attribute

Examples Gallery

Basic colspan merge and dynamic JavaScript colSpan adjustment.

👀 Live Preview

Table with a cell spanning two columns in the first row:

Row 1, Cell 1Row 1, Cell 2Row 1, Cell 3 and 4 (colspan=2)
Row 2, Cell 1Row 2, Cell 2Row 2, Cell 3Row 2, Cell 4

Example

Let’s take a look at a simple example to understand how the colspan attribute works:

colspan.html
<table border="1">
  <tr>
    <td>Row 1, Cell 1</td>
    <td>Row 1, Cell 2</td>
    <td colspan="2">Row 1, Cell 3 and 4 (colspan=2)</td>
  </tr>
  <tr>
    <td>Row 2, Cell 1</td>
    <td>Row 2, Cell 2</td>
    <td>Row 2, Cell 3</td>
    <td>Row 2, Cell 4</td>
  </tr>
</table>
Try It Yourself

How It Works

In this example, the third cell in the first row has a colspan attribute set to 2, indicating that it should span two columns. As a result, this cell covers both the third and fourth columns.

Dynamic Values with JavaScript

You can dynamically set the colspan attribute using JavaScript to adjust the layout of your tables based on user interactions or specific conditions. Here’s a brief example:

dynamic-colspan.html
<td id="dynamicCell">This cell will span 3 columns</td>

<script>
  // Dynamically set colspan for a table cell
  document.getElementById("dynamicCell").colSpan = 3;
</script>
Try It Yourself

How It Works

In this script, the colSpan property is set to 3 for a table cell with the id dynamicCell. This dynamic adjustment can be helpful when you need to respond to changes in your data or user actions. Note the capital S in the DOM property name colSpan.

♿ Accessibility

  • Use proper headers — Pair merged header cells with scope="colgroup" or scope="col" where appropriate.
  • Keep structure logical — Screen readers rely on consistent row and column relationships.
  • Do not over-merge — Excessive colspan can confuse assistive technology and sighted users alike.
  • Caption your table — Use <caption> to describe the table purpose.
  • Test with screen readers — Verify that merged cells still announce correct header associations.

🧠 How colspan Works

1

Author sets colspan on a cell

Define how many column slots one td or th should occupy.

Markup
2

Browser adjusts the grid

Adjacent column slots merge into a single wider cell.

Layout
3

Remaining cells align

Other rows must account for the total column count.

Structure
=

Merged table layout displayed

Headers and grouped data span columns cleanly across the table.

Browser Support

The colspan attribute is supported in all modern browsers on <td> and <th> elements.

HTML5 · Fully supported

Universal table cell merging

All major browsers render colspan consistently on table cells.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
colspan attribute 99% supported

Bottom line: Use colspan confidently for table layouts; test complex merges across browsers.

💡 Best Practices

✅ Do

  • Use colspan thoughtfully to enhance table structure
  • Keep column counts consistent across every row
  • Use colspan on header rows for section titles
  • Test tables across different browsers and screen sizes
  • Combine with scope on header cells for accessibility

❌ Don’t

  • Overuse colspan until the table becomes hard to read
  • Confuse colspan with textarea cols
  • Leave mismatched column counts in a row
  • Rely on merged cells alone to convey data relationships
  • Forget to validate table markup with complex merges
  • Use the colspan attribute thoughtfully to enhance the visual structure of your tables without sacrificing clarity.
  • Avoid excessive use of colspan as it may make your table more complex and harder to understand.
  • Always test your tables across different browsers to ensure consistent rendering.

Conclusion

The colspan attribute is a valuable tool for designing tables in HTML, allowing developers to create more sophisticated and visually appealing layouts. By mastering the use of colspan, you can efficiently organize and present tabular data on your web pages.

Start with simple two-column merges, then build up to full-width headers. Always verify that each row’s effective column total stays correct when cells span multiple columns.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about colspan

Bookmark these before merging your next table cells.

5
Core concepts
📄 02

td and th

Cell elements.

Scope
🔢 03

Integer ≥ 1

Column count.

Values
⚙️ 04

colSpan

Capital S in JS.

Script
05

Keep It Clear

Don’t over-merge.

A11y

❓ Frequently Asked Questions

It makes a table cell span multiple columns horizontally, merging adjacent column slots into one cell.
<td> and <th> inside a <table>. Other elements ignore colspan.
colspan merges table cells. cols sets textarea visible width in character columns.
colspan spans columns horizontally. rowspan spans rows vertically.
Use cellElement.colSpan = 3 (capital S) or setAttribute("colspan", "3").
Yes, on table cells in all modern browsers. Test complex table layouts for consistent rendering.

Design smarter HTML tables

Practice the colspan attribute with merged cells and JavaScript examples in the Try It editor.

Try colspan example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

2 people found this page helpful