Can a table row have no cells?

时间秒杀一切 提交于 2019-12-10 16:36:45

问题


I'm using HTML tables to create a timetable. Each row represents half hour, and the cells can span multiple rows by using rowspan.

Sometimes I have empty tr elements, because all the slots are taken by cells from previous rows.

In that case, the HTML validator complains:

Row [N] of a row group established by a tbody element has no cells beginning on it.

Sure, I could remove those empty tr and reduce the rowspan value of the expanded cells. But then the rowspan value would no longer univocally correspond to the duration of the cell.

Therefore, are empty trs really invalid? Why?


回答1:


You can’t have empty rows in a table in a valid HTML document per the current HTML spec.

Maybe you rightly should be able to, but the spec currently clearly says that you can’t. So if anybody believes it should be allowed by the spec, the right thing to do is to file an issue against the HTML Standard in its github tracker or even write a patch and open a PR for it.

Specifically the spec defines the following error case:

https://html.spec.whatwg.org/multipage/tables.html#the-table-element

If there exists a row or column in the table containing only slots that do not have a cell anchored to them, then this is a table model error.

In Internet-spec terms, that is a “normative” authoritative statement that can’t be ignored or overridden by anything else. It is stating a hard requirement.

The spec elsewhere says this:

The tr element: Content model

https://html.spec.whatwg.org/multipage/tables.html#the-tr-element Zero or more td, th, and script-supporting elements

But that’s not actually a contradiction and does not conflict with or supersede or override the “table model error” requirement cited above. In fact, it’s the opposite—the “table model error” requirement supersedes the more-liberal requirement in the other section (cited above) that a valid tr element can have zero or more children.

Any stricter requirements in a spec always supersede or override any more-liberal requirements.




回答2:


In the table model section of the HTML spec, you can find the following statement:

Rows usually correspond to tr elements, though a row group can have some implied rows at the end in some cases involving cells spanning multiple rows.

However, that doesn't work for empty rows in the middle of your table, as the implied rows can only occur at the end.

Another possibility would be to add a column with <th> elements to the left that serve as a caption for the row. After all, the user may want to know what one table row represents - just as you have told us here.



来源:https://stackoverflow.com/questions/32382485/can-a-table-row-have-no-cells

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