FLX FLX - 1 year ago 91
CSS Question

HTML colspan 0 alternative

I have a HTML table, and i would like to give the last row (in

tag) only one cell expanding to all the table.

I was using
, then i saw that it only worked in Firefox.
I then tried
It works fine, but not pass the w3c validator (Very important in my project).

Is there a working alternative ?

I saw people who use
, not a bad idea but are there some performance problems with this ?

Thanks for advice.

Answer Source

My first answer is: refactor your code. If you need the total number of columns to build the table footer then the function you use to build the table body should return that number (and not only the HTML).

That said and only in case it's too complicated (or you don't have control about that code) you may simply count them by yourself, I would avoid any trick about colspan because it's behavior isn't homogeneous (and it's not validated too).

You can easy count the number of cells using the first row (if the table is well formed all the rows have the same number of columns).

The first naive solution would be to split() HTML tbody then to substr_count() the <td/> of the first row. Unfortunately this may work only in a very controlled situation (tables must be well formed, table may contain or not tbody and it doesn't manage colspan of that cells).

Better solution involves a small HTML parser (see this great post here on SO for a good and detailed list), when you have DOM then you can easily count TDs and check for their attributes (I say this in advance: no, you can't use regex to parse HTML).

To be honest I think refactoring is much more suitable...