views:

130

answers:

4

I have a HTML table with several columns and several rows. The last row does not always have as many TD elements as the other rows. It renders correctly in several browsers I have checked and passes validation checks. But it seems incorrect. Should the number of TD elements be the same in every row?

EDIT: This could happen with or without colspans and rowspans.

+6  A: 

"Should the number of TD elements be the same in every row?"

Though it is not strictly required, I say "yes, or you should have colspans to compensate for them."

<table>
    <tr>
        <td>two</td>
        <td>columns</td>
    </tr>
    <tr>
        <td colspan="2">one big column</td>
    </tr>
</table>

My take on this, despite that it is not required by the W3C spec, is that when considering how spotty browsers are about the way they support anything, I'd make it as consistent as possible, using colspans or empty cells, to make sure that it's doing exactly what I want on all browsers.

The secondary benefit is for anyone maintaining your code. Makes it more obvious what is intended, rather than it being percieved as a possible error.

Beska
Really though? Say I have 3 columns - a row identifier and 2 attributes, and the 2nd attribute isn't applicable for one row - a colspan doesn't seem very semantic (same value for two rows to me).
Ross
I tend to agree, but do you have a reference to back that up?
Liam
The previous sentence says "user agents should base the number of columns on what is required by the rows" - which clearly targets that section at user agents (such as browsers and spiders) not authors. –
David Dorward
+7  A: 
David Dorward
Ooh! I was just about to put that quote in...but I wasn't clear whether when they said "the end of the row should be padded with empty cells" applied to the coder, or the browser. The way I read it, it was the coder, since that's what the rest of the document seemed to be applicable to.
Beska
The previous sentence says "user agents should base the number of columns on what is required by the rows" - which clearly targets that section at user agents (such as browsers and spiders) not authors.
David Dorward
I guess that if it is indeed tabular data, then rather than having cells missing, there should be cells which describe the lack of data such as NULL, or N/A.
Liam
Whee! You're quite right!
Beska
+3  A: 

There is no requirement in HTML standards that the number of columns defined by each row match. It can be said that you "should" provide a uniform number of columns, from a moralistic or best-practices standpoint, and I'd support that, but it isn't required by standard, which is why it's validating.

chaos
Even if it was required by the standard, it would still validate. There is no way for a DTD to describe a requirement to have the same number of columns as every other row in the table.
David Dorward
A: 

Just an idea: try to create your table with element ;-) There are some issues in rendering tables.