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

℡╲_俬逩灬. 提交于 2019-12-05 01:20:26

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 the same 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
 <!-- omitted for brevity -->
</table>

There is nothing wrong with an empty element. It is not incorrect syntax, nor is it a bad practice, in fact; it would be much better to use an empty element in this instance as opposed to using other hacks to achieve such a layout.

Aside from that, there is even an :empty selector.

This selector is proof in itself that there is nothing incorrect about an empty element.

The :empty pseudo-class represents an element that has no children at all. In terms of the document tree, only element nodes and content nodes (such as DOM [DOM-LEVEL-3-CORE] text nodes, CDATA nodes, and entity references) whose data has a non-zero length must be considered as affecting emptiness; comments, processing instructions, and other nodes must not affect whether an element is considered empty or not.

W3C http://www.w3.org/TR/css3-selectors/#empty-pseudo

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.

Ronak Jain

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

Exact code:

<tr>
 <th></th>
 <th colspan="6"></th>
</tr>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!