CSS Border Collapse

The CSS border-collapse property is used to specify whether the borders of a table should be collapsed into a single border or separated. It is primarily used in tables to control the behavior of borders between table cells.

Key Points on CSS Border Collapse:

Syntax for CSS Border Collapse:

Syntax Example


        /* Border collapse syntax */
        .table {
            border-collapse: collapse;
               }
        
        /* Separate borders syntax */
        .table {
            border-collapse: separate;
               }

Examples of CSS Border Collapse:

The following examples demonstrate the usage of the border-collapse property:

Code Example: Border Collapse


            .table {
            border-collapse: collapse;
            width: 100%;
                   }
            th, td {
            border: 1px solid black;
            padding: 10px;
            text-align: center;
        }

Output

Header 1 Header 2
Row 1, Cell 1 Row 1, Cell 2
Row 2, Cell 1 Row 2, Cell 2

Code Example: Border Separate


            .table {
            border-collapse: separate;
            border-spacing: 10px;
            width: 100%;
                   }
        
            th, td {
            border: 1px solid black;
            padding: 10px;
            text-align: center;
                   }

Output

Header 1 Header 2
Row 1, Cell 1 Row 1, Cell 2
Row 2, Cell 1 Row 2, Cell 2

Common Border Collapse Properties:

Using border-collapse: collapse; is typically preferred for tables that need a neat, compact look without any extra spacing between cells. The border-collapse: separate; setting allows for more flexible table layouts with space between table cells.