Tables are fundamental to web design, but making table cells (td elements) behave exactly how you want can be frustrating. Whether you're building a responsive design that needs cells to stack on mobile or trying to create a full-width table that spans its container, understanding CSS table behavior is essential. This guide covers practical solutions for making td elements take up full width, with code examples you can use immediately.
If you're exploring other CSS layout techniques, our guide on centering elements with CSS display table-cell complements these table styling methods nicely. For a comprehensive overview of modern CSS layouts, see our article on full width containers with limited width parents.
Understanding Table Cell Width Behavior
By default, table cells (<td>) size themselves based on their content and share table width proportionally. The table-layout property defaults to auto, which means column widths adjust to fit content. Without explicit styling, multiple <td> elements in a row will share the available width equally.
The default behavior means:
- Cells expand to fit their content
- Column widths are distributed based on content size
- Empty space is shared proportionally across columns
- The browser calculates the optimal column widths automatically
Understanding this default behavior is crucial before applying fixes for your specific use case.
The MDN Web Docs on table-layout explains that this automatic calculation can lead to unexpected column sizing when dealing with tables that have varying content lengths.
The Role of table-layout
The CSS table-layout property controls how the browser calculates table cell widths. It has two values:
table-layout: auto (default)
- The browser calculates column widths based on content
- All cells must be processed before rendering
- Slower for large tables with many rows
- More flexible but less predictable
table-layout: fixed
- Requires explicit width on the table element
- Column widths are determined by the first row or explicit widths
- Subsequent rows follow the same column widths regardless of content
- Faster rendering because the browser can render after downloading the first row
The fixed layout gives you predictable, consistent column widths across all rows, making it ideal for data tables where alignment matters. Our guide on CSS compare techniques demonstrates how to use tables effectively for comparing content side by side.
Method 1: Display Block for Stacked Cells
The most common solution for making td elements stack vertically is using display: block combined with width: 100%.
td {
display: block;
width: 100%;
clear: both;
}
How it works:
display: blockchanges cells from table-cell to block-level elementswidth: 100%makes each cell take the full container widthclear: bothensures proper stacking without float issues
This approach is ideal for mobile-responsive tables where side-by-side columns become too cramped.
For more CSS transform techniques, check our guide on CSS transform translate which covers advanced CSS positioning methods. Additionally, our article on centering in the unknown explores various centering techniques that work well with stacked table layouts.
This approach is particularly useful for these scenarios:
Mobile-First Designs
Stack cells on small screens where side-by-side columns are too cramped for readability.
Form Layouts
Use table semantics for accessibility while achieving stacked layouts on all screen sizes.
Data Presentations
Display each cell value on its own line for better data comprehension.
Dashboard Interfaces
Create tables that collapse gracefully on smaller devices.
Method 2: Fixed Table Layout
For predictable, consistent column widths, use table-layout: fixed with an explicit width on the table element.
table {
table-layout: fixed;
width: 100%;
}
How it works:
- The table requires an explicit width (here, 100%)
- Column widths are determined by the first row's cell widths or explicit width attributes
- All subsequent rows follow the same column widths regardless of content
- Creates consistent, predictable table sizing across all rows
This method is faster to render because the browser doesn't need to wait for all content to calculate optimal widths.
The MDN table-layout documentation notes that fixed layout is significantly faster for large tables because the horizontal layout depends only on the table width, column widths, borders, and cell spacing--not cell content.
Combining Fixed Layout with Width Control
For precise control over column proportions, set specific widths on your cells:
table {
table-layout: fixed;
width: 100%;
}
td, th {
width: 33.33%;
}
Pro tips:
- Use percentage widths for responsive behavior
- The
box-sizing: border-boxproperty includes padding and borders in width calculations - Set different widths on different columns by targeting specific cells
- This method gives you pixel-perfect control over column proportions
Our guide on CSS flexbox layouts explores complementary modern layout techniques that work well with these table styling approaches. For more CSS fundamentals, see our article on what we know so far about CSS reading order.
Method 3: Full-Width Table Container
To make an entire table span the full width of its container, combine width settings with border-collapse.
html, body {
margin: 0;
padding: 0;
width: 100%;
}
table {
width: 100%;
border-collapse: collapse;
}
Key properties:
width: 100%on the table makes it span the full container widthborder-collapse: collapseremoves default cell spacing and combines borders- Reset margin and padding on body/html if tables aren't spanning full width
- The container div can constrain the table width if needed
For comprehensive CSS integration methods, see our overview on including CSS in JavaScript applications. Our guide on applying custom CSS to admin areas also demonstrates practical CSS application techniques.
Container-Based Full-Width Tables
For embedded tables within content sections, wrap them in a container:
<div class="table-container">
<table>
<!-- table content -->
</table>
</div>
.table-container {
width: 100%;
max-width: 800px; /* Optional constraint */
margin: 0 auto;
}
table {
width: 100%;
border-collapse: collapse;
}
This approach gives you flexibility to control the table's maximum width while still spanning the container.
Best Practices for Responsive Tables
Responsive Design Strategies
- Use media queries to switch between stacked and side-by-side layouts
- Consider horizontal scrolling for data-heavy tables on mobile with
overflow-x: auto - Test table rendering across multiple screen sizes
- Provide alternative views for complex tables on mobile devices
@media (max-width: 768px) {
td {
display: block;
width: 100%;
}
}
Performance Considerations
Why fixed layout is faster:
- The browser can render the table after downloading the first row
- No recalculation needed based on cell content
- For large tables with many rows, fixed layout provides better performance
- Trade-off: less flexible but more predictable and faster
Common Pitfalls to Avoid
- Forgetting explicit width with
table-layout: fixed - Not using
border-collapse: collapsefor consistent borders - Ignoring
box-sizingwhen setting fixed widths on cells - Overlooking padding and border calculations in width percentages
- Not testing on actual mobile devices
For performance optimization insights, explore our guide on animation performance which covers broader CSS performance considerations. Our article on scroll-triggered animation with vanilla JavaScript also demonstrates performance-conscious animation techniques.
Complete Implementation Examples
Responsive Data Table
This example shows a table that displays side-by-side on desktop and stacked on mobile:
<table class="responsive-table">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget A</td>
<td>$19.99</td>
<td>In Stock</td>
</tr>
<tr>
<td>Widget B</td>
<td>$29.99</td>
<td>Limited</td>
</tr>
</tbody>
</table>
.responsive-table {
width: 100%;
border-collapse: collapse;
}
.responsive-table th,
.responsive-table td {
padding: 12px 16px;
border: 1px solid #ddd;
text-align: left;
}
/* Stack on mobile */
@media (max-width: 768px) {
.responsive-table th,
.responsive-table td {
display: block;
width: 100%;
box-sizing: border-box;
}
.responsive-table th {
background-color: #f5f5f5;
font-weight: bold;
}
}
Frequently Asked Questions
Why isn't my td element expanding to 100% width?
By default, td elements share table width proportionally based on content. Use `display: block` to make them stack, or use `table-layout: fixed` with explicit widths for predictable column sizing.
Should I use table-layout auto or fixed?
Use `auto` when you want content to determine column widths. Use `fixed` when you need predictable, consistent column widths and faster rendering for large tables.
How do I make a table span full width on mobile?
Use a media query to set `td { display: block; width: 100%; }` on mobile screens. This stacks cells vertically for better readability on small devices.
What's the difference between width: 100% and max-width: 100%?
`width: 100%` forces the element to match the container width exactly. `max-width: 100%` allows the element to be smaller but never larger than the container.
Sources
- MDN Web Docs: table-layout - Official CSS table-layout documentation
- MDN Web Docs: td element - HTML td element reference
- Stack Overflow: CSS td full width solutions - Community solutions for full-width table cells
- GeeksforGeeks: Full-Width Table CSS - Comprehensive guide to full-width tables