Monday, January 20, 2025
HomeTechRounded table corners CSS only

Rounded table corners CSS only

You can create rounded table corners in CSS by applying the border-radius property to the <table> element. Here’s how you can do it:

CSS Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Rounded Table Corners</title>
  <style>
    table {
      border-collapse: separate; /* Important for rounded corners */
      border-spacing: 0; /* Removes gaps between table cells */
      border: 2px solid #333; /* Table border */
      border-radius: 15px; /* Rounds the corners */
      overflow: hidden; /* Ensures content fits within rounded borders */
    }

    th, td {
      padding: 10px;
      border: 1px solid #333; /* Adds cell borders */
    }

    th {
      background-color: #f4f4f4;
    }

    td {
      background-color: #fff;
    }
  </style>
</head>
<body>
  <table>
    <thead>
      <tr>
        <th>Header 1</th>
        <th>Header 2</th>
        <th>Header 3</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Row 1, Col 1</td>
        <td>Row 1, Col 2</td>
        <td>Row 1, Col 3</td>
      </tr>
      <tr>
        <td>Row 2, Col 1</td>
        <td>Row 2, Col 2</td>
        <td>Row 2, Col 3</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

Key Points

  1. border-radius:
    • This property is applied to the <table> to round its corners.
    • Example: border-radius: 15px;.
  2. border-collapse: separate:
    • Ensures that the border-radius works. If you use border-collapse: collapse, rounded corners won’t be applied.
  3. overflow: hidden:
    • Prevents content from spilling out of the rounded corners.
  4. border-spacing: 0:
    • Removes spacing between cells to keep the table neat.
See also  Read a file line by line in Python

Optional Enhancements

If you want rounded corners only on specific sides, you can adjust the border-radius values:

  • Top corners:
    border-radius: 15px 15px 0 0;
    
  • Bottom corners:
    border-radius: 0 0 15px 15px;
    

Let me know if you’d like further customizations!

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x