π Introduction
The table-layout property in CSS controls how the browser calculates column widths in HTML tables. It helps improve table rendering speed, layout stability, and predictable column sizing. Especially useful for complex tables, fixed-width designs, and responsive dashboards. πβ¨
π― What table-layout Does
- Controls how column widths are determined
- Makes tables render faster when using fixed layout
- Allows consistent alignment of cells
- Prevents layout shifts during table loading
β¨ Syntax
Syntax
table {
table-layout: auto | fixed;
}Note
π§© Values of table-layout
1. auto (default)
The browser inspects the **content of all cells** to determine column widths. This can cause slow rendering for large tables.
auto example
table {
table-layout: auto;
}- Slower rendering
- More accurate width based on content
- Columns expand/shrink based on cells
2. fixed
Column widths are determined by:
β table width β column widths (<col> or <th>) β borders/padding
**Cell content does NOT affect layout.**
fixed example
table {
width: 100%;
table-layout: fixed;
}- Much faster rendering for large tables
- Prevents layout shifts
- Equal column widths if none specified
π₯ Practical Examples
1. Equal Column Layout (fixed)
Equal Columns
table {
width: 100%;
table-layout: fixed;
}
td {
border: 1px solid #ccc;
padding: 8px;
}2. Fixed Layout with Custom Column Sizes
Custom Columns
table {
table-layout: fixed;
width: 100%;
}
colgroup col:first-child {
width: 150px;
}
colgroup col:nth-child(2) {
width: 200px;
}3. auto vs fixed Comparison
Comparison
/* table-layout: auto */
table.auto td {
width: auto; /* content decides */
}
/* table-layout: fixed */
table.fixed {
table-layout: fixed;
}π auto vs fixed β Comparison Table
| Property | auto | fixed |
|---|---|---|
| Speed | Slower | Much faster |
| Column width based on | Cell content | Table width + col/th widths |
| Stability | Can shift during load | Stable layout |
| Use Case | Content-based tables | Dashboards, fixed UI tables |
π§ Tips & Best Practices
- Use fixed for large data tables (faster rendering)
- Use colgroup to manage individual column widths
- Combine with overflow: hidden when fixed layout hides overflow text
- Prefer auto when content must dictate layout (e.g., names, addresses)
π Conclusion
The table-layout property gives you precise control over how table columns are sized. Using table-layout: fixed speeds up rendering and creates predictable layouts, while auto lets content define width naturally. With the right choice, your tables can be both performant and beautifully structured. π