👀 Live Preview
Table with a cell spanning two columns in the first row:
| Row 1, Cell 1 | Row 1, Cell 2 | Row 1, Cell 3 and 4 (colspan=2) | |
| Row 2, Cell 1 | Row 2, Cell 2 | Row 2, Cell 3 | Row 2, Cell 4 |

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.
Span 2, 3, or more.
Where colspan applies.
1 or greater.
Horizontal vs vertical.
DOM property.
Headers and groups.
colspanThe 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 merges cells horizontally across columns. rowspan merges cells vertically across rows. You can combine both on the same cell when needed.
Add colspan to a <td> or <th> element with a positive integer:
<td colspan="2">Spans two columns</td>
<th colspan="3">Section header</th><td> and <th> elements inside a <table>.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.<tr>
<th colspan="3">Quarterly Report</th>
</tr>
<tr>
<td>Jan</td>
<td>Feb</td>
<td>Mar</td>
</tr>| Use Case | colspan Value | Notes |
|---|---|---|
| Single cell | colspan="1" | Default behavior |
| Merge two columns | colspan="2" | Most common merge |
| Full-width header row | colspan="N" | N = total columns |
| JavaScript DOM | cell.colSpan = 3 | Capital S in colSpan |
| Applicable elements | td, th | Inside table only |
| Vertical merge | rowspan | Separate attribute |
| Element | Supported? | Notes |
|---|---|---|
<td> | Yes | Data cells |
<th> | Yes | Header cells; pair with scope |
<table> | No | Set colspan on cells, not the table |
<textarea> | No | Use cols for textarea width |
<div> | No | Table-only attribute |
Basic colspan merge and dynamic JavaScript colSpan adjustment.
Table with a cell spanning two columns in the first row:
| Row 1, Cell 1 | Row 1, Cell 2 | Row 1, Cell 3 and 4 (colspan=2) | |
| Row 2, Cell 1 | Row 2, Cell 2 | Row 2, Cell 3 | Row 2, Cell 4 |
Let’s take a look at a simple example to understand how the colspan attribute works:
<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>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.
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:
<td id="dynamicCell">This cell will span 3 columns</td>
<script>
// Dynamically set colspan for a table cell
document.getElementById("dynamicCell").colSpan = 3;
</script>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.
scope="colgroup" or scope="col" where appropriate.<caption> to describe the table purpose.Define how many column slots one td or th should occupy.
Adjacent column slots merge into a single wider cell.
Other rows must account for the total column count.
Headers and grouped data span columns cleanly across the table.
The colspan attribute is supported in all modern browsers on <td> and <th> elements.
All major browsers render colspan consistently on table cells.
Bottom line: Use colspan confidently for table layouts; test complex merges across browsers.
scope on header cells for accessibilitycolspan with textarea colscolspan attribute thoughtfully to enhance the visual structure of your tables without sacrificing clarity.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.
colspanBookmark these before merging your next table cells.
Span columns.
PurposeCell elements.
ScopeColumn count.
ValuesCapital S in JS.
ScriptDon’t over-merge.
A11y<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.cellElement.colSpan = 3 (capital S) or setAttribute("colspan", "3").Practice the colspan attribute with merged cells and JavaScript examples in the Try It editor.
2 people found this page helpful