Animate CSS border-spacing property

The CSS border-spacing property controls the distance between the borders of adjacent table cells. When animated, it creates a dynamic effect where the spacing between cells changes smoothly over time.

Syntax

selector {
    border-spacing: value;
    animation: animation-name duration timing-function;
}

Example

The following example animates the border-spacing property from 0px to 20px −

<!DOCTYPE html>
<html>
<head>
<style>
    table {
        border-collapse: separate;
        border-spacing: 0px;
        border: 2px solid #333;
        animation: spacingAnimation 3s infinite alternate;
    }
    
    th, td {
        border: 1px solid #4CAF50;
        padding: 10px;
        text-align: center;
        background-color: #f9f9f9;
    }
    
    th {
        background-color: #4CAF50;
        color: white;
    }
    
    @keyframes spacingAnimation {
        0% {
            border-spacing: 0px;
        }
        100% {
            border-spacing: 20px;
        }
    }
</style>
</head>
<body>
    <h2>Animated Border Spacing</h2>
    <table>
        <tr>
            <th>Subject</th>
            <th>Student</th>
            <th>Marks</th>
        </tr>
        <tr>
            <td>Maths</td>
            <td>Amit</td>
            <td>98</td>
        </tr>
        <tr>
            <td>Science</td>
            <td>Sachin</td>
            <td>99</td>
        </tr>
    </table>
</body>
</html>
A table with green headers and borders appears on the page. The spacing between table cells animates smoothly from 0px to 20px and back, creating a breathing effect where the cells move apart and come together continuously.

Key Points

When animating border-spacing, remember these important points −

  • The table must have border-collapse: separate for border-spacing to work
  • You can animate both horizontal and vertical spacing using two values
  • The animation creates smooth transitions between different spacing values

Conclusion

Animating the border-spacing property creates engaging visual effects for tables. It works only with separated borders and can enhance user interface designs with smooth spacing transitions.

Updated on: 2026-03-15T13:26:26+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements