I recently had a discussion with another developer who was using <table> for data that was not tabular.
<table><tr><td> Server: </td><td> Development </td></tr></table>
I pointed out that tabular data has more than one dimension, and use of tables for styling/presentation is discouraged. He responded that just because the table was one row does not mean that the data is non-tabular.
It seems to me that if "tabular data" has more than one dimension it would require at least two rows and two columns. However, he pointed out that <table><tr><td></td></tr></table> (as well as <table></table>) both validate, and that if he is using <table> incorrectly, the W3C is unclear and they did a poor job with their validator.
I think that <table> is supposed to be used to represent data that is tabular whether or not the resulting structure is tabular as their can be caveats. In the above example, there will only ever be one server, but you can potentially have zero or more rows of multi-column data that is perhaps filtered on a date range.
My question is who is right here? If I am misunderstanding the spec, then how am I misunderstanding? If you have data that is tabular but not enough filtered data to print out multiple rows (i.e. if it is filtered on a small date range) is it incorrect to print only the column headers? Should you print a "no results found" full colspan row as well? If data in <table> must be multi-dimensional, why does a single-column and/or single-row table validate?
I agree with the others, validity of markup is not equivalent to semantics of the markup.
Tabular data should have a column headers to describe the data it holds. So the most minimal table would be, (1 column with 1 row of data, that column should have a heading):
<table>
<thead>
<tr>
<th>My Heading</th>
</tr>
</thead>
<tbody>
<tr>
<td>my value</td>
</tr>
</tbody>
</table>
If you want to represent "no results found" it should be displayed outside of the table. Because no results foundis not data. No results should display nothing in the table, or better yet, hide the table and display your no results found message.
It seems to me that if "tabular data" has more than one dimension it would require at least two rows and two columns. However, he pointed out that
<table><tr><td></td></tr></table>(as well as<table></table>) both validate, and that if he is using<table>incorrectly, the W3C is unclear and they did a poor job with their validator.
Validity of markup has absolutely nothing to do with semantics. <p> by itself is valid HTML, even though empty paragraphs make no sense in writing. In the same way, the following three tables are all valid, even though they contain no actual data:
<table></table>
<table>
<tbody>
</table>
<table>
<thead><tr><th>
<tbody>
<tfoot><tr><th>
</table>
As you have observed, though, a <tr> element with no children is invalid:
<table>
<tr>
</table>
It's not clear to me why that is the case, as I cannot find any rules in the current HTML 5.0 CR that state that a <tr> element must have at least one child which is either a <td> or a <th> element (it simply says it may contain zero or more of either kind).
Anyway, if a table contains only one row, so be it; that simply means there's only one record to be listed in the table. But tabular data semantics is not a question of whether this table has only one row, but whether this structure is suited for any number of data records expressed in a two-dimensional format. That's what constitutes tabular data.
In your example, will there potentially be more than one entry for "Server" (which, incidentally, if it were a real table heading it should be a <th> rather than a <td> and it should not contain a colon)? If not, and all you want to do is to tell the user which server they're on for instance, then a table is not the most appropriate element to use to present this information. You should possibly use a heading or a label, and some other textual container for the value "Development".
If you have data that is tabular but not enough filtered data to print out multiple rows (i.e. if it is filtered on a small date range) is it incorrect to print only the column headers? Should you print a "no results found" full colspan row as well?
Whether you choose to display a row that says "no results found" is up to you; either way should be acceptable. In this case, all you have is a table that is designed to contain tabular data but for some reason does not have any data to present.
If data in
<table>must be multi-dimensional, why does a single-column and/or single-row table validate?
Because even a 1x1 square (i.e. 1²) grid is still considered two-dimensional, even if this single cell is a <th> with no accompanying rows (again, validity and semantics are two very distinct matters).
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