763

Is this possible via CSS?

I'm trying

tr.classname {
  border-spacing: 5em;
}

to no avail. Maybe I'm doing something wrong?

| |

25 Answers 25

526

You need to use padding on your td elements. Something like this should do the trick. You can, of course, get the same result using a top padding instead of a bottom padding.

In the CSS code below, the greater-than sign means that the padding is only applied to td elements that are direct children to tr elements with the class spaceUnder. This will make it possible to use nested tables. (Cell C and D in the example code.) I'm not too sure about browser support for the direct child selector (think IE 6), but it shouldn't break the code in any modern browsers.

/* Apply padding to td elements that are direct children of the tr elements with class spaceUnder. */

tr.spaceUnder>td {
  padding-bottom: 1em;
}
<table>
  <tbody>
    <tr>
      <td>A</td>
      <td>B</td>
    </tr>
    <tr class="spaceUnder">
      <td>C</td>
      <td>D</td>
    </tr>
    <tr>
      <td>E</td>
      <td>F</td>
    </tr>
  </tbody>
</table>

This should render somewhat like this:

+---+---+
| A | B |
+---+---+
| C | D |
|   |   |
+---+---+
| E | F |
+---+---+
| |
  • 1
    code.google.com/p/ie7-js adds support to child selector in ie5.5, ie6, ie7 as well – Antony Hatchkins Jan 27 '12 at 17:16
  • 412
    Except this doesn't solve the problem at all when your rows have a background colour and you actually want WHITESPACE between your rows. – Simon East Feb 26 '12 at 22:49
  • 6
    @Simon: Whitespace usually refers to empty space, and not specifically the color white. If you want to colorize the space between the rows, you can try with the CSS <code>border-bottom</code> property on the td-elements. That will probably only render correctly with a 1px border. Another, but not so elegant, solution would be to use an empty row. – Jan Aagaard Feb 27 '12 at 10:44
  • 18
    @Jan: Yeah, exactly my point... It's quite difficult to put transparent empty space between rows. Your solution helps in some instances, but doesn't solve that problem. Inserting an empty table row might work, but it's f'ugly. A bottom-border set to transparent might also work, but I'm not sure how cross-browser-compatible it is. – Simon East Feb 28 '12 at 6:28
  • 1
396

In the parent table, try setting

border-collapse:separate; 
border-spacing:5em;

Plus a border declaration, and see if this achieves your desired effect. Beware, though, that IE doesn't support the "separated borders" model.

| |
  • 5
    Yeah, this method would be ideal, except that IE 7 doesn't support it. For browser support, see: quirksmode.org/css/tables.html – Simon East Feb 26 '12 at 22:50
  • 7
    Also, it controls the spacing across all rows in the table, you can't set row spacing for individual rows (which is what the OP might be wanting to achieve). – Simon East Feb 26 '12 at 22:54
  • 1
    This is inconsistent between Chrome and Firefox. If you have a thead and tbody, it will render a double border-spacing between those, in Chrome (but a single one in Firefox). – Camilo Martin Aug 28 '12 at 15:56
  • 94
    This is the actual answer, specifically set border-spacing: 0 1em to get your spacing between only rows. – igneosaur Jun 25 '14 at 11:40
  • 1
    @igneosaur I tried doing what you said but it still added spacing between all the rows and not just the first two. Can you provide a working jsfiddle? – user3281466 Aug 27 '14 at 16:05
187

You have table with id albums with any data... I have omitted the trs and tds

<table id="albums" cellspacing="0">       
</table>

In the css:

table#albums 
{
    border-collapse:separate;
    border-spacing:0 5px;
}
| |
133

since I have a background image behind the table, faking it with white padding wouldn't work. I opted to put an empty row in-between each row of content:

<tr class="spacer"><td></td></tr>

then use css to give the spacer rows a certain height and transparent background.

| |
  • 3
    This is probably more flexible, I would think. You can't always control when in advance you'll need space (dynamically generated pages). – Stefan Kendall Nov 2 '10 at 17:16
  • 10
    This reminds me of the 90s, when CSS support was almost non-existent and tables were used for most layout. I'm still giving it +1, though, because the other "answers" are not any better in terms of actually making space between table rows. – labyrinth Nov 28 '13 at 19:33
  • @labyrinth Not much has changed since the 90s. Concerning HTML, it all just has become much more powerful and much more messy. – maaartinus Jun 19 '14 at 20:10
77

From Mozilla Developer Network:

The border-spacing CSS property specifies the distance between the borders of adjacent cells (only for the separated borders model). This is equivalent to the cellspacing attribute in presentational HTML, but an optional second value can be used to set different horizontal and vertical spacing.

That last part is often overseen. Example:

.your-table {
    border-collapse: separate; /* allow spacing between cell borders */
    border-spacing: 0 5px; /* NOTE: syntax is <horizontal value> <vertical value> */

UPDATE

I now understand that the OP wants specific, seperate rows to have increased spacing. I've added a setup with tbody elements that accomplishes that without ruining the semantics. However, I'm not sure if it is supported on all browsers. I made it in Chrome.

The example below is for showing how you can make it look like the table exists of seperate rows, full blown css sweetness. Also gave the first row more spacing with the tbody setup. Feel free to use!

Support notice: IE8+, Chrome, Firefox, Safari, Opera 4+

.spacing-table {
  font-family: 'Helvetica', 'Arial', sans-serif;
  font-size: 15px;
  border-collapse: separate;
  table-layout: fixed;
  width: 80%;
  border-spacing: 0 5px; /* this is the ultimate fix */
}
.spacing-table th {
  text-align: left;
  padding: 5px 15px;
}
.spacing-table td {
  border-width: 3px 0;
  width: 50%;
  border-color: darkred;
  border-style: solid;
  background-color: red;
  color: white;
  padding: 5px 15px;
}
.spacing-table td:first-child {
  border-left-width: 3px;
  border-radius: 5px 0 0 5px;
}
.spacing-table td:last-child {
  border-right-width: 3px;
  border-radius: 0 5px 5px 0;
}
.spacing-table thead {
  display: table;
  table-layout: fixed;
  width: 100%;
}
.spacing-table tbody {
  display: table;
  table-layout: fixed;
  width: 100%;
  border-spacing: 0 10px;
}
<table class="spacing-table">
  <thead>
    <tr>
        <th>Lead singer</th>
        <th>Band</th>
    </tr>
  </thead>
  <tbody>
    <tr>
        <td>Bono</td>
        <td>U2</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
        <td>Chris Martin</td>
        <td>Coldplay</td>
    </tr>
    <tr>
        <td>Mick Jagger</td>
        <td>Rolling Stones</td>
    </tr>
    <tr>
        <td>John Lennon</td>
        <td>The Beatles</td>
    </tr>
  </tbody>
</table>

| |
  • If I wanted some vertivcal space between two particular rows I just added a spacer row <tr><td colspan="2" style="padding-bottom:1em;"></td></tr> – Paul Taylor Feb 7 '18 at 10:13
  • That would work but is semantically incorrect (as in, your seperator row is not an actual row with data like the other rows are. This can be annoying for people using screen-readers or other assistant devices, as well as for indexing purposes and the likes. It might not make a huge difference but it is good to keep it back in your head when developing a website :) – Justus Romijn Feb 7 '18 at 11:46
  • That is true, if he is actually using a table to represent a table. If he is just using a table as a way as a way to display data in a tabular fashion then i would argue it makes more sense to insert a row to represent a gap between two items rather than just adding spacing to either the td elments above or below the line because what the user wants to do is add space between two lines, not increase the size of elements within a line. – Paul Taylor Feb 7 '18 at 12:02
54

You may try to add separator row:

html:

<tr class="separator" />

css:

table tr.separator { height: 10px; }
| |
  • 7
    For IE, tr without a body would not be recognize. add dummy td tag too. – Nap Feb 7 '12 at 12:41
  • The problem is that in HTML5, the W3C validator will emit an error saying " A table row was 0 columns wide, which is less than the column count established by the first row". – David Grayson Mar 4 '15 at 23:00
  • Nice, I had added another <tr>, <td> and <p> and set the p-tag's visibility to hidden.. Works too. :-) – Jeppe Sep 18 '15 at 22:45
47

You can't change the margin of a table cell. But you CAN change the padding. Change the padding of the TD, which will make the cell larger and push the text away from the side with the increased padding. If you have border lines, however, it still won't be exactly what you want.

| |
  • Yep, padding will push the border down, so border-bottom and padding-bottom means that the border will be below the padding. – Dan Dascalescu Jan 17 '13 at 17:46
23

Take a look at the border-collapse: separate attribute (default) and the border-spacing property.

First, you have to seperate them with border-collapse, then you can define the space between columns and rows with border-spacing .

Both of these CSS properties are actually well-supported on every browser, see here.

table     {border-collapse: separate;  border-spacing: 10px 20px;}

table, 
table td,
table th  {border: 1px solid black;}
<table>
  <tr>
    <td>Some text - 1</td>
    <td>Some text - 1</td>
  </tr>
  <tr>
    <td>Some text - 2</td>
    <td>Some text - 2</td>
  </tr>
  <tr>
    <td>Some text - 3</td>
    <td>Some text - 3</td>
  </tr>
</table>

| |
20

Ok, you can do

tr.classname td {background-color:red; border-bottom: 5em solid white}

Make sure the background color is set on the td rather than the row. This should work across most browsers... (Chrome, ie & ff tested)

| |
20

You need to set border-collapse: separate; on the table; most browser default stylesheets start at border-collapse: collapse;, which ditches border spacing.

Also, border-spacing: goes on the TD, not the TR.

Try:

<html><head><style type="text/css">
    #ex    { border-collapse: separate; }
    #ex td { border-spacing: 1em; }
</style></head><body>
    <table id="ex"><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table>
</body>
| |
  • 2
    border-spacing is also for table not td – silversky Sep 4 '13 at 19:23
  • 4
    Thanks. I thanked you in the comment fix previously, but an overzealous editor who wanted to change my style and broke the code in the process also removed my saying thanks. I'm now putting it here where it can't be edited. – John Haugeland Nov 30 '14 at 19:33
19

You can use line-height in the table:

<table style="width: 400px; line-height:50px;">
| |
  • You are correct but The text will also get the line-height which is not good, I think you should see the coleman! He has the nice solution – Waqas Tahir Jun 1 '15 at 5:20
13
tr { 
    display: block;
    margin-bottom: 5px;
}
| |
11

A too late answer :)

If you apply float to tr elements, you can space between two rows with margin attribute.

table tr{
float: left
width: 100%;
}

tr.classname {
margin-bottom:5px;
}
| |
11

For creating an illusion of spacing between rows, apply background color to row and then create a thick border with white color so that a "space" is created :)

tr 
{
   background-color: #FFD700;
   border: 10px solid white;
}
| |
9

The correct way to give spacing for tables is to use cellpadding and cellspacing e.g.

<table cellpadding="4">
| |
  • 5
    You can do this via CSS, which I'd say it's more "correct" (besides the fact that it's more elegant, moving styling from markup to CSS saves bandwidth). – Camilo Martin Aug 28 '12 at 14:36
  • 2
    It does not save bandwidth. I actually uses more. Assuming you're not adding a stylesheet just for this you need to add a selector and possibly an id or name to this table. That's more bytes not less. – majinnaibu May 29 '15 at 21:42
9

I stumbled upon this while struggling with a similar issue. I've found Varun Natraaj's answer to be quite helpful, but I would use a transparent border instead.

td { border: 1em solid transparent; }

Transparent borders still have width.

| |
6

Works for most latest browsers in 2015. Simple solution. It doesn't work for transparent, but unlike Thoronwen's answer, I can't get transparent to render with any size.

    tr {
      border-bottom:5em solid white;
    }
| |
3

Simply put div inside the td and set the following styles of div:

margin-bottom: 20px;
height: 40px;
float: left;
width: 100%;
| |
2

I realize this is an answer to an old thread and may not be the solution requested, but while all the suggested solutions did not do what I needed, this solution worked for me.

I had 2 table cells, one with background color, the other with a border color. The above solutions remove the border, so the cell on the right would appear to be floating in mid-air. The solution that did the trick was to replace the table, tr and td with divs and corresponding classes: table would be div id="table_replacer", tr would be div class="tr_replacer" and td would be div class="td_replacer" (change closing tags to divs as well obviously)

To get the solution for my problem the css is:

#table_replacer{display:table;}
.tr_replacer {border: 1px solid #123456;margin-bottom: 5px;}/*DO NOT USE display:table-row! It will destroy the border and the margin*/
.td_replacer{display:table-cell;}

Hope this helps someone.

| |
  • Imho, this solution is equivalent to using the regular table, tr, td pattern and style tr {display: block;} which I tried. Problem: the table looks different, as the td elements are not rendered to the table width, but float left within the tr. Means, the td elements of all rows are no longer aligned vertically. – Andreas Stankewitz Mar 23 '18 at 8:43
1

You can fill the <td/> elements with <div/> elements, and apply any margin to those divs that you like. For a visual space between the rows, you can use a repeating background image on the <tr/> element. (This was the solution I just used today, and it appears to work in both IE6 and FireFox 3, though I didn't test it any further.)

Also, if you're averse to modifying your server code to put <div/>s inside the <td/>s, you can use jQuery (or something similar) to dynamically wrap the <td/> contents in a <div/>, enabling you to apply the CSS as desired.

| |
0

Or just add a blank with the height of the margin in between the rows you would like to add the spacing

| |
0

Here's a simple and elegant solution, with a few caveats:

  • You can't actually make the gaps transparent, you need to give them a specific color.
  • You can't round the corners of the borders above & below the gaps
  • You need to know the padding and borders of your table cells

With that in mind, try this:

td {padding:5px 8px;border:2px solid blue;background:#E0E0E0}  /* lets say the cells all have this padding and border, and the gaps should be white */

tr.gapbefore td {overflow:visible}
tr.gapbefore td::before,
tr.gapbefore th::before
{
  content:"";
  display:block;
  position:relative;
  z-index:1;
  width:auto;
  height:0;
  padding:0;
  margin:-5px -10px 5px;  /* 5px = cell top padding, 10px = (cell side padding)+(cell side border width)+(table side border width) */
  border-top:16px solid white;  /* the size & color of the gap you want */
  border-bottom:2px solid blue; /* this replaces the cell's top border, so should be the same as that. DOUBLE IT if using border-collapse:separate */
}

What you're actually doing is sticking a rectangular ::before block into the top of all the cells in the row you want preceded by a gap. These blocks stick out of the cells a bit to overlap the existing borders, hiding them. These blocks are just a top and bottom border sandwiched together: The top border forms the gap, and the bottom border re-creates the appearance of the cells' original top border.

Note that if you have a border around the table itself as well as the cells, you'll need to further increase the horizontal -ve margin of of your 'blocks'. So for a 4px table border, you'd instead use:

margin:-5px -12px 5px;     /* 14px = original 10px + 2px for 'uncollapsed' part of table border */

And if your table uses border-collapse:separate instead of border-collapse:collapse, then you'll need to (a) use the full table border width:

margin:-5px -14px 5px;     /* 14px = original 10px + 4px full width of table border */

... and also (b) replace the double-width of border that now needs to appear below the gap:

border-bottom:4px solid blue;     /* i.e. 4px = cell top border + previous row's bottom border */

The technique is easily adapted to a .gapafter version, if you prefer, or to creating vertical (column) gaps instead of row gaps.

Here's a codepen where you can see it in action: https://codepen.io/anon/pen/agqPpW

| |
-1

Here this works smoothly:

#myOwnTable td { padding: 6px 0 6px 0;}

I suppose you could work out a more finely-grained layout by specifying which td if need be.

| |
-5

doing this shown above...

table tr{ float: left width: 100%; }  tr.classname { margin-bottom:5px; } 

removes vertical column alignment so be careful how you use it

| |
-17

Have you tried:

tr.classname { margin-bottom:5em; }

Alternatively, each td can be adjusted as well:

td.classname { margin-bottom:5em; }

or

 td.classname { padding-bottom:5em; }
| |
  • Does this actually work? I have div.el { display: table-row; margin: 10px; }, and margin doesn't do anything. I know it's a little different than an actual table, but it shouldn't be... – bradlis7 Jun 26 '10 at 19:36
  • 21
    Table rows/cells does not have margins. – Espen Mar 3 '11 at 8:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.