Skip to content

Using the Cell spacing Attribute in table

The cellspacing attribute in HTML tables is used to specify the amount of space between cells. This attribute creates space between adjacent cells, both vertically and horizontally, providing visual separation between them.

Basic Usage of cellspacing

The cellspacing attribute is applied to the <table> element, and its value is specified in pixels. This value determines the amount of space between cells in the table.

Example of cellspacing

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Table with Cellspacing</title>

</head>

<body>

    <h1>Table with Cellspacing Attribute</h1>

    <table border=”1″ cellspacing=”10″>

        <tr>

            <th>Header 1</th>

            <th>Header 2</th>

            <th>Header 3</th>

        </tr>

        <tr>

            <td>Data 1</td>

            <td>Data 2</td>

            <td>Data 3</td>

        </tr>

        <tr>

            <td>Data 4</td>

            <td>Data 5</td>

            <td>Data 6</td>

        </tr>

    </table>

</body>

</html>

In this example, the cellspacing attribute is set to 10, which adds 10 pixels of space between cells in the table, both vertically and horizontally.

Cellspacing with CSS

While the cellspacing attribute is convenient and easy to use, modern web development practices recommend using CSS for styling, as it provides more flexibility and control over the appearance of HTML elements. Using CSS, you can achieve the same effect as cellspacing by applying margin or padding to <th> and <td> elements.

Example Using CSS for Cell Spacing

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>

    <title>Table with CSS Cell Spacing</title>

    <style>

        table {

            border-collapse: separate; /* Ensures cells are separated */

            border-spacing: 10px; /* Equivalent to cellspacing=”10″ */

            width: 100%; /* Optional: Makes the table take up full width */

        }

        th, td {

            border: 1px solid black;

            padding: 10px; /* Padding to separate content from cell border */

            text-align: left; /* Optional: Aligns text to the left */

        }

    </style>

</head>

<body>

    <h1>Table with CSS Cell Spacing</h1>

    <table>

        <tr>

            <th>Header 1</th>

            <th>Header 2</th>

            <th>Header 3</th>

        </tr>

        <tr>

            <td>Data 1</td>

            <td>Data 2</td>

            <td>Data 3</td>

        </tr>

        <tr>

            <td>Data 4</td>

            <td>Data 5</td>

            <td>Data 6</td>

        </tr>

    </table>

</body>

</html>

In this CSS example:

  • The border-collapse: separate; property is used on the table element to ensure that the cells are separated.
  • The border-spacing: 10px; property is applied to specify the amount of space between cells.
  • Padding is added to <th> and <td> elements to separate the content from the cell border, providing additional spacing.

Comparison and Recommendations

  • Using cellspacing Attribute: Quick and simple for basic tables, suitable for small projects or quick prototypes.
  • Using CSS for Cell Spacing: Recommended for modern web development due to greater flexibility, maintainability, and compatibility with other CSS styles.

By using CSS, you can easily customize the spacing between cells and achieve a more polished and tailored look for your table. Additionally, CSS provides comprehensive control over the styling of your table, making it suitable for complex and responsive web designs.