18

Say I have the given table:

       +------+------+------+
       | Col1 | Col2 | Col3 |
+------+------+------+------+
| Row1 | D1.1 | D1.2 | D1.3 |
+------+------+------+------+
| Row2 | D2.1 | D2.2 | D2.3 |
+------+------+------+------+
| Row3 | D3.1 | D3.2 | D3.3 |
+------+------+------+------+

And I want to represent it in HTML5. The tricky thing is that tables like this must be semantically important, but the top-left cell is not semantically important, but instead a spacer to line up the more important column headers. What's the best way to do this? My first idea is to do it like this:

<table>
    <thead>
        <tr>
            <th></th>
            <th>Col1</th>
            <th>Col2</th>
            <th>Col3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>Row1</th>
            <td>D1.1</td>
            <td>D1.2</td>
            <td>D1.3</td>
        </tr>
        <tr>
            <th>Row2</th>
            <td>D2.1</td>
            <td>D2.2</td>
            <td>D2.3</td>
        </tr>
        <tr>
            <th>Row3</th>
            <td>D3.1</td>
            <td>D3.2</td>
            <td>D3.3</td>
        </tr>
    </tbody>
</table>

Though, putting <th></th> in there feels just wrong, like using <p>&nbsp;</p> for spacing. Is there a better way to do this?

3
  • 2
    There is nothing wrong with an empty element. There is even an :empty selector. Commented Nov 7, 2013 at 17:12
  • @JoshC Interesting... could you elaborate in an answer?
    – Ky -
    Commented Nov 7, 2013 at 17:17
  • I think it's worth mentioning that, in the past 7 years, I've come to see empty elements like these to be semantically important, since they semantically represent "there's no data here." This is very important for parsers, which is the intention behind semantic HTML
    – Ky -
    Commented Dec 7, 2020 at 18:04

3 Answers 3

14

It's completely acceptable to have an empty <th> element, speaking in terms of either validity or semantics. Nothing in the spec forbids it; in fact, it contains at least one example that makes use of an empty <th> for this very purpose:

The following shows how one might mark up the gross margin table on page 46 of Apple, Inc's 10-K filing for fiscal year 2008:

<table>
 <thead>
  <tr>
   <th>
   <th>2008
   <th>2007
   <th>2006
 <tbody>
  <tr>
   <th>Net sales
   <td>$ 32,479
   <td>$ 24,006
   <td>$ 19,315
 <!-- snip -->
</table>
3
  • 1+ Great find... i'm just wondering why they don't close their element tags in this example.. Commented Nov 7, 2013 at 17:43
  • 4
    @JoshC: Some of the examples have closing tags, others don't. I guess it's just their way of saying that closing tags are optional for most table elements - which they are.
    – BoltClock
    Commented Nov 7, 2013 at 17:45
  • @JoshC: But I'm positive that by leaving out the closing tag like this, the line break and the indentation could be taken as part of the content of that <th> making it non-empty for the purposes of the :empty selector. As far as HTML is concerned, however, it's still an "empty cell", even if all it contains is whitespace (the spec mentions this).
    – BoltClock
    Commented Nov 8, 2013 at 10:34
2

For a discussion about semantics and empty table elements I would like to refer to this question on StackOverflow

Styling of "empty" cells (like background or borders) can sometimes depend on the absence/presence of "content" that is why people often put a &nbsp; inside. There is a special CSS tag for styling empty cells you can read about it here on MDN.

table {
    empty-cells: hide;
}

Here you can find another article with some nice background information on this topic.

2
  • It would be nice to have some of the contents of those links in your answer, in case of link rot
    – Ky -
    Commented Jan 5, 2018 at 14:11
  • 1
    @Supuhstar, thanks for your comment. I added the css example, &nbsp; is self explanatory in my opinion.
    – Wilt
    Commented Jan 6, 2018 at 12:20
0

Any better way of using empty <th></th>:

Exact code:

<tr>
 <th></th>
 <th colspan="6"></th>
</tr>
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.