skip to Main Content

I really need some help with CSS.

I’m trying to style a table and I’m having difficulties adding borders.

Here’s the table style I’m going for (Photoshopped): https://ibb.co/hFkCkDg

Adding a border around the table is simple:

.table-class {
    border: 1px solid #dddddd !important;
    padding: 20px !important;
    border-radius: 5px;
}

Screenshot: https://ibb.co/Fs6qsNv

To add the separating lines inside the table I need to add a top or bottom border to the rows in the table. Rows are tr elements. By default a tr element of a table does not accept borders. So in order to overcome this I added {border-collapse: collapse !important;} to the whole table which allowed me to add borders to rows, but it messed up the border around the whole table. Screenshot: https://ibb.co/Vgfq9jp

Because of {border-collapse: collapse !important;}, the properties border, padding, border-radius don’t work for the table.

Which means I can either add a border around the whole table or add the separating lines, but not both.

How can I achieve the look I’m going for?

2

Answers


  1. I’d go using flexbox, and setting flex: 1 or flex-grow: 1 to the first child of each "row":

    * {margin:0; box-sizing: border-box;}
    body {font: 16px/1.4 'Varela Round', sans-serif; padding: 20px;} /* DEMO ONLY */
    
    /*
      Order
    */
    
    .Order {
      border-radius: 5px;
      border: 1px solid #ddd;
      padding: 10px 25px
    }
    
    .Order-price {
      display: flex;
      border-bottom: 1px solid #ddd;
    }
    
    .Order-price > * {
      padding: 10px 0;
    }
    
    .Order-price > *:first-child{
      flex: 1;
    }
    
    .Order-price:last-child {
      border-bottom: none;
    }
    
    .Order-price--sub {
      font-size: 1.2em;
      font-weight: 500;
    }
    
    .Order-price--tot {
      font-size: 1.4em;
      font-weight: 700;
    }
    
    /*
      Colors
    */
    
    .color-lighter {
      color: #999;
    }
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Varela+Round&display=swap" rel="stylesheet">
    
    <div class="Order">
      <div class="Order-price">
        <span class="color-lighter">Custom Tatoo Design - Small &times; 1</span>
        <span><s class="color-lighter">$99.00</s> <b>$80.00</b></span>
      </div>
      <div class="Order-price Order-price--sub">
        <span>Subtotal</span>
        <span>$80.00</span>
      </div>
      <div class="Order-price Order-price--tot">
        <span>Total</span>
        <span><small>USD</small> $80.00</span>
      </div>
    </div>
    Login or Signup to reply.
  2. working with table border is boring, my suggestion is to use the border in td/th elements.

    I created this table without style, only solving the problem of borders

       .table-class {
        border: 1px solid #dddddd;
        border-radius: 6px;
        padding: 30px;
        border-spacing: unset;
        font-family: sans-serif;
        font-size: 1.5em;
    }
    
    .table-class thead th {
        border-bottom: 1px solid #dddddd;
        text-align: left;
    }
    
    .table-class tbody td {
        border-bottom: 1px solid #dddddd;
    }
    
    .table-class td:last-child, .table-class th:last-child {
        text-align: right;
    }
    
    .table-class th, .table-class td{
        padding: 10px;
    }
    <table class="table-class">
      <thead>
        <tr>
          <th>Custom Tattoo Desing - Small x 1</th>
          <th>
            <span><s>$99.00</s></span>
            <span>$80.00</span>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Subtotal</td>
          <td>$80.00</td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>Total</td>
          <td>USD $80.00</td>
        </tr>
      </tfoot>
    </table>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search