Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to represent an empty TH in HTML5?

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?

like image 710
Ky. Avatar asked Nov 07 '13 17:11

Ky.


People also ask

How do I show an empty table in HTML?

By simply adding the &nbsp; character code within your HTML, your table cell will be visible.

How do I create an empty TD in HTML?

The usual trick: &nbsp; The usual trick is to put &nbsp; (which is a so-called escape sequence, or formally entity reference, for a character called no-break space) into a table cell.

What is the correct way to indicate an empty table heading?

In an HTML table, table headers are denoted with a <th> element. If the table is very simple, has only one header group (either for rows or columns but not for both), and if the data is not ambiguous in its type, then using <th> alone is sufficient to identify the table heading.

How do you add th in HTML?

The <th> tag defines a header cell in an HTML table. An HTML table has two kinds of cells: Header cells - contains header information (created with the <th> element) Data cells - contains data (created with the <td> element)


1 Answers

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>
like image 83
BoltClock Avatar answered Sep 24 '22 16:09

BoltClock