π Introduction
The border-collapse property in CSS determines how table borders are rendered. It controls whether the borders of table cells are separated (default) or collapsed into a single shared border. This property is essential when styling HTML tables for cleaner layouts, consistent spacing, and reduced border clutter. πβ¨
π― What Does border-collapse Do?
- Controls how table borders behave
- Allows collapsing all inner borders into one
- Reduces double borders in table designs
- Works only on <table> elements
β¨ Syntax
Syntax
table {
border-collapse: separate | collapse;
}π§© Values Explained
1. separate (default)
Borders remain independent for each table cell. There is space between cells (controlled using border-spacing).
separate (default)
table {
border-collapse: separate;
border-spacing: 10px;
}2. collapse
Adjacent borders are merged into a single border. This gives a cleaner, grid-like table appearance.
collapse
table {
border-collapse: collapse;
}Note
border-spacing property is ignored.π₯ Practical Examples
1. A Table With Separate Borders
Separate Borders Example
table {
border-collapse: separate;
border-spacing: 8px;
}
td, th {
border: 2px solid #333;
padding: 10px;
}2. A Clean Collapsed Border Table
Collapsed Borders Example
table {
border-collapse: collapse;
}
td, th {
border: 2px solid #666;
padding: 10px;
}3. Styling With Collapse for Grid-Like Layout
Grid Table
.grid-table {
border-collapse: collapse;
}
.grid-table td {
border: 1px solid #999;
padding: 6px;
}4. Rounded Corners Only on Outer Table
When borders collapse, you can style the table border as a whole.
Rounded Table
table {
border-collapse: collapse;
border: 2px solid #333;
border-radius: 8px;
}
td {
border: 1px solid #aaa;
padding: 8px;
}π border-collapse vs border-spacing
| Property | Used With | Meaning |
|---|---|---|
| border-collapse: separate | β border-spacing works | Cells have gaps between them |
| border-collapse: collapse | β border-spacing ignored | All borders merge into one |
π§ Tips & Best Practices
- Use collapse to create clean, minimal, grid-like tables (great for data tables)
- Use separate when you want spacing between cells or custom rounded borders per cell
- Combine
border-collapse: collapsewiththstyles for neat header rows - For modern UI tables, collapse usually looks cleaner and more professional
π Conclusion
The border-collapse property is essential for controlling how table borders behave. Whether you want clean grid-like tables or spaced-out cells, this property allows precise control over table layout and visual structure. Mastering it will help you build elegant, readable, and professional-looking data tables. π