
CSS Width and Height: How to Set Minimum and Maximum Sizes
12/10/2019
CSS Layout and Positioning: Display, Position, Float, and More
12/10/2019
Use an HTML table for data with meaningful rows and columns. Keep the caption and headers in the markup, then use CSS to control how the table looks.
11. CSS table properties
The table summarizes the properties for captions, column sizing, borders, and empty cells.
| Property | Description | Values |
|---|---|---|
caption-side | Caption position relative to the table | top | bottom |
table-layout | Column sizing algorithm | auto | fixed |
border-collapse | Border model | collapse | separate |
border-spacing | Spacing between neighboring cell borders | length |
empty-cells | Whether empty cells show borders and backgrounds | show | hide |
11.1. Caption placement with caption-side
Put the table caption above the rows with top or below them with bottom. Write it in a caption element so readers can identify what the table shows.
.a { caption-side: bottom; }
.b { caption-side: top; }Open the caption-side example in CodePen
11.2. Column layout with table-layout
The default auto algorithm considers cell content when sizing columns. With fixed and a specified table width, column widths come from explicit columns or the first row, and remaining space is distributed. Long content may require wrapping or an overflow strategy.
.a { table-layout: auto;} /* Columns adapt to their content*/
.b { table-layout: auto; width: 100%; } /* Columns adapt to the content and the table fills the available width */
.c { table-layout: fixed; width: 400px; } /* Use the fixed layout algorithm */Open the table-layout example in CodePen
11.3. Shared borders with border-collapse
Use collapse to merge neighboring cell borders. Separate keeps each cell’s borders independent and lets you add gaps between cells.
.a { border-collapse: separate; }
.b { border-collapse: collapse; } Open the border-collapse example in CodePen
11.4. Space between cells with border-spacing
Works when borders are separate. One length sets both directions. Two lengths set horizontal and vertical spacing, in that order. Use cell padding to create space inside a cell.
.a { border-collapse: separate; border-spacing: 50px 10px;}
.b { border-collapse: collapse; border-spacing: 40px;} /* Spacing applies only in the separate-border model. */
.c { border-spacing: 0;} /* No spacing, but the borders remain separate. */Open the border-spacing example in CodePen
11.5. Empty cells with empty-cells
In the separate-border model, show draws borders and backgrounds around empty cells and hide suppresses them. It does not remove a cell from the table structure.
.a { empty-cells: hide; }
.b { empty-cells: show; } Open the empty-cells example in CodePen
Try it yourself
Place the caption below a table and leave a larger horizontal than vertical gap between cells.
Show one solution
table {
caption-side: bottom;
border-collapse: separate;
border-spacing: 1rem 0.5rem;
}