Styling Table Borders With CSS

How to styling table borders with CSS?
Tables are generally used to display data in rows and columns. Borders can be used to make this image more beautiful. We can style table borders using CSS. Below I will explain how we style table borders using various CSS properties.

Table Borders

To add a border to a table with CSS, use the border property. The border property applies a border to the entire table.


table{
    border: 1px solid black;
}

This will give your table a simple black border with a 1px thickness.
Example:

City Country
Paris France
Milano İtaly

Styling Table Cell Borders

To style the borders of individual table cells (<td> or <th>), you can target the td and th elements specifically.


table{
    border: 1px solid black;
}
th, td {
    border: 1px solid red;
}

Example:

City Country
Paris France
Milano İtaly

Collapse Table Borders

The border-collapse property controls how the borders are displayed in the table.


table{
    border-collapse: collapse; 
}
table,th,td{
    border: 1px solid black;
}

Example:

City Country
Paris France
Milano İtaly

border-collapse: collapse; property causes the table borders to collapse into a single border where the cells meet.

Let’s summarize from beginning to end. If you want to give a border property to a table, the table{border: 1px solid;} property is used. If you want to add a border to all cells along with the table, the table,th,td{ border: 1px solid;} property is used. If you want to collapse the table borders, the table{border-collapse: collapse;} property is used.