Table layout algorithms
Definition and purpose
A semantic HTML table represents data with row and column relationships. Its layout algorithm must coordinate column sizes across rows. With automatic table layout, content and width constraints inform the column sizing; with fixed layout and a definite table width, column widths can be established from explicit columns and the first row rather than measuring later cell content.
Fixed column distribution
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 300px; border-spacing: 0; border-collapse: collapse; } th, td { padding: 0; border: 0; text-align: start; } #x { width: 100px; } #a { table-layout: fixed; } </style> </head> <body> <table id="a"> <caption>Stock</caption> <thead> <tr> <th id="x" scope="col">Item</th> <th id="y" scope="col">Count</th> </tr> </thead> <tbody> <tr> <td>Notebook</td> <td>12</td> </tr> </tbody> </table> </body></html>Explicit first-column width leaves 200px for the remaining column.
Syntax and mental model
The fixed algorithm takes an explicit column width first, then an applicable first-row cell width, and distributes remaining space among unspecified columns, subject to the algorithm’s constraints. In our borderless 300px table the first header cell is 100px, leaving 200px for the second. border-spacing and collapsed borders change the available geometry. In automatic mode, widths are constraints, not necessarily exact allocations, and long unbreakable content can enlarge the table. Use a scrollable wrapper when needed without removing header semantics.
Automatic table sizing
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: 300px; border-spacing: 0; border-collapse: collapse; } th, td { padding: 0; border: 0; text-align: start; } #x { width: 100px; } #a { table-layout: auto; } </style> </head> <body> <table id="a"> <caption>Stock</caption> <thead> <tr> <th id="x" scope="col">Item</th> <th id="y" scope="col">Count</th> </tr> </thead> <tbody> <tr> <td>Notebook</td> <td>12</td> </tr> </tbody> </table> </body></html>The short content fits the specified table width; the individual column allocation is content-sensitive.
Common mistakes
Do not use table-layout: fixed without supplying a suitable table width and assume every long cell will fit. Preserve table headers and caption; fixed layout is not a substitute for overflow handling.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Display contents and hidden boxes, International component project. Next lesson: Multi-column layout.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Table layout algorithms — knowledge check
Five questions on table layout algorithms.
What does automatic table sizing consider?
Which cells can inform fixed column sizing when no explicit column width is set?
What remains after a 120px column in a borderless 320px two-column fixed table?
What can long content do in fixed layout?
What preserves header associations?