CSS: how do I create a gap between rows in a table?
Solution 1:
All you need:
table {
border-collapse: separate;
border-spacing: 0 1em;
}
That assumes you want a 1em vertical gap, and no horizontal gap. If you're doing this, you should probably also look at controlling your line-height.
Sort of weird that some of the answers people gave involve border-collapse: collapse, whose effect is the exact opposite of what the question asked for.
Solution 2:
table {
border-collapse: collapse;
}
td {
padding-top: .5em;
padding-bottom: .5em;
}
The cells won't react to anything unless you set the border-collapse first. You can also add borders to TR elements once that's set (among other things.)
If this is for layout, I'd move to using DIVs and more up-to-date layout techniques, but if this is tabular data, knock yourself out. I still make heavy use of tables in my web applications for data.
Solution 3:
If none of the other answers are satisfactory, you could always just create an empty row and style it with CSS appropriately to be transparent.
Solution 4:
If you don't have borders, or have borders and want the spacing inside the cells, you can use padding
, or line-height
. As far as I know, margin has no effect on cells and rows.
A CSS property for spacing of cells is border-spacing
, but it doesn't work on IE6/7 (so you can use it depending on your crowd).
If all else fails you can use the old cellspacing
attribute in your markup - but this will also give you spacing between the columns. Some CSS reset suggest you should set it anyway to get cross-browser support:
/* tables still need
cellspacing="0"
in the markup */
Solution 5:
Simply you can use padding-top
and padding-bottom
on a td
element.
Unit can anything from this list:
Demo Code:
td
{
padding-top: 10px;
padding-bottom: 10px;
}
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>