CKTIS / HTML
colspan: make one table cell cover several columns
Reason about table-grid positions instead of counting only DOM cells.
Version: HTML Living Standard
Syntax
<td colspan="2">Two columns</td>On a data or header cell, colspan specifies how many table columns the cell covers. Valid values are integers from 1 to 1000; omission gives a single-column cell. Zero does not mean “all remaining columns”, unlike the special zero rule for row spanning. The resulting cells must not overlap in the table model.
Spanning does not create extra cell elements. Consequently cellIndex describes an element’s position in its row’s cell collection, which can differ from its visual grid column. Keep header associations meaningful for assistive technology and use real tabular data rather than a table as a general page-layout device.
The fixture has three explicitly sized columns. Its first row contains only two cells because the first covers two columns; the second row has three individual cells. Geometry checks confirm the combined width and alignment of the final column. The example therefore tests the grid effect instead of merely reading the attribute back.
Examples
<!doctype html><meta charset="utf-8">
<style>table { width:300px; table-layout:fixed; border-spacing:0; } td { padding:0; }</style>
<table><colgroup><col style="width:100px"><col style="width:100px"><col style="width:100px"></colgroup>
<tbody><tr id="joined"><td id="span" colspan="2">Two columns</td><td id="tail">Third</td></tr>
<tr><td id="a">A</td><td id="b">B</td><td id="c">C</td></tr></tbody></table><output id="result"></output>
<script>
const box = id => document.getElementById(id).getBoundingClientRect();
document.querySelector('#result').textContent = 'rowCells=' + document.querySelector('#joined').cells.length + '; coversTwo=' + (Math.abs(box('span').width - box('a').width - box('b').width) < 1) + '; thirdAligned=' + (Math.abs(box('tail').left - box('c').left) < 1);
</script>Result
rowCells=2; coversTwo=true; thirdAligned=true
A small tolerance is used for geometry measurements; the assertion is about alignment, not a universal pixel size.