- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- Table Styling & Borders
HTML Lists and Tables
Table Styling & Borders
By default a table has no visible borders at all. Adding them well comes down to one property,
border-collapse, and understanding which element (the table, the rows, or the cells) actually draws each line.
Try it
Two identical tables. The first uses the default border-collapse: separate; the second collapses. Run it and look at where the lines double up.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table borders</title>
<style>
table { margin-bottom: 24px; font-family: system-ui; }
th, td { border: 1px solid #334155; padding: 6px 12px; }
.separate { border-collapse: separate; border-spacing: 4px; }
.collapse { border-collapse: collapse; }
</style>
</head>
<body>
<table class="separate">
<tr><th>Tag</th><th>Type</th></tr>
<tr><td><td></td><td>Data cell</td></tr>
<tr><td><th></td><td>Header cell</td></tr>
</table>
<table class="collapse">
<tr><th>Tag</th><th>Type</th></tr>
<tr><td><td></td><td>Data cell</td></tr>
<tr><td><th></td><td>Header cell</td></tr>
</table>
</body>
</html>Who draws the border
border on <table> draws a box round the outside. border on <th> and <td> draws a box round each cell. Rows (<tr>) and column groups can carry borders too, but only in collapse mode. Most of the confusion with table borders is putting the property on the wrong one of these.
border-collapse: separate (the default)
Every cell is its own box with its own four borders, and there is a gap between cells set by border-spacing (default 2px). Two neighbouring cells each draw a border, so you see two lines with a gap. This is what the old cellspacing attribute controlled.
border-collapse: collapse
Adjacent borders merge into one line, border-spacing is ignored, and borders on <tr> start working. When two merged borders conflict (one cell says 1px grey, its neighbour says 2px black) the wider one wins, then the "more solid" style, then the one from the cell (over row, over table). Ninety per cent of tables want this mode.
The four common looks
css
/* 1. Full grid — every cell outlined */
.grid { border-collapse: collapse; }
.grid th, .grid td { border: 1px solid #cbd5e1; }
/* 2. Horizontal rules only — the usual "data table" look */
.rows { border-collapse: collapse; }
.rows th, .rows td { border-bottom: 1px solid #e2e8f0; }
.rows thead th { border-bottom: 2px solid #334155; }
/* 3. Outer frame only */
.frame { border-collapse: collapse; border: 1px solid #334155; }
/* 4. Card-style with rounded corners — needs separate mode */
.card { border-collapse: separate; border-spacing: 0; border: 1px solid #cbd5e1; border-radius: 8px; overflow: hidden; }
.card th, .card td { border-bottom: 1px solid #e2e8f0; }
.card tr:last-child td { border-bottom: 0; }A realistic example: a readable data table
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data table</title>
<style>
.scroll { overflow-x: auto; } /* phones: scroll the table, not the page */
.data { border-collapse: collapse; width: 100%; min-width: 480px; font: 15px system-ui; }
.data th, .data td { padding: 10px 14px; text-align: left; border-bottom: 1px solid #e2e8f0; }
.data thead th { border-bottom: 2px solid #0f172a; }
.data td:not(:first-child), .data th:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }
.data tbody tr:nth-child(even) { background: #f8fafc; }
.data tbody tr:hover { background: #e0f2fe; }
</style>
</head>
<body>
<div class="scroll">
<table class="data">
<thead>
<tr><th>Lesson</th><th>Exercises</th><th>Bug fixes</th><th>Minutes</th></tr>
</thead>
<tbody>
<tr><td>Introduction to HTML</td><td>5</td><td>5</td><td>110</td></tr>
<tr><td>CSS Flexbox</td><td>6</td><td>4</td><td>95</td></tr>
<tr><td>Two pointer technique</td><td>5</td><td>5</td><td>90</td></tr>
<tr><td>JavaScript closures</td><td>4</td><td>6</td><td>120</td></tr>
</tbody>
</table>
</div>
</body>
</html>Horizontal rules only, a heavier line under the header, numbers right-aligned in tabular figures so the digits line up, zebra striping for long tables, and a scroll wrapper so a wide table does not break the page on a phone. That is the whole recipe for most data tables.
Common mistakes
1. Border on the table only
table { border: 1px solid } gives you a box around the outside and nothing between cells. To outline cells you must put the border on <th> and <td>.
2. Double lines
Cell borders with the default separate mode produce two lines and a gap between every pair of cells. Either add border-collapse: collapse, or set border-spacing: 0 and draw only border-right and border-bottom on each cell so neighbours do not overlap.
3. Border on tr does nothing
tr { border-bottom: 1px solid } is ignored in separate mode. Switch to collapse, or put the border on the row's cells instead.
4. border-radius with collapse
Rounded corners on a collapsed table are clipped or ignored in most browsers. For a card look, use border-collapse: separate; border-spacing: 0 and draw the borders on cells, as in look 4 above.
5. The old attributes
border="1", cellpadding and cellspacing on <table> still work, but they are obsolete, cannot be changed per breakpoint, and produce the ugly 3D inset border from 1997. CSS: border, padding on cells, border-spacing.
6. Wide tables on mobile
A table cannot wrap its columns, so a six-column table on a 360px phone either shrinks to unreadable text or pushes the page sideways. Wrap it in a div with overflow-x: auto so only the table scrolls. Giving the table a min-width stops the columns crushing each other.
Browser support
border-collapse, border-spacing and cell borders are universal. font-variant-numeric: tabular-nums works in all current browsers and is harmless in old ones.
Related chapters
- CSS for tables: alignment, widths and
table-layout - CSS border: the shorthand and its styles
<td>and<th><caption>: the table's title
Table Styling & Borders exercises
A line around each cell
The table is given. Give each td a border: 1px, solid, black.
Result
