Skip to main content

How to Merge Table Cell Borders with border-collapse in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Give a table and its cells a border and you get double lines, with a gap between each cell. border-collapse: collapse on the table merges them into single lines, like a grid.

Syntax​

Syntax
<table selector> {
border-collapse: collapse;
}
PartWhat it does
<table selector>The table, such as table or a class on it.
collapseMerges the borders of neighboring cells into one. The default, separate, keeps each cell's own border.

MDN's border-collapse page explains both values, and that with separate borders, the distance between cells comes from border-spacing.

The Example​

The example is the same small table of holidays twice. Every table, heading cell and data cell has a 1-pixel grey border, and the second table has the class collapsed.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule giving table, th and td a 1px solid 8a8886 border and a rule for the class collapsed with border-collapse collapse, and two tables with the headings Date and Holiday and one row, 1 January, New Year's Day, the second table with the class collapsed.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Border collapse</title>
<style>
table, th, td {
border: 1px solid #8a8886;
}

.collapsed {
border-collapse: collapse;
}
</style>
</head>
<body>
<table>
<tr><th>Date</th><th>Holiday</th></tr>
<tr><td>1 January</td><td>New Year's Day</td></tr>
</table>
<p></p>
<table class="collapsed">
<tr><th>Date</th><th>Holiday</th></tr>
<tr><td>1 January</td><td>New Year's Day</td></tr>
</table>
</body>
</html>

The empty paragraph between the tables only adds some space between them.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the first table with double grey lines and small gaps around every cell, and the second table with single grey lines between the cells.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The first table has double lines. Its border-collapse is separate, the default: the table and each cell draw their own border, with a small gap between the cells.
  • The second table has single lines. With collapse, neighboring borders merged into one, and the gaps are gone.

Common Mistakes​

MistakeWhat happensDo this instead
Putting border-collapse on the cellsIt does nothing there: with td { border-collapse: collapse; }, the table stayed separate, with 2-pixel gaps between the cellsPut it on the table
Using border-spacing on a collapsed tableIt's ignored: with collapse, the gap stayed 0Use border-spacing only with separate

Troubleshooting​

SymptomLikely causeFix
The table still has double linesborder-collapse is on the cells, or the selector doesn't match the tablePut it on the table
The gap between cells doesn't changeThe table is collapsedSwitch to separate to space the cells

FAQ​

How do I change the space between cells?​

Keep the borders separate and set border-spacing. With border-spacing: 10px, the cells were 10 pixels apart. On a collapsed table, the same rule left them touching.

Why does the browser use double lines by default?​

The default is separate, where every cell is its own box with its own border. Chromium also adds 2 pixels of spacing between the cells.

Conclusion​

Give the table and its cells their borders, then add border-collapse: collapse to the table for clean single lines. Keep separate when you want space between the cells.

Additional Resources​