CSS border-spacing Property
The border-spacing property sets the horizontal and vertical distance between adjacent table cell borders.
It applies to tables using the separated border model, which is enabled with border-collapse: separate. You can specify one length for equal horizontal and vertical spacing or two lengths to control each direction independently.
Unlike cell padding, which adds space inside a cell, border-spacing creates space between cells. The property also affects the spacing between cells and the outer table border.
border-spacing Syntax
The property accepts one or two non-negative CSS length values.
selector {
border-spacing: 10px;
}
The general syntax is:
selector {
border-spacing: <length> <length>?;
}
One Value
table {
border-collapse: separate;
border-spacing: 10px;
}
Sets both horizontal and vertical spacing to 10 pixels.
Two Values
table {
border-collapse: separate;
border-spacing: 10px 20px;
}
Sets horizontal spacing to 10 pixels and vertical spacing to 20 pixels.
Zero Spacing
table {
border-collapse: separate;
border-spacing: 0;
}
Relative Lengths
table {
border-collapse: separate;
border-spacing: 0.5rem 1rem;
}
Calculated Lengths
table {
border-collapse: separate;
border-spacing: calc(4px + 0.25rem) 12px;
}
CSS-Wide Keywords
table.inherited {
border-spacing: inherit;
}
table.reset {
border-spacing: initial;
}
table.unset {
border-spacing: unset;
}
table.reverted {
border-spacing: revert;
}
table.layer-reverted {
border-spacing: revert-layer;
}
Negative lengths and percentages are invalid. A nonzero length requires a unit.
Property Values
The border-spacing property accepts one or two length values and the standard CSS-wide keywords.
| Value | Description |
|---|---|
<length> |
Sets equal horizontal and vertical spacing. |
<length> <length> |
Sets horizontal spacing first and vertical spacing second. |
0 |
Removes spacing between adjacent cell borders. |
inherit |
Inherits the computed spacing values from the parent. |
initial |
Resets both spacing values to 0. |
unset |
Inherits the parent's computed spacing because the property is inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 |
| Inherited | Yes |
| Applies to | Table and inline-table elements |
| Computed value | Two absolute lengths |
| Animation type | By computed value type (two lengths) |
| Percentages | Not accepted |
| Negative values | Not accepted |
| Effective border model | separate |
| Property type | Individual property |
The initial value is 0, although browsers commonly apply their own default table spacing through user-agent stylesheets.
How It Works
When a table uses the separated border model, the browser places a specified amount of space between adjacent cell borders.
Step 1: Create a Table
<table>
<tr>
<td>HTML</td>
<td>CSS</td>
</tr>
<tr>
<td>Elements</td>
<td>Properties</td>
</tr>
</table>
Step 2: Use Separate Borders
table {
border-collapse: separate;
}
Step 3: Set Equal Spacing
table {
border-collapse: separate;
border-spacing: 10px;
}
The browser places 10 pixels between adjacent cell borders horizontally and vertically.
Step 4: Set Different Spacing
table {
border-collapse: separate;
border-spacing: 10px 20px;
}
Columns are separated by 10 pixels, and rows are separated by 20 pixels.
Step 5: Remove Spacing
table {
border-collapse: separate;
border-spacing: 0;
}
Adjacent cell borders have no spacing between them, but the table still uses the separated border model.
The spacing is measured between cell border edges, not between the text or other content inside the cells.
One vs. Two Values
The number of values determines whether the horizontal and vertical distances are equal or independent.
One Value
table {
border-spacing: 12px;
}
Both directions use 12 pixels.
Two Values
table {
border-spacing: 12px 24px;
}
The first value controls horizontal spacing. The second controls vertical spacing.
Comparison
| Declaration | Horizontal | Vertical |
|---|---|---|
border-spacing: 0; |
0 | 0 |
border-spacing: 8px; |
8px | 8px |
border-spacing: 8px 16px; |
8px | 16px |
border-spacing: 0 12px; |
0 | 12px |
border-spacing: 12px 0; |
12px | 0 |
Invalid Three Values
table {
border-spacing: 4px 8px 12px;
}
This declaration is invalid because the property accepts no more than two lengths.
Unlike the margin and padding shorthands, border-spacing does not support three-value or four-value syntax.
Horizontal Spacing
The first value controls the horizontal distance between neighboring cell borders in the same row.
Horizontal Spacing Only
table {
border-collapse: separate;
border-spacing: 20px 0;
}
Cells have 20 pixels of horizontal separation and no vertical separation.
Small Horizontal Gap
table {
border-spacing: 4px 12px;
}
Large Horizontal Gap
table {
border-spacing: 24px 12px;
}
No Horizontal Gap
table {
border-spacing: 0 12px;
}
Adjacent cells touch horizontally, but separate borders are still painted independently.
Horizontal Spacing With Cell Borders
table {
border-collapse: separate;
border-spacing: 12px 0;
}
td {
border: 1px solid #1769aa;
padding: 10px;
}
The space appears between the borders of neighboring cells.
Horizontal spacing also contributes to the table's overall width.
Vertical Spacing
The second value controls the vertical distance between cell borders in adjacent rows.
Vertical Spacing Only
table {
border-collapse: separate;
border-spacing: 0 16px;
}
Rows are separated vertically by 16 pixels, with no horizontal gap.
Small Vertical Gap
table {
border-spacing: 8px 4px;
}
Large Vertical Gap
table {
border-spacing: 8px 24px;
}
No Vertical Gap
table {
border-spacing: 8px 0;
}
Separated Table Rows
table {
border-collapse: separate;
border-spacing: 0 10px;
}
td {
padding: 12px;
background-color: #f3f8fc;
}
The spacing creates a visible gap between the background areas of neighboring rows.
Vertical spacing contributes to the table's overall height.
border-collapse Interaction
The border-spacing property only affects table layout when border-collapse is set to separate.
Separate Border Model
table {
border-collapse: separate;
border-spacing: 12px;
}
Each cell has its own borders, and the browser places 12 pixels between neighboring cell borders.
Collapsed Border Model
table {
border-collapse: collapse;
border-spacing: 12px;
}
The spacing declaration has no effect while the table uses collapsed borders.
Comparison
| Feature | separate | collapse |
|---|---|---|
| Cell borders | Painted separately | Combined through border conflict rules |
border-spacing |
Applied | Ignored for layout |
| Gaps between cells | Supported | Not created by border-spacing |
| Typical use | Separated cells and row gaps | Continuous table grid lines |
Switching Between Models
table {
border-collapse: separate;
border-spacing: 10px;
}
table.compact {
border-collapse: collapse;
}
The compact table does not display the specified spacing.
Use separate when the design requires visible gaps between cells. Use collapse when a continuous grid of shared borders is preferred.
Outer Table Spacing
Border spacing also affects the distance between the outermost cells and the table's border.
Table With an Outer Border
table {
border: 2px solid #1769aa;
border-collapse: separate;
border-spacing: 12px;
}
td {
border: 1px solid #cccccc;
padding: 10px;
}
The table includes spacing around the outermost cells as well as between adjacent cells.
Different Horizontal and Vertical Spacing
table {
border: 2px solid #1769aa;
border-collapse: separate;
border-spacing: 8px 16px;
}
The horizontal spacing contributes to the left and right edges, while the vertical spacing contributes to the top and bottom edges.
Adding Table Padding
table {
border: 2px solid #1769aa;
border-collapse: separate;
border-spacing: 8px;
padding: 12px;
}
Table padding adds further space between the table border and its internal content.
Removing Outer Gaps
table {
border-collapse: separate;
border-spacing: 0;
padding: 0;
}
With zero spacing and zero padding, the outer cells can sit directly against the inner edge of the table border.
The exact outer distance depends on the table border, table padding, and border-spacing values together.
Padding vs. Border Spacing
Cell padding and border spacing control different areas of a table layout.
Cell Padding
td {
padding: 12px;
}
Padding creates space between the cell content and its border.
Border Spacing
table {
border-collapse: separate;
border-spacing: 12px;
}
Border spacing creates space between the borders of neighboring cells.
Using Both
table {
border-collapse: separate;
border-spacing: 10px;
}
td {
border: 1px solid #1769aa;
padding: 15px;
}
Each cell has 15 pixels of internal padding, while adjacent cell borders are separated by 10 pixels.
Comparison
| Property | Controls | Applied To |
|---|---|---|
border-spacing |
Space between cell borders | Table |
padding |
Space inside cell borders | Table cells |
margin |
Space outside the table | Table element |
Use padding to improve readability inside cells and border spacing to separate cells visually.
CSS Length Units
The border-spacing property supports non-negative CSS lengths, including absolute and relative units.
Pixels
table {
border-spacing: 8px 12px;
}
rem Units
table {
border-spacing: 0.5rem 1rem;
}
The spacing is based on the root element's font size.
em Units
table {
border-spacing: 0.5em 1em;
}
The spacing is based on the table element's font size.
Viewport Units
table {
border-spacing: 1vw 0.5vw;
}
The spacing changes with the viewport width.
Calculated Lengths
table {
border-spacing: calc(4px + 0.25rem) calc(8px + 0.5rem);
}
Using clamp()
table {
border-spacing: clamp(4px, 1vw, 16px) clamp(6px, 1.5vw, 20px);
}
The spacing adjusts within the specified minimum and maximum values.
Using Custom Properties
:root {
--table-gap-x: 8px;
--table-gap-y: 12px;
}
table {
border-spacing: var(--table-gap-x) var(--table-gap-y);
}
Percentage values are not accepted, and calculated values must resolve to valid non-negative lengths.
Inheritance
Unlike most border properties, border-spacing is inherited.
Inherited Value
.table-wrapper {
border-spacing: 12px;
}
.table-wrapper table {
border-collapse: separate;
}
The table can inherit the 12-pixel spacing from its parent when no more specific spacing declaration overrides it.
Explicit Inheritance
table {
border-spacing: inherit;
}
Reset to Initial
table {
border-spacing: initial;
}
The initial value is zero.
Using unset
table {
border-spacing: unset;
}
Because the property is inherited, unset behaves like inherit.
Nested Tables
table {
border-collapse: separate;
border-spacing: 10px;
}
table table {
border-spacing: 4px;
}
The nested table uses 4-pixel spacing instead of the inherited value.
When unexpected spacing appears, check inherited declarations and browser default styles.
Table Layout and Sizing
Border spacing contributes to the total dimensions of a table.
Horizontal Space
table {
border-collapse: separate;
border-spacing: 10px 0;
}
For a table with three columns, horizontal spacing contributes four spacing intervals: one at each outer edge and two between the columns.
These intervals contribute 40 pixels to the table's horizontal sizing, before accounting for cell widths, table borders, and padding.
Vertical Space
table {
border-collapse: separate;
border-spacing: 0 12px;
}
For a table with three rows, vertical spacing contributes four spacing intervals, totaling 48 pixels.
Fixed Table Layout
table {
width: 100%;
table-layout: fixed;
border-collapse: separate;
border-spacing: 8px;
}
Spacing is still included in table layout calculations. Increasing the spacing reduces the width available for columns within a constrained table.
Automatic Table Layout
table {
table-layout: auto;
border-collapse: separate;
border-spacing: 12px;
}
The browser considers cell content and spacing when determining the table's dimensions.
Wide Tables
.table-scroll {
overflow-x: auto;
}
.table-scroll table {
border-collapse: separate;
border-spacing: 8px;
}
A scrollable wrapper can accommodate a table that becomes wider than its available container.
Large spacing values can significantly increase the size of tables with many rows or columns.
Cascade and Overrides
The CSS cascade determines which border spacing values apply when multiple declarations target the same table.
Later Declaration
table {
border-spacing: 6px;
border-spacing: 12px;
}
The later 12-pixel value wins when cascade priority is otherwise equal.
Higher Specificity
table {
border-spacing: 6px;
}
table.featured {
border-spacing: 16px;
}
Class Override
.data-table {
border-spacing: 8px;
}
.data-table.compact {
border-spacing: 2px;
}
CSS Layers
@layer base, components;
@layer base {
table {
border-spacing: 4px;
}
}
@layer components {
.data-table {
border-spacing: 12px;
}
}
For normal declarations, the later layer takes precedence.
Collapsed Border Override
table {
border-collapse: separate;
border-spacing: 12px;
}
table.compact {
border-collapse: collapse;
}
The compact table retains its specified spacing value, but that value has no visible effect while borders are collapsed.
When spacing appears incorrect, inspect the cascade, inherited values, user-agent styles, and the computed border-collapse value.
CSS Custom Properties
CSS custom properties make it easier to reuse and adjust table spacing values.
Shared Spacing
:root {
--table-spacing: 10px;
}
table {
border-collapse: separate;
border-spacing: var(--table-spacing);
}
Separate Horizontal and Vertical Values
:root {
--table-spacing-x: 8px;
--table-spacing-y: 16px;
}
table {
border-collapse: separate;
border-spacing: var(--table-spacing-x) var(--table-spacing-y);
}
Component Variants
.data-table {
--spacing-x: 8px;
--spacing-y: 12px;
border-collapse: separate;
border-spacing: var(--spacing-x) var(--spacing-y);
}
.data-table.compact {
--spacing-x: 2px;
--spacing-y: 4px;
}
.data-table.spacious {
--spacing-x: 16px;
--spacing-y: 24px;
}
Fallback Values
table {
border-collapse: separate;
border-spacing: var(--gap-x, 8px) var(--gap-y, 12px);
}
Theme-Based Spacing
:root {
--table-gap: 8px;
}
[data-density="comfortable"] {
--table-gap: 16px;
}
table {
border-collapse: separate;
border-spacing: var(--table-gap);
}
Custom properties are useful when multiple tables need consistent spacing or adjustable density.
Responsive Design
Table spacing can be adjusted for smaller screens, narrow containers, and print layouts.
Media Query
table {
border-collapse: separate;
border-spacing: 12px;
}
@media (max-width: 600px) {
table {
border-spacing: 4px;
}
}
Different Spacing by Direction
table {
border-collapse: separate;
border-spacing: 12px 16px;
}
@media (max-width: 600px) {
table {
border-spacing: 4px 8px;
}
}
Container Query
.table-container {
container-type: inline-size;
}
.data-table {
border-collapse: separate;
border-spacing: 4px;
}
@container (min-width: 600px) {
.data-table {
border-spacing: 12px;
}
}
Fluid Spacing
table {
border-collapse: separate;
border-spacing: clamp(2px, 1vw, 12px);
}
Scrollable Table
.table-wrapper {
max-width: 100%;
overflow-x: auto;
}
.table-wrapper table {
border-collapse: separate;
border-spacing: 8px;
}
Print Styles
table {
border-collapse: separate;
border-spacing: 10px;
}
@media print {
table {
border-spacing: 2px;
}
}
Reducing spacing on narrow screens can help preserve room for cell content, but important data should remain readable and accessible.
Transitions and Animation
The border-spacing property can animate between compatible length values.
Basic Transition
table {
border-collapse: separate;
border-spacing: 4px;
transition: border-spacing 0.3s ease;
}
table:hover {
border-spacing: 12px;
}
The horizontal and vertical gaps increase gradually when the table is hovered.
Different Horizontal and Vertical Transitions
table {
border-collapse: separate;
border-spacing: 4px 8px;
transition: border-spacing 0.4s ease;
}
table:hover {
border-spacing: 12px 20px;
}
Keyframe Animation
@keyframes table-spacing-change {
from {
border-spacing: 2px;
}
to {
border-spacing: 12px;
}
}
.animated-table {
border-collapse: separate;
animation: table-spacing-change 2s ease-in-out infinite alternate;
}
Reduced Motion Preference
table {
border-collapse: separate;
border-spacing: 4px;
transition: border-spacing 0.3s ease;
}
table:hover {
border-spacing: 12px;
}
@media (prefers-reduced-motion: reduce) {
table {
transition: none;
}
}
Animating border spacing can trigger table layout recalculations. Use these effects sparingly, especially on large data tables.
Practical Uses
The border-spacing property is useful for creating readable tables, separated rows, and adjustable table layouts.
Separated Data Cells
.data-table {
border-collapse: separate;
border-spacing: 8px;
}
.data-table td {
padding: 12px;
background-color: #f3f8fc;
}
Row Gaps
.data-table {
border-collapse: separate;
border-spacing: 0 10px;
}
.data-table td {
padding: 12px;
background-color: #f3f8fc;
}
Compact Table
.compact-table {
border-collapse: separate;
border-spacing: 2px;
}
.compact-table td {
padding: 6px;
}
Spacious Table
.spacious-table {
border-collapse: separate;
border-spacing: 16px 20px;
}
.spacious-table td {
padding: 15px;
}
No Cell Gaps
.grid-table {
border-collapse: separate;
border-spacing: 0;
}
.grid-table td {
border: 1px solid #1769aa;
}
Each cell retains its own border. Adjacent borders may appear doubled because the borders are not collapsed.
Separated Header Cells
.report-table {
border-collapse: separate;
border-spacing: 6px 10px;
}
.report-table th {
padding: 12px;
background-color: #1769aa;
color: #ffffff;
}
.report-table td {
padding: 12px;
background-color: #f3f8fc;
}
Use consistent spacing values to maintain clear relationships between rows and columns.
border-spacing Example
The following example compares tables with zero spacing, equal spacing, horizontal spacing, and vertical spacing.
<div class="border-spacing-demo">
<div class="demo-group">
<h3>border-spacing: 0;</h3>
<table class="spacing-zero">
<tr><td>HTML</td><td>CSS</td></tr>
<tr><td>Elements</td><td>Properties</td></tr>
</table>
</div>
<div class="demo-group">
<h3>border-spacing: 10px;</h3>
<table class="spacing-equal">
<tr><td>HTML</td><td>CSS</td></tr>
<tr><td>Elements</td><td>Properties</td></tr>
</table>
</div>
<div class="demo-group">
<h3>border-spacing: 16px 0;</h3>
<table class="spacing-horizontal">
<tr><td>HTML</td><td>CSS</td></tr>
<tr><td>Elements</td><td>Properties</td></tr>
</table>
</div>
<div class="demo-group">
<h3>border-spacing: 0 16px;</h3>
<table class="spacing-vertical">
<tr><td>HTML</td><td>CSS</td></tr>
<tr><td>Elements</td><td>Properties</td></tr>
</table>
</div>
</div>
.border-spacing-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
.border-spacing-demo .demo-group {
min-width: 0;
}
.border-spacing-demo h3 {
font-size: 1rem;
margin-bottom: 10px;
}
.border-spacing-demo table {
width: 100%;
border-collapse: separate;
border: 2px solid #1769aa;
}
.border-spacing-demo td {
padding: 10px;
border: 1px solid #b7d1e5;
background-color: #f3f8fc;
color: #173b5e;
}
.border-spacing-demo .spacing-zero {
border-spacing: 0;
}
.border-spacing-demo .spacing-equal {
border-spacing: 10px;
}
.border-spacing-demo .spacing-horizontal {
border-spacing: 16px 0;
}
.border-spacing-demo .spacing-vertical {
border-spacing: 0 16px;
}
border-spacing: 0;
| HTML | CSS |
| Elements | Properties |
border-spacing: 10px;
| HTML | CSS |
| Elements | Properties |
border-spacing: 16px 0;
| HTML | CSS |
| Elements | Properties |
border-spacing: 0 16px;
| HTML | CSS |
| Elements | Properties |
The first table has no spacing between cell borders. The second uses equal spacing in both directions.
The third table separates columns horizontally, while the fourth separates rows vertically. All four tables use border-collapse: separate.
Important Considerations
Keep the following points in mind when using border-spacing:
- Purpose: Controls horizontal and vertical spacing between table cell borders.
- Initial value:
0. - Inheritance: The property is inherited.
- Applies to: Table and inline-table elements.
- Border model: Only affects tables using
border-collapse: separate. - One value: Sets equal horizontal and vertical spacing.
- Two values: Sets horizontal spacing first and vertical spacing second.
- Length units: Supports non-negative CSS lengths.
- Percentages: Not accepted.
- Negative values: Not accepted.
- Zero: Removes spacing between cell borders without collapsing them.
- Outer spacing: Also contributes to spacing between the outer cells and the table border.
- Cell padding: Controls space inside cell borders, not between cells.
- Table dimensions: Spacing contributes to overall table width and height.
- Browser defaults: User-agent stylesheets may specify nonzero table spacing.
- Custom properties: Can store reusable spacing values.
- Responsive design: Supports media queries, container queries, and calculated lengths.
- Animation: Supports interpolation between compatible length values.
- Accessibility: Use appropriate table markup and header associations for data tables.
- Browser support: Widely supported by major browsers.
Use border-spacing when a table needs controlled gaps between its cells rather than a continuous collapsed-border grid.
Browser Support
The border-spacing property is widely supported in Chrome, Edge, Firefox, Safari, and other major browsers.
It is a long-established CSS property and does not require vendor prefixes. The table must use the separated border model for the spacing to affect layout.
table {
border-collapse: separate;
border-spacing: 8px 12px;
}
For current compatibility details, visit Can I Use? and search for border-spacing.
Specifications
The border-spacing property is defined in the CSS Tables Module Level 3 specification.
The specification describes how spacing is applied between cells in the separated border model and how it contributes to table layout.
