htmlcsshtml-tableborderborder-radius

Border radius of table not working due to border-collapse property


My border radius does not show if I have the property of border-collapse on the table tag. I need the border-radius property on and if I remove the border-collapse property I don't get the look I want which is the grey sections to go to the very edge of the table.

What is the solution to this and whats the cause of it?

Thanks in advance

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

table {
  /*if i comment out the border-collapse property it then shows my radius*/
  border-collapse: collapse;
  margin: 25px 0;
  width: 50%;
  border-radius: 5px;
  font-size: 1.4rem;
  min-width: 400px;
  border: 1px solid #c3c3c3;
  /*margin is just for demo*/
  margin: 20px 20px;
}

table tr {
  border-bottom: solid 1px #d1d1d1;
}

table tr:nth-child(odd) {
  background-color: #eee;
}

table td {
  padding: 10px 15px;
}

table td:first-of-type {
  font-weight: 600;
}
<table>
  <tbody>
    <tr>
      <td>Application</td>
      <td>Natural gas & LPG</td>
    </tr>
    <tr>
      <td>Standards</td>
      <td>BS EN ISO 9001:2008 - EN 331:1998</td>
    </tr>
    <tr>
      <td>Connection</td>
      <td>BSP Taper F</td>
    </tr>
    <tr>
      <td>Finish</td>
      <td>Plated</td>
    </tr>
  </tbody>
</table>


Solution

  • If your intention is to not see any spacing between the content background and the border, then simply remove the border-collapse and add border-spacing: 0. border-spacing: 0 will not affect the border radius at all and it will also give you the results of no space between the border and the inner content.

    In searching it seems there are some anomalies with using collapse and radius together. There are also some work-arounds where you use pseudo tags on the tables children specifically to get a radius to work, but why waste all that time when you can just remove the space between the border and its inner content using border-spacing which works well with border-radius

    EDIT: By using pseudo selectors along with border-space: 0 you can achieve a more pronounced border radius.

    We want to target each td element that borders the edge of the table element. table tr td:first-of-type and table tr td:last of type to get the left and right sides. Then we target each subsequent first and last child to get the corners. Lastly, if this is a dynamic table and you will have more than two data tables located in the table, add td:not(:first-child):not(:last-child) on each first and last of type.

    I don't get the look I want which is the grey sections to go to the very edge of the table.

    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    table {
      /*add border-spacing: 0     instead of     border-collapse: collapse*/
      border-spacing: 0;
      margin: 25px 0;
      width: 50%;
      font-size: 1.4rem;
      min-width: 400px;
      /*margin is just for demo*/
      margin: 20px 20px;
    }
    
    
    /* Start psuedo child tags here, targeting each data elements relevant corners and sides */
    
    table tr td:first-of-type {
       border-left: 1px solid #c3c3c3;
    }
    
    table tr td:last-of-type {
       border-right: 1px solid #c3c3c3;
    }
    
    /* :not(:first-child):not(:last-child)
    This will get any potential data tables that are added 
    that are not sides or corners however, they are border 
    data tables on top or bottom */    
    
    table tr:first-of-type td:not(:first-child):not(:last-child){
      border-top: 1px solid #c3c3c3;
    }
    
    
    table tr:last-of-type td:not(:first-child):not(:last-child){
      border-bottom: 1px solid #c3c3c3;
    }
    
    table tr:first-of-type td:first-child {
        border-top: 1px solid #c3c3c3;
        border-top-left-radius: 5px;
    }
    
    table tr:first-of-type td:last-child {
        border-top: 1px solid #c3c3c3;
        border-top-right-radius: 5px;
    }
    
    table tr:last-of-type td:last-child {
        border-right: 1px solid #c3c3c3;
        border-bottom: 1px solid #c3c3c3;
        border-bottom-right-radius: 5px;
    }
    
    table tr:last-of-type td:first-child {
        border-left: 1px solid #c3c3c3;
        border-bottom:  1px solid #c3c3c3;
        border-bottom-left-radius: 5px;
    }
    
    /* End Psuedo tags here */
    
    table tr {
      border-bottom: solid 1px #d1d1d1;
    }
    
    table tr:nth-child(odd) {
      background-color: #eee;
    }
    
    table td {
      padding: 10px 15px;
    }
    
    table td:first-of-type {
      font-weight: 600;
    }
    <div id="table">
      <table>
        <tbody>
          <tr>
            <td>Application</td>
            <td>here is some data</td>
            <td>Natural gas & LPG</td>
          </tr>
          <tr>
            <td>Standards</td>
            <td>some data in between</td>
            <td>BS EN ISO 9001:2008 - EN 331:1998</td>
          </tr>
          <tr>
            <td>Connection</td>
            <td>some data in between</td>
            <td>BSP Taper F</td>
          </tr>
          <tr>
            <td>more tables</td>
            <td>some data in between</td>
            <td>more data</td>
          </tr>
          <tr>
            <td>some more data still</td>
            <td>some data in between</td>
            <td>and yet more about this data</td>
          </tr>
          <tr>
            <td>Finish</td>
            <td>Plated</td>
            <td>Plated too</td>
          </tr>
        </tbody>
      </table>
    </div>