I want to display several stacked tables on a page, with a resume row and a button to show the rest of rows.
I want all td columns of the same width, to facilitate reading data, apart their different text length.
I used css width with pseudo-classes:
table td:nth-child(1) {
width:10%;
}
I tried this:
but they are/becomes unaligned…
when I click “show” to show the other rows, td becomes unaligned, changing the width
reclicking to hide the lower table, the width are also different from the beginning, again all td unaligned
Thank you Archibald, but your way I’ll get all td the same width.
Instead I want to choose specific width for each td column: I want they to be the same vertically aligned with all rows of all tables.
Edit:
I tried also removing inline min-width, max-width, but still they are unaligned…
There is no way to ensure columns in separate tables are the exact same width. Specifying the column (td) widths is just used as a general guide, not an exact width.
The main problem is that you have commented out the table-layout:fixed algorithm and without it widths are only a suggestion and will depend on content.
table {
border-spacing: 4px;
border-collapse: separate;
margin-left: auto;
margin-right: auto;
background: #F0F9FF;
table-layout:fixed;/* important for widths to be obeyed*/
}
Once you have uncommented the css and then removed this erroneous code it will work,
When you give widths to a table cells (in the table-layout:auto algorithm) then all widths must add up to 100% exactly or one (or more) must have no width to soak up the difference
Unless you use the table-layout:fixed algorithm and then widths win over content and will be obeyed. Any content that doesn’t fit into a cell will overflow.
Thank you Paul!
That’s good. Infact I already tried the fixed layout, but it wasn’t working, so I uncommented it temporarily…
It was that tbody thing…!
Ok, there is a little movement on the width when showing/hiding BOTH tables, but that’s acceptable because it is overall the tables and in every case very little.
So, I would ask 2 more things:
I would like, if possible, to indent the rows showed after the button pressed, so initial table resume rows remain more in evidence.
I don’t want to pad left or margin-left the content of td, but really move the td margin-left to be positioned more on right.
How do you check with JS the actual width assigned by the css pseudo-class td:nth-child?
I tried to read width property on some rows, with no luck, with this:
EDIT
The only way you can have the illusion of an indent is to use an empty cell on the nested content and on the parent table have the extra empty cell but colspan it to cover two cells. Table-cells must always align in columns within the same table. The only way to have uneven cells is to use extra cells and then colspan them.
Here’s a very rough example to get the indent with the extra cell. It’s just proof of concept.
I haven’t worked out the exact widths as was short of time and of course you need to check that you have 10 cells in every row or they are colspanned correctly.
Also it would be much easier if you simply added classes to the dynamic table and then all the hard work can be done in the css
It will be a lot of work to linearise something like this into a vertical layout (although not impossible) so may have to go in a max-width wrapper and allow the table to scroll sideways on mobile.