Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply border-spacing to table body rows only

Tags:

html

css

I have a table with 2 header rows and multiple body rows. I want the spacing between rows in the body to be 10 pixels. I achieve this with:

border-collapse: separate;
border-spacing: 10px;

However, this also obviously applies to the rows in the header. But for the header, I want there to be no space between the rows. My HTML is:

table td {
  background-color: lime;
  padding: 12px 12px 12px 12px;
}
table th {
  background-color: red;
  padding: 12px 12px 12px 12px;
}
table {
  border-collapse: separate;
  border-spacing: 10px;
}
<table>
  <thead>
    <tr>
      <th>head 1</th>
      <th>head 1</th>
      <th>head 1</th>
    </tr>
    <tr>
      <th>head 2</th>
      <th>head 2</th>
      <th>head 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
  </tbody>
</table>

The fiddle is here. I want there to be no space between the bottom of the first header row and top of second header row. I've tried applying border-spacing just to the body but it only works at table level. Any ideas?

like image 869
Mark Avatar asked Aug 21 '15 09:08

Mark


People also ask

How do I add a border to a row in a table?

To add a border to your table, you need to define the <style> of your table. Remember to add borders also for <th> and <td> tags to have a complete table. Set the border-collapse property as well (if you don't define the border-collapse, it will use border-collapse: separate by default).

How is border spacing in a table is specified in?

Correct Option: C. border-spacing and border-collapse are the two properties by which one can set the border and its styling in a table. We give its value in pixels.

How do I add margins between table rows?

Use the border-collapse property with its "separate" value for the table. Use the border-spacing property to set the distance between the borders of neighbouring table cells. For the first row, set the background color and the color of the text by using the background-color and color properties.

How do you space a row in a table?

The space between two rows in a table can be done using CSS border-spacing and border-collapse property. The border-spacing property is used to set the spaces between cells of a table and border-collapse property is used to specify whether the border of table is collapse or not.


Video Answer


1 Answers

The border-spacing is applied to table elements and can not be targeted for tbody alone but you can try the below CSS hack and apply border: white to the td element to create a margin effect.

Additional Code:

table td {
  border: 10px solid white;
  border-right: 0;
  border-left: 0;
}

Output:

table td {
  background-color: lime;
  padding: 12px 12px 12px 12px;
  border: 10px solid white;
  border-right: 0;
  border-left: 0;
}
table th {
  background-color: red;
  padding: 12px 12px 12px 12px;
}
table {
  border-collapse: separate;
}
<table>
  <thead>
    <tr>
      <th>head 1</th>
      <th>head 1</th>
      <th>head 1</th>
    </tr>
    <tr>
      <th>head 2</th>
      <th>head 2</th>
      <th>head 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
    <tr>
      <td>Cell</td>
      <td>Cell</td>
      <td>Cell</td>
    </tr>
  </tbody>
</table>
like image 56
m4n0 Avatar answered Sep 22 '22 10:09

m4n0