403

I am trying to make a table with rounded corners using the CSS border-radius property. The table styles I'm using look something like this:

table {
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px
}

Here's the problem. I also want to set the border-collapse:collapse property, and when that is set border-radius no longer works. Is there a CSS-based way I can get the same effect as border-collapse:collapse without actually using it?

It seems that a large part of the problem is that setting the table to have rounded corners does not affect the corners of the corner td elements. If the table was all one color, this wouldn't be a problem since I could just make the top and bottom td corners rounded for the first and last row respectively. However, I am using different background colors for the table to differentiate the headings and for striping, so the inner td elements would show their rounded corners as well.

Surrounding the table with another element with round corners doesn't work because the table's square corners "bleed through."

Specifying border width to 0 doesn't collapse the table.

Bottom td corners still square after setting cellspacing to zero.

The tables are generated in PHP, so I could just apply a different class to each of the outer th/tds and style each corner separately. I'd rather not do this, since it's not very elegant and a bit of a pain to apply to multiple tables, so please keep suggestions coming.

I'd like to do this without JavaScript.

TylerH
  • 20,799
  • 66
  • 75
  • 101
vamin
  • 2,178
  • 6
  • 26
  • 30
  • 3
    Couldn't you wrap the table in a div, set border-radius and "overflow: hidden" on the div? I just tested and that works fine, unless you need scrolling/expanding in a div that has fix width/height or its parents that do. – Ian Aug 13 '12 at 18:05

27 Answers27

308

I figured it out. You just have to use some special selectors.

The problem with rounding the corners of the table was that the td elements didn't also become rounded. You can solve that by doing something like this:

table tr:last-child td:first-child {
    border: 2px solid orange;
    border-bottom-left-radius: 10px;
}
    
table tr:last-child td:last-child {
    border: 2px solid green;
    border-bottom-right-radius: 10px;
}
<table>
  <tbody>
    <tr>
      <td>1</td>
      <td>2</td>
    </tr>
    <tr>
      <td>3</td>
      <td>4</td>
    </tr>
  </tbody>
</table>

Now everything rounds properly, except that there's still the issue of border-collapse: collapse breaking everything.

A workaround is to add border-spacing: 0 and leave the default border-collapse: separate on the table.

mfluehr
  • 2,832
  • 2
  • 23
  • 31
vamin
  • 2,178
  • 6
  • 26
  • 30
  • 112
    Instead of mucking around with the HTML, why not add `border-spacing: 0;` as a border style? – Ramon Tayag Mar 08 '11 at 03:43
  • 3
    I was having an issue setting the background color of the TR tag instead of the TD tag. Be sure if you're striping your table that you're setting the background color of the TD not the TR. – Will Shaver Aug 22 '11 at 21:08
  • Well what happens if you **have to** use background-color on the TR? Is it possible at all? – Mohoch Oct 09 '11 at 15:21
  • tr.even>td { background-color:rgb(200,200,200); } ? – Chaosphere2112 Apr 24 '12 at 03:36
  • Ramon is correct. Then simply edit border-radius for :first-child and :last-child where needed. – worked May 16 '12 at 23:10
  • You could also use `table thead tr t[h|d] {border-top-[left|right]-radius: 1mm;}` and `table tfoot tr t[h|d] {border-bottom-[left|right]-radius: 1mm;}` – Richard Ayotte Jan 07 '13 at 02:25
  • 1
    Just adding `border-spacing: 0;` like Ramon recommend fixed it for me. Make sure you're adding the `border-radius` and `border-spacing` styles to the `` or `` elements, not `` or ``. – Gavin Oct 03 '13 at 18:36
  • 1
    I am using bootstrap and i have find the solution by using Ramon's advice, like this: `border-spacing: 0; border-collapse: separate;` – Caner SAYGIN Apr 13 '19 at 15:41
  • This solution does not solve rounded borders collapsed TDs – neoswf Apr 26 '23 at 22:15
151

The following method works (tested in Chrome) by using a box-shadow with a spread of 1px instead of a "real" border.

    table {
        border-collapse: collapse;
        border-radius: 30px;
        border-style: hidden; /* hide standard table (collapsed) border */
        box-shadow: 0 0 0 1px #666; /* this draws the table border  */ 
    }

    td {
        border: 1px solid #ccc;
    }
<table>
  <thead>
    <tr>
      <th>Foo</th>
      <th>Bar</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Baz</td>
      <td>Qux</td>
    </tr>
    <tr>
      <td>Life is short</td>
      <td rowspan="3">and</td>
    </tr>
    <tr>
      <td>Love</td>
    </tr>
    <tr>
      <td>is always over</td>
    </tr>
    <tr>
      <td>In the</td>
      <td>Morning</td>
    </tr>
  </tbody>
</table>
Dai
  • 141,631
  • 28
  • 261
  • 374
cmrd.Kaash
  • 1
  • 1
  • 2
  • 3
  • 6
    This is the only thing that has worked for me. It is difficult to get the right color on the table border though. – Thomas Ahle Mar 18 '11 at 23:40
  • 3
    It is not usable _if_ your table has different background color than the surrounding area. – g.pickardou Oct 29 '13 at 09:49
  • 4
    @g.pickardou that problem can be handled by adding 'overflow: hidden' onto table element. – Val Aug 04 '16 at 06:56
  • box-shadow makes the table larger and so the sides get cut off now. – Ray May 04 '20 at 16:20
  • Its extremely hard to use, especially if you need to use rounded borders tds. It might be suitable only for a rounded able, or rounded trs – neoswf Apr 26 '23 at 22:16
90

If you want a CSS-only solution (no need to set cellspacing=0 in the HTML) that allows for 1px borders (which you can't do with the border-spacing: 0 solution), I prefer to do the following:

  • Set a border-right and border-bottom for your table cells (td and th)
  • Give the cells in the first row a border-top
  • Give the cells in the first column a border-left
  • Using the first-child and last-child selectors, round the appropriate corners for the table cells in the four corners.

See a demo here.

Given the following HTML:

See example below:

   

table {
  border-collapse: separate;
  border-spacing: 0;
  min-width: 350px;
}
table tr th,
table tr td {
  border-right: 1px solid #bbb;
  border-bottom: 1px solid #bbb;
  padding: 5px;
}

table tr th:first-child,
table tr td:first-child {
  border-left: 1px solid #bbb;
}
table tr th {
  background: #eee;
  text-align: left;
  border-top: solid 1px #bbb;
}

/* top-left border-radius */
table tr:first-child th:first-child {
  border-top-left-radius: 6px;
}

/* top-right border-radius */
table tr:first-child th:last-child {
  border-top-right-radius: 6px;
}

/* bottom-left border-radius */
table tr:last-child td:first-child {
  border-bottom-left-radius: 6px;
}

/* bottom-right border-radius */
table tr:last-child td:last-child {
  border-bottom-right-radius: 6px;
}
<div>
    <table>
        <tr>
            <th>item1</th>
            <th>item2</th>
        </tr>
        <tr>
            <td>item1</td>
            <td>item2</td>
        </tr>
        <tr>
            <td>item1</td>
            <td>item2</td>
        </tr>
        <tr>
            <td>item1</td>
            <td>item2</td>
        </tr>
    </table>
</div>
user966939
  • 692
  • 8
  • 27
vxsx
  • 448
  • 5
  • 12
  • 1
    Please write answers which include (permanent) code. If there is a lot of code to the answer, just post the relevant bits and an explanation of why they are relevant. – Samuel Harmer Jan 20 '12 at 16:12
  • 3
    This is a great solution, but it was a bit hard to read. I rewrote some of the style rules and added an explanation of the code, so hopefully that helps. – Michael Martin-Smucker Oct 16 '12 at 15:13
  • apply a radius to the table too, or else it looks weird when you apply a background to the table itself. – goat Jun 19 '13 at 03:29
  • What does the `table.Info` class got to do with anything? – Pylinux Dec 22 '16 at 06:43
36

Have you tried using table{border-spacing: 0} instead of table{border-collapse: collapse} ???

Cesar
  • 4,076
  • 8
  • 44
  • 68
  • Thank you, this let me do what I needed to do (which involved a series of TH elements at the top outside of the 'rounded corner' box containing all the TDs below) – RonLugge Feb 22 '12 at 20:07
  • 16
    The problem with `border-spacing: 0` is that you can't have a 1px border, right? Because the borders stack up instead of collapsing. – Michael Martin-Smucker Oct 17 '12 at 12:17
  • 3
    `border-collapse: separate; border-spacing: 0; border-radius: 10px; overflow: hidden;` yielded exactly what I needed. – Milad.Nozari Jul 27 '18 at 09:43
27

You'll probably have to put another element around the table and style that with a rounded border.

The working draft specifies that border-radius does not apply to table elements when the value of border-collapse is collapse.

fish2000
  • 4,289
  • 2
  • 37
  • 76
user59200
  • 384
  • 2
  • 2
  • 1
    That was something I considered as well, but if I create a div to surround the table and set it to have rounded corners, the square table corners still bleed through. See the newly-posted example. – vamin Mar 09 '09 at 23:21
  • The best compromise I could find was adding a THEAD block to the table and applying the grey background to it (with #eee on the table itself). The header cells overflowed behind the TABLE's border instead in front of it. Then I increased the table border to 3px to hide the overflow. – user59200 Mar 10 '09 at 01:25
  • 3
    @vamin "bleed through"- not if you use `overflow:hidden;` – Brian McCutchon May 21 '13 at 00:19
  • So in this situation everyone can use my solution from bottom of these page http://b2n.ir/table-radius – AmerllicA Oct 24 '17 at 11:48
22

As Ian said, the solution is to nest the table inside a div and set it like that:

.table_wrapper {
  border-radius: 5px;
  overflow: hidden;
}

With overflow:hidden, the square corners won't bleed through the div.

Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
Chris
  • 1
  • 1
  • 3
  • 4
    Keep in mind, whoever wants to use it, that with `overflow: hidden` any popover/tooltip will get clipped by the wrapper dimensions. – user776686 Mar 16 '17 at 13:48
8

Here is a way:

div {
  border: 2px solid red;
  overflow: hidden;
  border-radius: 14px;
  transform: rotate(0deg);
}
table {
  border-spacing: 0;
  background-color: blue;
  height: 100%;
  width: 100%;
}
<div>
  <table>
    <tr>
      <td><br></td> 
    </tr>
  </table>
</div>

Or

    div {
      ...
      overflow: hidden;
      border-radius: 14px;
      position: relative;
      z-index: 1;
    }
        
        
Bill-G
  • 305
  • 4
  • 12
luow
  • 11
  • 1
  • 4
  • This is the only solution that worked for me (in Safari, where headers have background and are position:sticky). Thanks! – Dziad Borowy Feb 11 '23 at 23:01
8

To the best of my knowledge, the only way you could do it would be to modify all the cells like so:

table td {
  border-right-width: 0px;
  border-bottom-width: 0px;
}

And then to get the border on the bottom and right back

table tr td:last-child {
  border-right-width: 1px;
}
table tr:last-child td {
  border-bottom-width: 1px;
}

:last-child is not valid in ie6, but if you are using border-radius I assume you don't care.

EDIT:

After looking at your example page, it appears that you may be able to work around this with cell spacing and padding.

The thick gray borders you are seeing are actually the background of the table (you can see this clearly if you change the border color to red). If you set the cellspacing to zero (or equivalently: td, th { margin:0; }) the grey "borders" will disappear.

EDIT 2:

I can't find a way to do this with only one table. If you change your header row to a nested table, you might possibly be able to get the effect you want, but it'll be more work, and not dynamic.

Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
Joel
  • 19,175
  • 2
  • 63
  • 83
  • I've added an example with cellspacing=0, and it's much closer. The undesireable borders disappear, but the bottom corners still bleed out. – vamin Mar 09 '09 at 23:38
  • Thanks again for your help. The tables are generated in php, so I'm thinking if there isn't an elegent solution proposed I'll just assign a class to each corner th/td and style them separately. – vamin Mar 10 '09 at 00:07
7

Actually you can add your table inside a div as its wrapper. and then assign these CSS codes to wrapper:

.table-wrapper {
  border: 1px solid #f00;
  border-radius: 5px;
  overflow: hidden;
}

table {
  border-collapse: collapse;
}
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
6

I tried a workaround using the pseudo elements :before and :after on the thead th:first-child and thead th:last-child

In combination with wrapping the table with a <div class="radius borderCCC">

table thead th:first-child:before{ 
    content:" ";
    position:absolute;
    top:-1px;
    left:-1px;
    width:15px;
    height:15px;
    border-left:1px solid #ccc;
    border-top:1px solid #ccc; 
    -webkit-border-radius:5px 0px 0px;
}
table thead th:last-child:after{ 
    content:" "; 
    position:absolute; 
    top:-1px;
    right:-1px; 
    width:15px;
    height:15px;
    border-right:1px solid #ccc;
    border-top:1px solid #ccc;
    -webkit-border-radius:0px 5px 0px 0px;
}

see jsFiddle

Works for me in chrome (13.0.782.215) Let me know if this works for you in other browsers.

adardesign
  • 33,973
  • 15
  • 62
  • 84
5

I just wrote a crazy set of CSS for this that seems to work perfectly:

    table {
      border-collapse: separate;
      border-spacing: 0;
      width: 100%;
    }
    table td,
    table th {
      border-right: 1px solid #CCC;
      border-top: 1px solid #CCC;
      padding: 3px 5px;
      vertical-align: top;
    }
    table td:first-child,
    table th:first-child {
      border-left: 1px solid #CCC;
    }
    table tr:last-child td,
    table tr:last-child th {
      border-bottom: 1px solid #CCC;
    }
    table thead + tbody tr:first-child td {
      border-top: 0;
    }
    table thead td,
    table th {
      background: #EDEDED;
    }
    
    /* complicated rounded table corners! */
    table thead:first-child tr:last-child td:first-child {
      border-bottom-left-radius: 0;
    }
    table thead:first-child tr:last-child td:last-child {
      border-bottom-right-radius: 0;
    }
    table thead + tbody tr:first-child td:first-child {
      border-top-left-radius: 0;
    }
    table thead + tbody tr:first-child td:last-child {
      border-top-right-radius: 0;
    }
    table tr:first-child td:first-child,
    table thead tr:first-child td:first-child {
      border-top-left-radius: 5px;
    }
    table tr:first-child td:last-child,
    table thead tr:first-child td:last-child {
      border-top-right-radius: 5px;
    }
    table tr:last-child td:first-child,
    table thead:last-child tr:last-child td:first-child {
      border-bottom-left-radius: 5px;
    }
    table tr:last-child td:last-child,
    table thead:last-child tr:last-child td:last-child {
      border-bottom-right-radius: 5px;
    }
<table>
  <thead>
    <tr>
      <th>
        Table Head
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        Table Data
      </td>
    </tr>
  </tbody>
</table>
/* end complicated rounded table corners !*/
Bill-G
  • 305
  • 4
  • 12
JacobTheDev
  • 17,318
  • 25
  • 95
  • 158
5

The given answers only work when there are no borders around the table, which is very limiting!

I have a macro in SASS to do this, which fully supports external and internal borders, achieving the same styling as border-collapse: collapse without actually specifying it.

Tested in FF/IE8/Safari/Chrome.

Gives nice rounded borders in pure CSS in all browsers but IE8 (degrades gracefully) since IE8 doesn't support border-radius :(

Some older browsers may require vendor prefixes to work with border-radius, so feel free to add those prefixes to your code as necessary.

This answer is not the shortest - but it works.

.roundedTable {
  border-radius: 20px / 20px;
  border: 1px solid #333333;
  border-spacing: 0px;
}
.roundedTable th {
  padding: 4px;
  background: #ffcc11;
  border-left: 1px solid #333333;
}
.roundedTable th:first-child {
  border-left: none;
  border-top-left-radius: 20px;
}
.roundedTable th:last-child {
  border-top-right-radius: 20px;
}
.roundedTable tr td {
  border: 1px solid #333333;
  border-right: none;
  border-bottom: none;
  padding: 4px;
}
.roundedTable tr td:first-child {
  border-left: none;
}

To apply this style simply change your

<table>

tag to the following:

<table class="roundedTable">

and be sure to include the above CSS styles in your HTML.

Hope this helps.

Michael Martin-Smucker
  • 11,927
  • 7
  • 31
  • 36
robbie613
  • 675
  • 1
  • 10
  • 8
  • You don't need prefixes for border-radius anymore, expect for FF 3.6 (-moz). Also, -khtml is certainly no longer needed. – Jonatan Littke Mar 28 '13 at 11:02
  • @JonatanLittke, you can always edit the answer if you think it can be improved. I removed all the prefixes and added a link to caniuse.com so people can make their own decisions about prefixes for `border-radius`. – Michael Martin-Smucker Apr 18 '13 at 12:11
4

For a bordered and scrollable table, use this (replace variables, $ starting texts)

If you use thead, tfoot or th, just replace tr:first-child and tr-last-child and td with them.

#table-wrap {
  border: $border solid $color-border;
  border-radius: $border-radius;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
table td { border: $border solid $color-border; }
table td:first-child { border-left: none; }
table td:last-child { border-right: none; }
table tr:first-child td { border-top: none; }
table tr:last-child td { border-bottom: none; }
table tr:first-child td:first-child { border-top-left-radius: $border-radius; }
table tr:first-child td:last-child { border-top-right-radius: $border-radius; }
table tr:last-child td:first-child { border-bottom-left-radius: $border-radius; }
table tr:last-child td:last-child { border-bottom-right-radius: $border-radius; }

HTML:

<div id=table-wrap>
  <table>
    <tr>
       <td>1</td>
       <td>2</td>
    </tr>
    <tr>
       <td>3</td>
       <td>4</td>
    </tr>
  </table>
</div>
brauliobo
  • 5,843
  • 4
  • 29
  • 34
4

Found this answer after running into the same problem, but found it's pretty simple: just give the table overflow:hidden

No need for a wrapping element. Granted, I don't know if this would have worked 7 years ago when the question was initially asked, but it works now.

Akexis
  • 129
  • 9
  • 1
    This is a clever solution but it also "removes" the actual border as well. On Chrome the right border and bottom border of the table disappear and all rounded corners have no border. – Alex Jorgenson Oct 31 '16 at 17:19
4

I had the same problem. remove border-collapse entirely and use: cellspacing="0" cellpadding="0" in the html document. example:

<table class="top_container" align="center" cellspacing="0" cellpadding="0">
Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
lars
  • 1
  • 1
  • 1
3

Solution with border-collapse:separate for table and display:inline-table for tbody and thead.

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0px;
  background: transparent;   
}
table thead {
  display: inline-table;
  width: 100%;
  background: #fc0 url(../images/bg-heading.png) repeat-x 0% 0;
  -webkit-border-top-left-radius: 7px;
  -moz-border-radius-topleft: 7px;
  -webkit-border-top-right-radius: 7px;
  -moz-border-radius-topright: 7px;
    border-radius: 7px 7px 0px 0px;
  padding: 1px;
  padding-bottom: 0;
}

table tbody {
  border: 1px solid #ddd;
  display: inline-table;
  width: 100%;
  border-top: none;        
}
Tommer
  • 1
  • 1
  • there is no reason to make this answer community wiki. Doing this makes it so that you will get no reputation from your answer. – tacaswell Nov 28 '12 at 18:55
3

I am new with HTML and CSS and I was also looking for solution for this, here what I find.

table,th,td {
   border: 1px solid black;
   border-spacing: 0
}
/* add border-radius to table only*/
table {
   border-radius: 25px    
}
/* then add border-radius to top left border of left heading cell */
th:first-child {
   border-radius: 25px 0 0 0
}
/* then add border-radius to top right border of right heading cell */
th:last-child {
   border-radius: 0 25px 0 0
}
/* then add border-radius to bottom left border of left cell of last row */
tr:last-child td:first-child {
   border-radius: 0 0 0 25px
}
/* then add border-radius to bottom right border of right cell of last row */
tr:last-child td:last-child {
   border-radius: 0 0 25px 0
}

I try it, guess what it works :)

Ahmed Mostafa
  • 918
  • 1
  • 12
  • 20
3

I see a lot of weird hacks and workarounds so I would like to suggest my solution for creating a table with border-radius and the same visual effect as border: collapse; by simply targeting nested rows and cells to turn borders off.

You can get more in-depth to suit your needs using other pseudo selectors like first-child, etc, but this is the minimal solution:

table {
  width: 100%;
  border-spacing: 0;
  border-radius: 4px;
  border: 1px solid #ccc;
}

th, td {
    border-right: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
}

th:last-child, td:last-child  {
    border-right: none;
}

tr:last-child td {
    border-bottom: none;
}
<table>
  <thead>
    <tr>
      <th>Company</th>
      <th>Contact</th>
      <th>Country</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Alfreds Futterkiste</td>
      <td>Maria Anders</td>
      <td>Germany</td>
    </tr>
    <tr>
      <td>Centro comercial Moctezuma</td>
      <td>Francisco Chang</td>
      <td>Mexico</td>
    </tr>
  </tbody>
</table>
Jared Rice
  • 413
  • 3
  • 9
2

I started experiment with "display" and I found that: border-radius, border, margin, padding, in a table are displayed with:

display: inline-table;

For example

table tbody tr {
  display: inline-table;
  width: 960px; 
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}

But we need set a width of every column

tr td.first-column {
  width: 100px;
}
tr td.second-column {
  width: 860px;
}
Juan Sosa
  • 5,262
  • 1
  • 35
  • 41
Astro
  • 1
  • 2
2

Here is a solution using SCSS. It creates a table with rounded corners and bordered cells.

This solution uses the approach from @Ramon Tayag. The key is to use border-spacing: 0, as he points out.

$line: 1px solid #979797;
$radius: 5px;

table {
  border: $line;
  border-radius: $radius;
  border-spacing: 0;

  th,
  tr:not(:last-child) td {
    border-bottom: $line;
  }

  th:not(:last-child),
  td:not(:last-child) {
    border-right: $line;
  }
}
mfluehr
  • 2,832
  • 2
  • 23
  • 31
pearpages
  • 18,703
  • 1
  • 26
  • 27
2

Here is a recent example of how to implement a table with rounded-corners from http://medialoot.com/preview/css-ui-kit/demo.html. It's based on the special selectors suggested by Joel Potter above. As you can see, it also includes some magic to make IE a little happy. It includes some extra styles to alternate the color of the rows:

table-wrapper {
  width: 460px;
  background: #E0E0E0;
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#E9E9E9', endColorstr='#D7D7D7');
  background: -webkit-gradient(linear, left top, left bottom, from(#E9E9E9), to(#D7D7D7));
  background: -moz-linear-gradient(top, #E9E9E9, #D7D7D7);
  padding: 8px;
  -webkit-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
  -moz-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
  -o-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
  -khtml-box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
  box-shadow: inset 0px 2px 2px #B2B3B5, 0px 1px 0 #fff;
  -webkit-border-radius: 10px;
  /*-moz-border-radius: 10px; firefox doesn't allow rounding of tables yet*/
  -o-border-radius: 10px;
  -khtml-border-radius: 10px;
  border-radius: 10px;
  margin-bottom: 20px;
}
.table-wrapper table {
  width: 460px;
}
.table-header {
  height: 35px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  text-align: center;
  line-height: 34px;
  text-decoration: none;
  font-weight: bold;
}
.table-row td {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  text-align: left;
  text-decoration: none;
  font-weight: normal;
  color: #858585;
  padding: 10px;
  border-left: 1px solid #ccc;
  -khtml-box-shadow: 0px 1px 0px #B2B3B5;
  -webkit-box-shadow: 0px 1px 0px #B2B3B5;
  -moz-box-shadow: 0px 1px 0px #ddd;
  -o-box-shadow: 0px 1px 0px #B2B3B5;
  box-shadow: 0px 1px 0px #B2B3B5;
}
tr th {
  border-left: 1px solid #ccc;
}
tr th:first-child {
 -khtml-border-top-left-radius: 8px;
  -webkit-border-top-left-radius: 8px;
  -o-border-top-left-radius: 8px;
  /*-moz-border-radius-topleft: 8px; firefox doesn't allow rounding of tables yet*/
  border-top-left-radius: 8px;
  border: none;
}
tr td:first-child {
  border: none;
}
tr th:last-child {
  -khtml-border-top-right-radius: 8px;
  -webkit-border-top-right-radius: 8px;
  -o-border-top-right-radius: 8px;
  /*-moz-border-radius-topright: 8px; firefox doesn't allow rounding of tables yet*/
  border-top-right-radius: 8px;
}
tr {
  background: #fff;
}
tr:nth-child(odd) {
  background: #F3F3F3;
}
tr:nth-child(even) {
  background: #fff;
}
tr:last-child td:first-child {
  -khtml-border-bottom-left-radius: 8px;
  -webkit-border-bottom-left-radius: 8px;
  -o-border-bottom-left-radius: 8px;
  /*-moz-border-radius-bottomleft: 8px; firefox doesn't allow rounding of tables yet*/
  border-bottom-left-radius: 8px;
}
tr:last-child td:last-child {
  -khtml-border-bottom-right-radius: 8px;
  -webkit-border-bottom-right-radius: 8px;
  -o-border-bottom-right-radius: 8px;
  /*-moz-border-radius-bottomright: 8px; firefox doesn't allow rounding of tables yet*/
  border-bottom-right-radius: 8px;
}
Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
Mac Cowell
  • 256
  • 2
  • 5
1

easiest way...

table {
 border-collapse: inherit;
 border: 1px solid black;
 border-radius: 5px;
}
noe
  • 194
  • 1
  • 9
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Dima Kozhevin Jul 30 '20 at 06:44
0

Some of the other answers are good, but none of them consider you using thead, tbody and tfoot. Or cases, when you can either combination of those. And when you apply them, you can get some unnecessary rounding or borders. Thus I tried adjusting css-only answer from @NullUserException and this is what I got:

table {
    border-radius: 5px;
    border-width: 2px;
    border-style: solid;
    border-color: darkgreen;
    border-spacing: 0;
    border-collapse: separate;
    width: 100%;
}
table tr td,
table tr th {
    border-right-width: 2px;
    border-right-style: solid;
    border-right-color: darkgreen;
    border-bottom-width: 2px;
    border-bottom-style: solid;
    border-bottom-color: darkgreen;
}
table tr th:last-child,
table tr td:last-child {
    border-right-width: 2px;
    border-right-style: none;
    border-right-color: darkgreen;
}
table tr:last-child td,
table tr:last-child th {
    border-bottom-width: 2px;
    border-bottom-style: none;
    border-bottom-color: darkgreen;
}
/* top-left border-radius */
table :not(tfoot) tr:first-child th:first-child,
table :not(tfoot) tr:first-child td:first-child {
    border-top-left-radius: 5px;
    border-bottom-left-radius: 0;
}

/* top-right border-radius */
table :not(tfoot) tr:first-child th:last-child,
table :not(tfoot) tr:first-child td:last-child {
    border-top-right-radius: 5px;
    border-bottom-right-radius: 0;
}

/* bottom-left border-radius */
table :not(thead) tr:last-child th:first-child,
table :not(thead) tr:last-child td:first-child {
    border-bottom-left-radius: 5px;
}

/* bottom-right border-radius */
table :not(thead) tr:last-child th:last-child,
table :not(thead) tr:last-child td:last-child{
    border-bottom-right-radius: 5px;
}

/*Handle thead and tfoot borders*/
table thead tr:first-child th,
table thead tr:first-child td {
  border-top-style: none;
}
table thead tr:last-child th,
table thead tr:last-child td {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: darkgreen;
}
table tfoot tr:last-child th,
table tfoot tr:last-child td {
  border-bottom-style: none;
}
table tfoot tr:first-child th,
table tfoot tr:first-child td {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: darkgreen;
}
table tr:first-child th,
table tr:first-child td {
  border-top-style: none;
}

darkgreen is used to clearly show that borders are correct everywhere across the whole table. Essentially, wherever you see darkgreen - is where you style the table's borders.
This codepen shows regular table and the one with thead, tbody and tfoot. CSS is identical to the one above with only the addition of style reset for th. At the moment of writing, you can see, that they both render identically.

Simbiat
  • 339
  • 2
  • 12
-1

I always do this way using Sass

table {
  border-radius: 0.25rem;
  thead tr:first-child th {
    &:first-child {
      border-top-left-radius: 0.25rem;
    }
    &:last-child {
      border-top-right-radius: 0.25rem;
    }
  }
  tbody tr:last-child td {
    &:first-child {
      border-bottom-left-radius: 0.25rem;
    }
    &:last-child {
      border-bottom-right-radius: 0.25rem;
    }
  }
}
Diego Mello
  • 5,220
  • 3
  • 17
  • 21
  • I guess you may haven't get it, he's trying to do that with `border-collapse: collapse` enabled. – giovannipds May 09 '18 at 20:41
  • @giovannipds Take a look at his own reply (accepted answer). My way is just another way. Now remove the "-1". – Diego Mello May 10 '18 at 12:12
  • Oh, I'm sorry, you're definitely right, my mistake, his accepted answer seems to say exactly the same thing. I was to stick to what was written in the question's title, he emphasized he wanted border-collapse so I went straight to it. I would remove the -1 if I could, but I can't,you need to edit something in the answer to allow me that. Try to mention something about that this is not possible with `border-collapse: collapse`. Forgive again, I'll wait your update. – giovannipds May 11 '18 at 12:48
-1

The best solution so far comes from your own solution and it goes like this:

table, tr, td, th{
  border: 1px solid; 
  text-align: center;
}

table{
 border-spacing: 0;
  width: 100%;
  display: table;
}

table tr:last-child td:first-child, tr:last-child, table {
    border-bottom-left-radius: 25px;
}

table tr:last-child td:last-child, tr:last-child, table {
    border-bottom-right-radius: 25px;
}


table tr:first-child th:first-child, tr:first-child, table {
    border-top-left-radius: 25px;
}

table tr:first-child th:last-child, tr:first-child, table {
    border-top-right-radius: 25px;
}
<table>
  <tr>
    <th>Num</th><th>Lett</th><th>Lat</th>
  </tr>
  <tr>
    <td>1</td><td>A</td><td>I</td>
  </tr>
  <tr>
    <td>2</td><td>B</td><td>II</td>
  </tr>
  <tr>
    <td>3</td><td>C</td><td>III</td>
  </tr>
</table>
Andreas M.
  • 182
  • 1
  • 10
-1

table {
  width: 200px;
  text-align: center;
  border-radius: 12px;
  overflow: hidden;
}

table td {
  border-width: 1px 0 0 1px;
}

table tr:first-child td {
  border-top: none;
}

table tr td:first-child {
  border-left: none;
}

div {
  background-color: lime;
}
<table cellspacing="0" cellpadding="0" border="1">
  <tr>
    <td><div>1</div></td>
    <td>1</td>
    <td>1</td>
    <td>1</td>
  </tr>
  <tr>
    <td>1</td>
    <td>1</td>
    <td>1</td>
    <td>1</td>
  </tr>
  <tr>
    <td>1</td>
    <td>1</td>
    <td>1</td>
    <td>1</td>
  </tr>
</table>
jt3k
  • 642
  • 6
  • 17
-1

Use "overflow: hidden" with "border-radius" This works with "collapse" table as well

Example:

border-radius: 1em; overflow: hidden;