Advertisement

How to Create Rounded Table Corners

Tables are a common element in web design used to display data in a structured and organized manner. However, they can often appear rigid and square, not always fitting in with the overall design aesthetic. One way to add some visual interest to tables is to create rounded corners, giving them a softer and more polished look. In this article, we’ll explore different methods to achieve rounded table corners using CSS.


Method 1: Using Border-Radius Property

The simplest and most widely used method to create rounded table corners is by using the border-radius property. This method only requires a few lines of CSS code and works well on most modern browsers.


To get started, we need to select the table element and apply the border-radius property to it. We can specify the value of border-radius in pixels, ems, or percentages depending on our design needs. For example, to create rounded corners on all four sides of a table with a radius of 10px, we can use the following code:

table {

  border-collapse: separate;

  border-spacing: 0;

  border-radius: 10px;

}{codeBox}

 

Note that we also set the border-collapse property to separate and border-spacing to 0 to ensure that the border doesn't overlap or interfere with the table contents.

If we want to apply different radii to each corner of the table, we can use the border-top-left-radius, border-top-right-radius, border-bottom-left-radius, and border-bottom-right-radius properties separately.


CSS

table {

  border-collapse: separate;

  border-spacing: 0;

  border-top-left-radius: 10px;

  border-top-right-radius: 20px;

  border-bottom-left-radius: 30px;

  border-bottom-right-radius: 40px;

}{codeBox}


Method 2: Using Pseudo-Elements
Another way to create rounded table corners is by using pseudo-elements. This method involves adding two pseudo-elements :before and :after to the table element and setting their position to absolute. We can then apply the border-radius property to the pseudo-elements, giving the table a rounded appearance.

CSS
table {
  border-collapse: separate;
  border-spacing: 0;
  position: relative;
}
table:before, table:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
  border-radius: 10px;
}
table:before {
  background-color: #fff;
}
table:after {
  background-color: #f9f9f9;
}{codeBox}
Note that we set the z-index property of the pseudo-elements to -1 to ensure they remain behind the table contents. We also set different background colors for the :before and :after pseudo-elements to create a contrasting effect.


Method 3: Using SVG Background

Another way to create rounded table corners is to use a scalable vector graphic (SVG) as the background of the table. This method is especially useful if you want to create more complex designs for the corners, such as rounded corners with a gradient effect.

Here are the steps to follow:

Create an SVG file with the desired design for the corners. You can use vector graphics software like Adobe Illustrator or Inkscape to create the design. Make sure the SVG file has a transparent background and the same dimensions as the table.

Add the SVG file as the background of the table using CSS. Here's an example code:

CSS
table {
  background-image: url('path/to/svg/file.svg');
  background-repeat: no-repeat;
  background-size: 100% 100%;
}{codeBox}
The background-image property sets the SVG file as the background of the table, while the background-repeat property prevents the image from repeating. The background-size property ensures that the image covers the entire table.

To make the table responsive, you can set the background-size property to cover instead of 100% 100%. This will make the SVG image scale cover the entire table while maintaining its aspect ratio.

table {
  background-image: url('path/to/svg/file.svg');
  background-repeat: no-repeat;
  background-size: cover;
}{codeBox}


Method 4: Border-radius Property
The border-radius property is used to define the radius of the corners of an element's border. The value of the border-radius property can be specified in pixels, ems, or percentages. This property can be used to create rounded corners for a table by setting the border-radius property of the table or table cells.

For example, to create rounded corners for a table, you can add the following CSS code:

table {
border-collapse: collapse;
border-radius: 10px;
}

td, th {
padding: 10px;
border: 1px solid #ccc;
border-radius: 10px;
}{codeBox}
In this code, we set the border-collapse property to collapse, which merges the borders of adjacent cells to create a seamless appearance. We also set the border-radius property to 10px for both the table and its cells.


Method 5: Creating Rounded Corners for Specific Corners
If you want to create rounded corners for specific corners of a table, you can use the border-top-left-radius, border-top-right-radius, border-bottom-left-radius, and border-bottom-right-radius properties.

For example, to create a table with rounded corners only on the top-left and top-right corners, you can use the following CSS code:

table {
border-collapse: collapse;
}

td, th {
padding: 10px;
border: 1px solid #ccc;
}

.top-left-rounded {
border-top-left-radius: 10px;
}

.top-right-rounded {
border-top-right-radius: 10px;
}{codeBox}
In this code, we create a table with the border-collapse property set to collapse. We also set the padding and border properties for the table cells.

We then create two classes, .top-left-rounded and .top-right-rounded, which apply the border-top-left-radius and border-top-right-radius properties, respectively. To apply these classes to specific cells, you can add the class name to the HTML code, like this:

<table>
  <tr>
    <td class="top-left-rounded">Cell 1</td>
    <td class="top-right-rounded">Cell 2</td>
  </tr>
  <tr>
    <td>Cell 3</td>
    <td>Cell 4</td>
  </tr>
</table>{codeBox}