Adjust Nested Containers to Corresponding Sizes

Tables nested into tables. The nested tables are not related to each other, thus their column widths are different. Such layout does not look good. The JavaScript working in this page tries to fix that. Use your browser's "View Page Source" menu item to see its source code. You can also read in my Blog about this.

In the examples below, tables have a green border, header cells a blue, data cell a magenta. Tables of nesting level 0 are khaki, 1 yellow, 2 light-blue. Table cells that have a colspan defined (spanning several columns) are rendered orange.
As soon as you have clicked one of the "Adjust" buttons, any table cell will show its "category" (see definition below) on mouse-hover as a red label.

Predefine Pixel Widths on Columns


HTML TABLE with colspan cells

Column 1
Col 2.1Col 2.2Col 2.32.4
Column 3
XXX
XXXXXXX XXXXXXXXXXXX 0000
X XXXXXX 0000
C 3.1C 3.2
XXXXXXXXX
XXXXXX
XX
XXX
XXX
X
XXXXXXXXXXXXXXXXXXXXXX X XXX 0000
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 0000
XXXXX XX X 0000
D 3.1D 3.2
XXXXXX
XXXX
XXXX
X
X
XX XXXXXXXX 0000
E 3.1E 3.2
XXXXXXXXXXXXXXXXXXXXXXXXX

HTML DIV-table with CSS display: table; (does not support colspan)

Column 1
Col 2.1
Col 2.2
Col 2.3
2.4
Column 3
XXX
XXXXXXX
XX
XXXXXXXXXXXX
0000
X
XXXXXX
XXX
0000
C 3.1
C 3.2
XXX
XXXXXX
XXXXXX
X
X
XXX
XX
X
X
XXXXXXXXXXXXXXXXXXXXXX
X
XXX
0000
XXXXXXXXXX
XXXXXXXXXX
XXXXXXXXXX
0000
XXXXX
XX
X
0000
D 3.1
D 3.2
X
XXXXX
XXXX
XXXX
X
X
XX
XXXXXXXX
0000
E 3.1
E 3.2
XXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX


Term Definition:

A "column category" is a marker that is equal for all cells that are directly below each other. A numbering like "1", "1.1", "1.2", ... "2.1.1", ... would do it. The number of dots represents the nesting level.

Strategy for Natural Width

Strategy for Full Width with One Elastic Column

This script does NOT use a resize-listener.

This script currently can not provide more than one elastic column. I tried this out, but together with colspan cells the layout breaks completely.

Known Bug

After adjustment to natural size, mind cell 2.1 in first HTML TABLE, 3rd row from above (excluding header row), with content "XXXXXXXXXXXXXXXXXXXXXX". It is wider than its content, and there is no cell in this column that justifies that width. It had this width right from start because of the 3-colspan cell below it, which sized the table to be so wide that the cell above grew wider than needed.

The script does NOT fix this, because finding out the real "natural width" of a cell would require measuring the width of its innerHTML in a cell-independent context. This could be done by enclosing the content into a <div style="display: inline-block;">. But that might be slow, and break other functionality. Moreover such is a very rare case!

This bug does not occur in the DIV-table below, because DIV-tables do not support the colspan attribute. This also is the reason why the DIV-table has a little bit different column widths than the HTML TABLE.


ɔ⃝  Fritz Ritzberger, 2016-01-30