12

I want to put a bit of space between HTML table header and footer, and my body content. I though margin-top and margin-bottom would do it, but it does not. Yet the font-weight: bold; directive is taken into account.

My HTML:

<table id="myTbl">
   <thead>
     <tr>
       <th>My Table Header</th>
     </tr>
   </thead>  
   <tbody>
    <tr>
      <td>My Body Content</td>
    </tr>
    </tbody>
   <tfoot>
     <tr>
       <th>My Table Footer</th>
     </tr>
   </tfoot>  
</table>

My CSS:

#myTbl {
    font-weight: normal;
}

#myTbl thead {
    font-weight: bold;
    margin-bottom: 10px;
}

#myTbl tfoot {
    font-weight: bold;
    margin-top: 10px;
}

The JSFiddle is available here. I am using Chrome.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • 2
    Margin doesn't apply to internal `` elements - http://www.w3.org/TR/CSS2/box.html#margin-properties
    – Adrift Aug 21 '13 at 12:44
  • Possible duplicate? http://stackoverflow.com/questions/9258754/spacing-between-thead-and-tbody – adriaanp Aug 21 '13 at 12:44

4 Answers4

10

Use border-spacing property:

#myTbl {
    border-collapse: separate;
    border-spacing: 5px;
}

Fiddle

margin property:

Applies to all elements except elements with table display types other than table-caption, table and inline-table.

I explained Why probably there's no other way to achieve this, lately on SO.

Update:

Your solution adds spacing between all cells.

Then you need to change the display type to be able to use margin property:

#myTbl thead {
    display: table;
    width: 100%;
    margin-bottom: 10px;
}

#myTbl tfoot {
    display: table;
    width: 100%;
    margin-top: 10px;
}

Fiddle

Community
  • 1
  • 1
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
  • Your solution adds spacing between all cells. This is what I was looking for: http://jsfiddle.net/nCe3k/10/ – Jérôme Verstrynge Aug 21 '13 at 12:54
  • 1
    Ok, but when there are multiple columns, it does not work anymore: http://jsfiddle.net/nCe3k/13/ – Jérôme Verstrynge Aug 21 '13 at 13:15
  • @JVerstry, Yes in that case it fails. But note that `padding` is useful only when you don't have any `background` or `border` in `thead` and `tfoot` cells. – Hashem Qolami Aug 21 '13 at 13:20
  • 1
    Well done, your last solution still works quite well in 2022 :-) I wasn't aware that `display: table;` existed (as well as a dozen other table-related display settings) — and that it has been supported by major browsers for so long! Thanks! – Gwyneth Llewelyn Jul 29 '22 at 18:23
8

try using padding on the th elements.

#myTbl {
    font-weight: normal;
}

#myTbl thead tr th{
    font-weight: bold;
    padding-bottom: 10px;
}

#myTbl tfoot tr th{
    font-weight: bold;
    padding-top: 10px;
}

http://jsfiddle.net/nCe3k/9/

james31rock
  • 2,615
  • 2
  • 20
  • 25
8

Here is my solution:

.common-table-body:before {
  line-height:1.5em;
  content:".";
  color:white;
  display:block;
}
Haimei
  • 12,577
  • 3
  • 50
  • 36
5

You can add an empty row between the head and body/body and footer -

     ...</thead>
     <tr height="10px"></tr>
     <tbody>...
sideroxylon
  • 4,338
  • 1
  • 22
  • 40