I have a matrix of product data.
My goal is to:
- display the matrix using HTML5 in a tabular format
- display a left-aligned "title" above the data that describes it
- display a right-aligned "add" button, vertically aligned with the title, which facilitates the addition of rows to the table
- display a header row to describe each column of data
- do all of the above in a semantically "correct" way, if possible
So, the end result would look similar to this:
--------------------------------------------------------
|Product Catalog [+Add Product]|
--------------------------------------------------------
| Title | Cost | Weight | and | so | on | ... |
--------------------------------------------------------
| Thing1| $5.99| 3 oz | ... | ...| ...| ... |
| Thing2| $2.99| 2 oz | ... | ...| ...| ... |
--------------------------------------------------------
My current code:
<table>
<caption>Product Catalog <button style="float: right; display: block;">+Add Product</button></caption>
<thead>
<tr>
<th>Title</th>
<th>Cost</th>
<th>Weight</th>
<th>and</th>
<th>so</th>
<th>on</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>Thing1</td>
<td>$5.99</td>
<td>3 oz</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>Thing2</td>
<td>$2.99</td>
<td>2 oz</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
According to the HTML5 spec, the caption element can contain any flow content, which includes buttons.
From the spec:
Content Model: Flow content, but with no descendant table elements
However, the same spec defines the caption element as:
The caption element represents the title of the table that is its parent, if it has a parent and that is a table element.
Based on that description, it seems that a button does not belong inside of a caption, because it lends nothing to the title of the table, even though it is technically allowed.
My questions are: Is it semantically "correct" to include a button inside of a table caption? If it is not "correct", is it acceptable? Is there a more-semantically-correct way of achieving the desired result?
I found plenty of questions/answers dealing with table semantics, but most did not deal with buttons on the table.
This question/answer asks about buttons in a table, but it is specific to buttons inside of a "cell", not the caption.
HTML5 Specs:
- caption element: https://www.w3.org/TR/html5/tabular-data.html#the-caption-element
- flow-content: https://www.w3.org/TR/html5/dom.html#flow-content-1
EDIT:
This question/answer seems to be of the same nature as my question, but he is asking about putting his button(s) inside of a <th>
, not the table caption.
One solution I've considered is: don't use a caption at all. Just put a <h1>
or similar header tag before the table tag. The only problem with this is that the title will no longer be relative to the table.... or will it? Actually, that might be the answer. If I wrap the <h1>
and the <table>
in a <section>
or perhaps a <div>
then that might work?
I'm looking for the "most-correct" way.