I have a matrix of product data.
My goal is to:
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:
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.
HTML <caption> Tag.
HTML <caption> Tag. The caption tag is used to specify the caption of a table. This tag will be inserted just after the <table> tag.
The caption for a table is a table identifier and acts like a title or heading for the table. The caption element is the appropriate markup for such text and it ensures that the table identifier remains associated with the table, including visually (by default).
<caption>: The Table Caption element. The <caption> HTML element specifies the caption (or title) of a table.
HTML5 defines (in 4.9.2 The caption
element and in 4.9.12 Processing model) that the caption
element
table
", and I would assume that an "Add Product" button should not be part of a table’s title, based on my understanding of a title (or heading or caption).
If my assumption is true, it would be an error to use this button
in caption
. The fact that the content model allows this does not mean that it’s necessarily valid.
If something’s valid is not only based on syntactic requirements (like "Content model" and "Contexts in which this element can be used"), but also on semantic requirements. You can’t denote the content model in a way that would make semantic errors impossible. And while this specific button might be inappropriate for the caption
, there might be other cases where a button
could be appropriate.
If it is not "correct", is it acceptable?
That depends on what’s "acceptable" to you. For example, users of common Web browsers in default configurations will most likely not be affected by this error in any way. But it’s conceivable (but maybe not likely) that users with "uncommon" setups (e.g., with specific accessibility tools) might be affected by this somehow.
If having the button
in the caption
makes your implementation easier, I wouldn’t worry too much about this and keep it like that, unless your tests (or feedback you get) indicate that it could be problematic.
I wouldn’t include the "Add Product" button in the table
at all, but either before or after the table
element.
<form>
<button>…</button>
</form>
<table>
<caption>…</caption>
</table>
It depends on the context if including the table
and the button
in a sectioning content element (e.g., section
, or maybe even article
) is appropriate.
This question is actually one of accessibility in which case it isn't semantically correct or acceptable. Captions are used for screen readers to give information around a table and using it to add a button would make this table inaccessible. Tables are already difficult to make accessible but adding buttons exacerbates the problem. If you can't avoid buttons:
In the original example, the title and button would not actually be a part of the table, even if visually you styled them that way. Instead "Product Catalog" would be a heading with an appropriate heading level and the button would just be visually to the right and immediately follow the heading in the HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With