Tillitsdone
down Scroll to discover

CSS Border-Spacing Enhance Table Layout

CSS border-spacing enhances table layout by controlling the spacing between cells.

Available options include single length values, horizontal and vertical lengths, and global values.

Learn how to use this property effectively.
thumbnail

Introduction

The border-spacing CSS property sets the distance between the borders of adjacent cells in a table. It’s a great tool for making your tables look better by controlling the spacing between cells. This property works only when the border-collapse property is set to separate.

Specification

The border-spacing CSS property is defined in the CSS Level 2 (Revision 2) specification. This ensures that the property works consistently across different browsers.

Description

The border-spacing property defines the distance between the borders of adjacent cells in a table. It’s useful for making tables look better and is effective only when the border-collapse property is set to separate.

When border-spacing is applied, it affects both the spacing between cells and the spacing along the outside edge of the table. The distance between the table’s border and the cells in the first or last column or row is the sum of the relevant border-spacing value and the table’s padding.

Syntax

The border-spacing property can be specified using one or two values. Here is the basic syntax:

/* Single length value */
border-spacing: 2px;
/* Horizontal length | Vertical length */
border-spacing: 1cm 2em;
/* Global values */
border-spacing: inherit;
border-spacing: initial;
border-spacing: revert;
border-spacing: revert-layer;
border-spacing: unset;

Explanation

  1. Single length value: When one length value is specified, it defines both the horizontal and vertical spacing between cells.
  2. Horizontal length | Vertical length: When two length values are specified, the first value defines the horizontal spacing between cells, and the second value defines the vertical spacing.
  3. Global values: These values include inherit, initial, revert, revert-layer, and unset, which allow you to control the inheritance and default behavior of the border-spacing property.

Values

The border-spacing property accepts several types of values:

  1. Length Value: Specifies the distance between the borders of adjacent cells. It can be a single value or two values.
    • Single Length Value: If one length value is specified, it defines both the horizontal and vertical spacing between cells.
      border-spacing: 2px;
    • Two Length Values: If two length values are specified, the first value defines the horizontal spacing between cells, and the second value defines the vertical spacing.
      border-spacing: 1cm 2em;
  2. Global Values: Used to control the inheritance and default behavior of the border-spacing property.
    • inherit: Inherits the border-spacing value from the parent element.
      border-spacing: inherit;
    • initial: Sets the border-spacing property to its default value.
      border-spacing: initial;
    • revert: Resets the border-spacing property to its initial value, ignoring any user-defined styles.
      border-spacing: revert;
    • revert-layer: Resets the border-spacing property to its value in the previous cascade layer.
      border-spacing: revert-layer;
    • unset: Resets the border-spacing property to its inherited value if it is inheritable, or to its initial value if not.
      border-spacing: unset;

Formal Definition

The border-spacing property sets the distance between the borders of adjacent cells in a table. It applies to table and inline-table elements when the border-collapse property is set to separate.

  • Initial Value: 0
  • Applies To: table and inline-table elements
  • Inherited: Yes
  • Computed Value: Two absolute lengths
  • Animation Type: Discrete

Formal Syntax

The formal syntax for the border-spacing property is:

border-spacing = <length> [1,2]

Explanation

  • <length>: Represents a length value (e.g., px, cm, em).
  • [1,2]: Indicates that the border-spacing property can take one or two length values.

Examples

Basic Example

In this example, we’ll apply a uniform spacing of 2px between all adjacent cells in a table.

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 2px;
border: 1px solid black;
}
td {
width: 50px;
height: 50px;
background-color: lightgray;
text-align: center;
vertical-align: middle;
}

Example with Different Horizontal and Vertical Spacing

In this example, we’ll apply different horizontal and vertical spacing values to the table cells.

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 10px 5px; /* 10px horizontal spacing, 5px vertical spacing */
border: 1px solid black;
}
td {
width: 50px;
height: 50px;
background-color: lightgray;
text-align: center;
vertical-align: middle;
}

Example with Padding and Spacing

In this example, we’ll apply both padding and border-spacing to the table, demonstrating how the border-spacing value affects the outside edge of the table.

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 1em 0.5em; /* 1em horizontal spacing, 0.5em vertical spacing */
padding: 0 2em 1em 0; /* Adding padding to the table */
border: 1px solid orange;
}
td {
width: 1.5em;
height: 1.5em;
background: #d2d2d2;
text-align: center;
vertical-align: middle;
}

Example with Inherited Value

In this example, we’ll use the inherit value to inherit the border-spacing value from the parent element.

HTML
<div class="table-container">
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
</div>
CSS
.table-container {
border-spacing: 10px;
}
table {
border-collapse: separate;
border-spacing: inherit; /* Inherits the border-spacing value from the parent */
border: 1px solid black;
}
td {
width: 50px;
height: 50px;
background-color: lightgray;
text-align: center;
vertical-align: middle;
}

Spacing and Padding Table Cells

The border-spacing property is a powerful tool for controlling the spacing between table cells. When used effectively, it can greatly enhance the visual appeal and readability of tables. This section will focus on how border-spacing interacts with padding to create well-structured and aesthetically pleasing tables.

Understanding Spacing and Padding

  • Spacing: The border-spacing property sets the distance between the borders of adjacent cells in a table. This spacing can be applied both horizontally and vertically.
  • Padding: The padding property defines the space between the content of a cell and its border. This can be used in conjunction with border-spacing to create a more refined and visually appealing table layout.

How Spacing and Padding Work Together

When border-spacing is applied, it not only affects the spacing between cells within the table but also influences the spacing along the outside edge of the table. The distance between the table’s border and the cells in the first or last column or row is the sum of the relevant border-spacing value and the table’s padding.

Example: Combining Spacing and Padding

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 1em 0.5em; /* 1em horizontal spacing, 0.5em vertical spacing */
padding: 0 2em 1em 0; /* Adding padding to the table */
border: 1px solid orange;
}
td {
width: 1.5em;
height: 1.5em;
background: #d2d2d2;
text-align: center;
vertical-align: middle;
padding: 0.5em; /* Adding padding inside the cells */
}

Tips for Effective Use

  • Consistency: Keep spacing and padding values consistent for a uniform look.
  • Readability: Use spacing and padding to improve the table’s readability.
  • Aesthetics: Experiment with different values to achieve the desired visual effect.

Examples

Example: Uniform Spacing with Border

In this example, we’ll apply a uniform spacing of 10px between all adjacent cells in a table, along with a border to highlight the table structure.

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 10px; /* Uniform spacing between cells */
border: 2px solid blue;
}
td {
width: 50px;
height: 50px;
background-color: lightblue;
text-align: center;
vertical-align: middle;
}

Example: Custom Horizontal and Vertical Spacing

In this example, we’ll apply different horizontal and vertical spacing values to the table cells. This creates a more customized layout with varied spacing between cells.

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 15px 5px; /* 15px horizontal spacing, 5px vertical spacing */
border: 2px solid green;
}
td {
width: 50px;
height: 50px;
background-color: lightgreen;
text-align: center;
vertical-align: middle;
}

Example: Combining Border Spacing with Cell Padding

In this example, we’ll combine border-spacing with cell padding to create a more refined and visually appealing table layout. The padding adds space inside the table cells, while border-spacing adds space between the cell borders.

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 1em 0.5em; /* 1em horizontal spacing, 0.5em vertical spacing */
padding: 0 2em 1em 0; /* Adding padding to the table */
border: 2px solid purple;
}
td {
width: 1.5em;
height: 1.5em;
background: lavender;
text-align: center;
vertical-align: middle;
padding: 0.5em; /* Adding padding inside the cells */
}

Example: Nested Tables with Border Spacing

In this example, we’ll create a nested table structure and apply border-spacing to both the outer and inner tables. This showcases how border-spacing can be used to enhance the layout of more complex table structures.

HTML
<table class="outer-table">
<tr>
<td>
<table class="inner-table">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
</td>
<td>
<table class="inner-table">
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td>G</td>
<td>H</td>
<td>I</td>
</tr>
</table>
</td>
</tr>
</table>
CSS
.outer-table {
border-collapse: separate;
border-spacing: 20px; /* Spacing between outer table cells */
border: 2px solid red;
}
.inner-table {
border-collapse: separate;
border-spacing: 10px; /* Spacing between inner table cells */
border: 1px solid orange;
}
td {
width: 50px;
height: 50px;
background-color: lightcoral;
text-align: center;
vertical-align: middle;
}

Example: Border Spacing with Different Units

In this example, we’ll use different units for border-spacing to demonstrate how the property can be applied using various CSS length units such as pixels (px), centimeters (cm), and ems (em).

HTML
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
CSS
table {
border-collapse: separate;
border-spacing: 1cm 0.5em; /* 1cm horizontal spacing, 0.5em vertical spacing */
border: 2px solid teal;
}
td {
width: 2cm;
height: 2cm;
background-color: lightcyan;
text-align: center;
vertical-align: middle;
}

Browser Support

The border-spacing CSS property enjoys wide support across various web browsers, ensuring that web developers can rely on its consistent implementation for creating visually appealing and well-structured tables. Below is an overview of the browser support for the border-spacing property:

Desktop Browsers

  • Google Chrome: Fully supported since version 1.0.
  • Mozilla Firefox: Fully supported since version 1.0.
  • Internet Explorer: Fully supported since version 8.0.
  • Microsoft Edge: Fully supported since version 12.0.
  • Opera: Fully supported since version 4.0.
  • Safari: Fully supported since version 1.0.

Mobile Browsers

  • Chrome for Android: Fully supported.
  • Firefox for Android: Fully supported.
  • Safari on iOS: Fully supported.
  • Opera Mobile: Fully supported.
  • Samsung Internet: Fully supported.
  • UC Browser for Android: Fully supported.

Browser Support Table

BrowserVersionInitial Support Date
Chrome1.0December 2008
Firefox1.0November 2004
Internet Explorer8.0March 2009
Edge12.0July 2015
Opera4.0June 2000
Safari1.0June 2003

Importance of Browser Support

Ensuring that your web designs are compatible with a wide range of browsers is crucial for providing a consistent and enjoyable user experience. The border-spacing property’s extensive support across both desktop and mobile browsers allows web developers to confidently use it to enhance the visual appeal and readability of tables in their projects.

Tips for Ensuring Compatibility

  • Cross-Browser Testing: Regularly test your web designs in different browsers to ensure that the border-spacing property behaves as expected.
  • Fallbacks: Implement fallbacks for older browsers that may not fully support the border-spacing property. This can include using other CSS properties or JavaScript to achieve similar effects.
  • Consistent Design: Aim for a consistent design across all browsers to maintain a professional and polished look for your web pages.

Conclusion

The border-spacing CSS property is a powerful tool for web developers and designers to control the spacing between table cells, enhancing the visual appeal and readability of tables. Its wide browser compatibility ensures that you can rely on it to create consistent and visually appealing table layouts across various platforms and devices.

By understanding and utilizing the border-spacing property effectively, you can significantly improve the quality of your web design and development projects, providing a better user experience for your visitors.

Related Topics

Exploring related topics can provide you with a broader understanding of table styling in CSS and help you create more effective and visually appealing web designs. Here are some related resources:

  • border-collapse: Control the appearance of table borders by determining if they are shared or separated.
  • border-style: Enhance the visual appeal of your tables by choosing different border styles.
  • The <table> HTML Element: Learn about the HTML <table> element and how to style it using CSS properties like border-spacing.
  • CSS Table Module: Get comprehensive information on CSS properties for styling tables.
  • CSS Borders Tutorial: Learn how to use borders to make your tables look great.
  • CSS Tables Tutorial: Discover various CSS properties and techniques for styling tables, including border-spacing.
  • CSS Padding Property: Add space inside table cells to improve readability.
  • CSS Margin Property: Add space outside table cells for a balanced layout.
  • CSS Grid Layout: Use CSS Grid for complex layouts that might otherwise require tables.
  • CSS Flexbox Layout: Use Flexbox for flexible and responsive layouts.
icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.