Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to equally distribute columns width in HTML table when the number of them is unknown?

I am working with JSF/ICEFaces and I need to generate a table from a dynamic tree structure with an unknown (at run-time) number of rows and columns. The table must have width: 100%; so it occupies all the space inside its container.

I am able to generate all the markup required for the table, but I found that the cells' width is not the same.

It would be easy for me to set CSS width: (100/numberOfColumns)% if I knew the number of elements I'm rendering. Unfortunately, I can't modify the classes returned by my backing bean so I can only iterate over them with ui:repeater component.

Do you know if there is a CSS way to make sure that all columns in a table have the same width, no matter what it is?

I wouldn't like to use the Javascript way. Just as cleaner code as possible.

like image 868
usr-local-ΕΨΗΕΛΩΝ Avatar asked Apr 19 '11 08:04

usr-local-ΕΨΗΕΛΩΝ


People also ask

How do I equally space columns in HTML table?

Take the width of the table and divide it by the number of cell (). If the table dynamically widens or shrinks you could dynamically increase the cell size with a little javascript.

How do you fix a column width in a HTML table?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.


1 Answers

table {     table-layout : fixed; } 
like image 91
Nobody Avatar answered Oct 04 '22 06:10

Nobody