Search code examples
htmlsemantics

What is the best way to add a title to a table?


Which element should I use for titles of tables, <caption> or <th>?

For example, I want to create this table :

enter image description here

What is better,

<tr><th colspan="2">Discographie</th></tr>

or

<caption style="font-weight: bold">Discographie</caption>

?


Solution

  • th corresponds to the title of the column, so in that case : "Album" and "Année"

    W3C : "The th element represents a header cell in a table." https://www.w3.org/WAI/tutorials/tables/one-header/

    caption corresponds to the title / description of the whole table, so yes, you'd better use caption for "Discographie".

    W3C : "The caption element represents the title of the table that is its parent." https://www.w3.org/WAI/tutorials/tables/caption-summary/

    Plus, it is easier to maintain as your table grows