CSS Table Borders: Outer Frames, Grids, and Row Lines
Set border on table for an outer frame and on th and td for a cell grid. Set border-collapse: collapse on the table when adjacent cells should share one border.
Draw an outer border or a cell grid
An HTML table generally has no visible borders before you style it. border: 1px solid #607d8b sets the width, style, and color together. If the style remains none, changing the width or color alone won’t draw a line. See the CSS border shorthand rules.
| Desired effect | Property or selector | Where the line appears |
|---|---|---|
| Outer frame | border on table |
Outside edge of the table |
| Full grid | border on th and td |
Between cells and along the outer edge |
| Row divider | border-block-end |
At the bottom of each row |
.demo { border-collapse: collapse; }
.demo.frame { border: 1px solid #607d8b; }
.demo.grid :is(th, td),
.demo.spaced :is(th, td) {
border: 1px solid #607d8b;
}
.demo :is(th, td) { padding: .6rem .75rem; }
Cell padding adds space between the content and its border. In this example, the first value sets the vertical padding and the second sets the horizontal padding. Use frame for the outer edge and grid for the cell lines.
Choose collapsed or separated cell borders
In the CSS table border models, collapse combines touching borders into one shared line. separate keeps each cell’s border independent. Set border-spacing: 6px 10px in the separated model for a 6-pixel horizontal gap and a 10-pixel vertical gap.
.demo.spaced {
border-collapse: separate;
border-spacing: 6px 10px;
}
Even with zero spacing, the separated model can show two adjacent cell borders. The hide value of empty-cells hides the border and background of empty cells in the separated model.
Draw row lines or leave the sides open
For horizontal dividers, put a border across the top of the table and along the bottom of each cell. The rows rules also close the bottom edge. In horizontal writing, block-start and block-end refer to the top and bottom.
.demo:is(.rows, .open) {
border-block-start: 1px solid #607d8b;
}
.demo:is(.rows, .open) :is(th, td) {
border-block-end: 1px solid #607d8b;
}
.demo.open :is(th, td) + :is(th, td) {
border-inline-start: 1px solid #607d8b;
}
Use open to add vertical lines between columns while keeping the sides open. The adjacent sibling combinator + selects cells with a preceding cell in the same row. Setting their border-inline-start adds a line at each column boundary.
Check row borders and shared-edge conflicts
A border on tr is ignored in the separated model. In the collapsed model, it can compete with cell or table borders at the same edge. Set the border on cells if you need a divider in separated mode too.
At a shared edge, hidden takes precedence. Setting one side to none can leave the adjacent cell’s line visible. A wider line generally wins among visible candidates. See the collapsed-border conflict rules for equal widths or differing styles.
Compare five styles on the same table
Save the following as a UTF-8 file named table-borders.html and open it in a browser. Start with grid. Change the last class on <table> to frame, spaced, rows, and open in turn and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Warehouse stock</title>
<style>
body { font: 1rem/1.5 system-ui, sans-serif; margin: 1rem; }
.jb-table-scroll { overflow-x: auto; }
.demo { inline-size: min(100%, 36rem); }
.demo caption { margin-block-end: .5rem; font-weight: bold; }
.demo th { text-align: start; background: #e5f0f7; }
.demo { border-collapse: collapse; }
.demo.frame { border: 1px solid #607d8b; }
.demo.grid :is(th, td),
.demo.spaced :is(th, td) {
border: 1px solid #607d8b;
}
.demo :is(th, td) { padding: .6rem .75rem; }
.demo.spaced {
border-collapse: separate;
border-spacing: 6px 10px;
}
.demo:is(.rows, .open) {
border-block-start: 1px solid #607d8b;
}
.demo:is(.rows, .open) :is(th, td) {
border-block-end: 1px solid #607d8b;
}
.demo.open :is(th, td) + :is(th, td) {
border-inline-start: 1px solid #607d8b;
}
</style>
</head>
<body>
<div class="jb-table-scroll">
<table class="jb-table-01 demo grid">
<caption>Warehouse stock</caption>
<thead>
<tr><th scope="col">Item</th><th scope="col">Quantity</th><th scope="col">Zone</th></tr>
</thead>
<tbody>
<tr><td>Pencils</td><td>12</td><td>3</td></tr>
<tr><td>Notebooks</td><td>87</td><td>2</td></tr>
</tbody>
</table>
</div>
</body>
</html>
frame draws the outside line, grid makes a full grid, and spaced shows gaps between cells. rows leaves horizontal lines, and open adds internal vertical lines with no vertical line along either outer edge.
If a border looks wrong, inspect the table’s border-collapse and the cell’s computed border-style in developer tools. To change where text or numbers sit, see table and cell content alignment.




