Table Layout: Table Display, Borders, Sizing and Responsive Tables
Styling data tables: the table display values and anonymous boxes, automatic and fixed table layout, separate and collapsed borders, border-spacing, captions, cell alignment, zebra rows, sticky headers, and responsive tables that keep their meaning.
kvi5hnuCode Sprout team member8 min readOn this page
Tables are for tabular data: rows and columns where each cell relates to a header, like prices, timetables or comparison charts. For page layout, use grid. But data tables are common, and the browser's table layout has its own rules for sizing, borders and alignment that work differently from everything else in CSS.
The table display values
HTML table elements get their layout from display values in the browser's styles, and any element can use them:
display | HTML element |
|---|---|
table, inline-table | <table> |
table-row | <tr> |
table-row-group, table-header-group, table-footer-group | <tbody>, <thead>, <tfoot> |
table-cell | <td>, <th> |
table-column, table-column-group | <col>, <colgroup> |
table-caption | <caption> |
If parts of the structure are missing, the browser creates anonymous table boxes to complete it. Tested in Chrome: a <div style="display: table"> with two <span>s set to display: table-cell works as a one-row table, with an anonymous row; in a 300px table, with the same text in both, the two cells are 150px each. With different text, auto layout (below) gives the wider content more of the width.
Before flexbox and grid, display: table-cell was a common way to make equal-height columns or to center vertically. Today, use flexbox or grid for that, and keep table display for data. Changing a real <table>'s display to something else can also remove its table semantics in some browsers' accessibility trees, which matters for responsive tables below.
Table sizing: table-layout
table-layout: auto (the default) sizes columns from their content: the browser reads every cell, then shares the table's width according to each column's minimum and preferred widths. Tested in Chrome, with the default font: in a 400px table with the cells <td>short</td> and <td>A much longer sentence that needs more room than the other one</td>, the columns are 34px and 360px. The short column is just its word plus the default 1px cell padding on each side, and the sentence's column gets the rest, minus the three 2px gaps of border-spacing.
Two consequences:
- A table's
widthis only a minimum in auto layout. Tested in Chrome, with the default font: a table withwidth: 100pxand one cell containing "Pneumonoultramicroscopicsilicovolcanoconiosis" grew to 320px, the word's width plus the cell padding and border spacing. - Large tables can be slow, because every cell must be measured before anything is drawn.
table-layout: fixed sizes columns from the first row and the <col> widths only, ignoring the rest of the content. It's faster and predictable, and the table keeps its width:
.data { table-layout: fixed; inline-size: 100%; }
.data .col-name { inline-size: 40%; } /* on a <col> or a first-row cell */
Tested in Chrome: in a 400px table with table-layout: fixed and width: 100px on the first cell, the first column is 102px (the width plus the 1px padding on each side) and the second takes the rest, 292px, and a long word overflows its cell instead of widening the column. Combine it with overflow-wrap: anywhere or overflow: hidden; text-overflow: ellipsis on cells.

The numbers under each table come from the render: auto layout treats width: 25% and the table's width as minimums, fixed layout keeps both, and overflow-wrap cleans up what fixed layout lets overflow.
Borders
border-collapse

| Value | Effect |
|---|---|
separate (initial) | each cell has its own borders, with border-spacing between cells |
collapse | neighbouring cells share one border, with no spacing |
In the separate model, border-spacing sets the space between cells and around the edge, one value or horizontal vertical; the browser default is 2px, which is why unstyled tables have little gaps. Borders on rows and row groups have no effect. empty-cells: hide hides the borders and background of empty cells.
In the collapse model, borders can go on cells, rows, columns and the table, and when two meet, a conflict is resolved: hidden wins over everything, then the wider border, then the stronger style (double, solid, dashed, dotted, ridge, outset, groove, inset), then the element closest to the cell. Half of a collapsed border sits on each side of the line.
A modern data table usually collapses borders and draws only horizontal lines:
.data { border-collapse: collapse; inline-size: 100%; }
.data th, .data td { padding: 0.5rem 0.75rem; border-block-end: 1px solid #e8dccb; text-align: start; }
.data thead th { border-block-end: 2px solid #8a3b12; }
border-radius on a table only works with border-collapse: separate (use border-spacing: 0).
Captions and alignment
<caption>gives the table an accessible name.caption-side: top(the default) orbottomplaces it.- Cells are vertically centered by default (
vertical-align: middlefrom the browser's styles, tested in Chrome). In table cells,vertical-aligntakestop,middle,bottomorbaseline, and aligns the content inside the cell. - Align numbers to the end, with
text-align: endandfont-variant-numeric: tabular-nums(article 86) so digits line up.
.data td.num { text-align: end; font-variant-numeric: tabular-nums; }
.data tbody tr:nth-child(even) { background: #faf6f0; } /* zebra rows */
.data tbody tr:hover { background: #f4efe6; }
Sticky headers
Header cells can stick while the table scrolls (article 57):
.table-wrap { max-block-size: 60vh; overflow: auto; }
.data thead th { position: sticky; inset-block-start: 0; background: white; }
With border-collapse: collapse, a sticky header's border may scroll away with the body, because collapsed borders belong to both cells; use separate with border-spacing: 0, or a box-shadow as the line, if the header needs a visible edge.
Responsive tables
Wide tables don't fit on phones. In order of preference:
-
Scroll the table horizontally inside a wrapper. It keeps the table structure intact for screen readers. Make the wrapper focusable and labelled, so keyboard users can scroll it:
html<div class="table-wrap" tabindex="0" role="region" aria-labelledby="cap"> <table class="data"><caption id="cap">Opening hours</caption>…</table> </div>css.table-wrap { overflow-x: auto; } -
Hide less important columns on small screens, if the data still makes sense without them.
-
Stack rows as cards, with each cell labelled by its header. The label can come from a
data-attribute withattr()(article 30):
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.frame { inline-size: var(--w, 20rem); container-type: inline-size; margin-top: 0.5rem; }
.data { border-collapse: collapse; inline-size: 100%; }
caption { text-align: start; font-weight: 700; color: #8a3b12; margin-bottom: 0.4rem; }
.data th, .data td { padding: 0.4rem 0.5rem; text-align: start; border-block-end: 1px solid #e8dccb; }
.data thead th { border-block-end: 2px solid #8a3b12; }
@container (width < 17rem) {
.data thead { display: none; } /* headers move into each cell's label */
.data tr { display: block; padding: 0.4rem 0; border-block-end: 2px solid #e8dccb; }
.data th, .data td { display: flex; justify-content: space-between; border: 0; padding: 0.2rem 0.5rem; }
.data td::before { content: attr(data-label); font-weight: 600; color: #555; }
}
Narrow the frame below 17rem: each row becomes a card, and each value is labelled with its column header from data-label. Note that display: none on the <thead> removes the column headers from the accessibility tree too; the data-label text is only visual.
Changing display on table elements can remove their table semantics in some browsers, so screen readers no longer announce rows and columns. If you stack a table, test it with a screen reader, and consider adding explicit roles (role="table", role="row", role="cell", role="columnheader") to restore them. Scrolling is the safer default.
Accessibility
- Use real
<table>markup for data, with<th>for headers andscope="col"orscope="row". - Give the table a
<caption>. - Don't use tables for layout.
What's current
- All table display values,
table-layout,border-collapseandborder-spacingwork in all browsers. - Sticky table headers work everywhere.
- Container queries (article 78) make component-level responsive tables possible, as in the demo.
Common questions
Why are there little gaps between my table cells?
The browser's default border-spacing is 2px with separate borders. Use border-collapse: collapse, or border-spacing: 0.
Why does my table ignore its width?
In auto layout, width is a minimum; content can make it wider. Use table-layout: fixed, and let long content wrap.
Why doesn't border-radius work on my table?
It needs border-collapse: separate.
Should I use display: table for layout?
No; use flexbox or grid. Keep table display for tabular data.
How do I make a wide table work on phones?
Put it in a wrapper with overflow-x: auto, so the table scrolls sideways while the page doesn't; make the wrapper focusable with a label so keyboard users can scroll it too. Restyling rows as stacked cards is the other option, covered under responsive tables above.
Summary
- Table elements use
display: table,table-row,table-celland the rest; missing parts get anonymous boxes. table-layout: autosizes columns by content, andwidthis only a minimum;fixeduses the first row and is fast and predictable.border-collapse: separatewithborder-spacing, orcollapsewith shared borders and conflict resolution.- Cells are vertically centered by default; align numbers to the end with tabular figures.
- For small screens, prefer a scrollable, labelled wrapper; stack rows as cards only with care for semantics.
Practice questions
Write CSS for a full-width table with collapsed borders, a line under each row, and a thicker line under the header.
Answer
table { border-collapse: collapse; inline-size: 100%; }
th, td { padding: 0.5rem; border-block-end: 1px solid #ddd; }
thead th { border-block-end: 2px solid #333; }
A table has table-layout: fixed; width: 600px, a first row with cells of 200px and 400px, and a long URL in a later row. What happens to the URL's column?
Answer
Nothing: fixed layout only uses the first row, so the column stays 400px and the URL overflows its cell, unless the cell has overflow-wrap: anywhere or overflow: hidden.
Two collapsed cells meet: one has border-right: 1px solid red, the other border-left: 3px dashed blue. Which border is drawn?
Answer
The 3px dashed blue one. Width is compared before style, and wider wins.
What's the safest way to make a wide data table usable on phones, and what does it need for keyboard users?
Answer
Put it in a wrapper with overflow-x: auto. Give the wrapper tabindex="0", a role such as region, and a label (for example aria-labelledby pointing to the caption), so keyboard users can focus and scroll it.
Review quiz
Test yourself on Part 10 with the CSS Part 10 review quiz.
Previous: Multi-Column Layout: columns, column-span and Fragmentation
Next: Responsive Design: Mobile First, the Viewport Meta Tag and Breakpoints

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.