The core difference lies in their function: border-collapse
determines if table cell borders merge or remain separate, while border-spacing
sets the distance between cell borders only when they are separate.
Let's break down each property to understand their roles in styling HTML table borders.
Understanding border-collapse
The border-collapse
CSS property controls whether table borders are separated or collapsed into a single border. It affects the way adjacent table cells and the table's outer border are rendered.
Key border-collapse
values:
separate
: This is the default value. Adjacent table cells have their own distinct borders. There is a gap between borders of adjacent cells. This model allows the use ofborder-spacing
.collapse
: Adjacent cells share borders. When cells share a border, the most visually prominent border style and width (based on specific rules) are displayed. In this model, there is no space between borders, and theborder-spacing
property has no effect.
Understanding border-spacing
The border-spacing
CSS property specifies the distance between the borders of adjacent table cells. This property only applies when border-collapse
is set to separate
.
As stated in the reference: "In the separated model, adjacent cells each have their own distinct borders. In the collapsed model, adjacent table cells share borders. The border-spacing CSS property specifies the distance between the borders of adjacent table cells (only for the separated borders model)."
You can specify one value for both horizontal and vertical spacing, or two values for horizontal and vertical spacing respectively.
Example:
border-spacing: 10px;
- Sets a 10px gap between cell borders horizontally and vertically.border-spacing: 15px 5px;
- Sets a 15px horizontal gap and a 5px vertical gap between cell borders.
Key Differences at a Glance
Here's a table summarizing the main distinctions:
Feature | border-collapse: separate |
border-collapse: collapse |
border-spacing |
---|---|---|---|
Border Style | Each cell has its own border. | Adjacent cells share a single border. | Does not define border style, only spacing. |
Space between Cells | Yes, controlled by border-spacing . |
No space between shared borders. | Defines this space only in separate model. |
Compatibility | Works with border-spacing . |
border-spacing has no effect. |
Only works when border-collapse is separate . |
Visual Impact | Creates grid-like appearance with gaps. | Creates a single, continuous border grid. | Controls the size of the gap between cells in separate mode. |
Practical Application
- Use
border-collapse: collapse;
when you want a clean, continuous grid of borders for your table data. This is common for standard data tables. - Use
border-collapse: separate;
along withborder-spacing
when you want visible gaps between cells, perhaps for a more stylized or card-like layout within a table structure.
In essence, border-collapse
is the switch that determines the border model, and border-spacing
is the dial that adjusts the gap size if the separated model is chosen.