Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML element: caption

The table-caption element gives a table a title or concise explanation, helping readers understand the data’s scope before its cells.

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

Syntax

<table><caption>Table title</caption>…</table>

When caption is present, it must be the first element child of table; text or comment nodes may precede it, and a table cannot have more than one caption.

In the DOM, table.caption references the associated caption; createCaption() creates or returns it, while deleteCaption() removes only that element.

A caption does not replace cell headers: th with scope or a headers–id association still describes relationships in the grid.

CSS caption-side can change visual placement. The example reads firstElementChild, the rows and tBodies collections, and the header count.

Examples

<!doctype html>
<meta charset="utf-8">
<table id="sales">
  <caption>Quarterly sales</caption>
  <thead><tr><th scope="col">Quarter</th><th scope="col">Units</th></tr></thead>
  <tbody><tr><th scope="row">Q1</th><td>42</td></tr></tbody>
</table>
<output id="result"></output>
<script>
const sales = document.querySelector("#sales");
document.querySelector("#result").textContent = `caption=${sales.caption.textContent}; first=${sales.firstElementChild.tagName}; rows=${sales.rows.length}; bodies=${sales.tBodies.length}; headers=${sales.querySelectorAll("th").length}`;
</script>

Result

caption=Quarterly sales; first=CAPTION; rows=2; bodies=1; headers=3

The result verifies one caption as the first element and a two-row structure; it does not simulate screen-reader table announcements.

Sources