Border for Table

To display border around table, table headings, and table cells using CSS, set CSS border property of this table, table headings (th), and table cells (td) with the required border value.

By default, table does not display any border.

</>
Copy
table, th, td {
  border: 1px solid red;
}

Examples

1. Set border for table and its contents

In the following example, we take a table with two columns, and three rows; and set a border of width 1px, solid style, and red color.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <style>
    table, th, td {
      border: 1px solid red;
    }
  </style>
</head>
<body>
  <table>
    <thead>
      <tr>
        <th>Fruit</th>
        <th>Quantity</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Apple</td>
        <td>32</td>
      </tr>
      <tr>
        <td>Banana</td>
        <td>48</td>
      </tr>
      <tr>
        <td>Cherry</td>
        <td>75</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

2. Border collapse

In the output of previous example, we see double lines, since, there is a border for each cell in the table. We can collapse this double lines into single line using border-collapse property as shown in the following.

</>
Copy
table, th, td {
  border: 1px solid red;
  border-collapse: collapse;
}

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <style>
    table, th, td {
      border: 1px solid red;
      border-collapse: collapse;
    }
  </style>
</head>
<body>
  <table>
    <thead>
      <tr>
        <th>Fruit</th>
        <th>Quantity</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Apple</td>
        <td>32</td>
      </tr>
      <tr>
        <td>Banana</td>
        <td>48</td>
      </tr>
      <tr>
        <td>Cherry</td>
        <td>75</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

Conclusion

In this CSS Tutorial, we learned how to use CSS border property to display border around table and its contents, with examples.