Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

span — column count for col and colgroup

The span attribute specifies how many columns a col or colgroup represents. It accepts integers from 1 through 1000 and does not merge table cells.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<col span="n">
<colgroup span="n"></colgroup>
<!-- n: integer from 1 to 1000 -->

The span attribute on col specifies how many consecutive columns that element represents. On a colgroup without col elements, it specifies the number of columns in the group. It is neither the span element nor a global attribute.

A conforming value is an integer from 1 through 1000. In the table model, a missing attribute, a parsing failure, or zero means 1; values above 1000 are capped at 1000. Error recovery does not make those values conforming.

A colgroup with a span attribute must be empty. To put col elements inside it, omit span on the colgroup; the group's extent then follows its col elements. The span attribute does not merge cells: use colspan on td or th to make a cell span columns.

In the example, the first group covers two columns, and the col in the second represents the next two. The DOM property span reflects the attribute through HTMLTableColElement. The output 2 2 4 shows both values and four separate cells.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Column span</title>
<table>
  <caption>Four columns</caption>
  <colgroup id="group" span="2"></colgroup>
  <colgroup><col id="columns" span="2"></colgroup>
  <tbody>
    <tr><td>A</td><td>B</td><td>C</td><td>D</td></tr>
  </tbody>
</table>
<pre id="result"></pre>
<script>
  const group = document.getElementById('group');
  const columns = document.getElementById('columns');
  const cellCount = document.querySelector('tr').cells.length;
  document.getElementById('result').textContent =
    `${group.span} ${columns.span} ${cellCount}`;
</script>
</html>

Result

2 2 4

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources