CSS Cell Margin

423,649

Solution 1

Apply this to your first <td>:

padding-right:10px;

HTML example:

<table>
   <tr>
      <td style="padding-right:10px">data</td>
      <td>more data</td>
   </tr>
</table>

Solution 2

A word of warning: though padding-right might solve your particular (visual) problem, it is not the right way to add spacing between table cells. What padding-right does for a cell is similar to what it does for most other elements: it adds space within the cell. If the cells do not have a border or background colour or something else that gives the game away, this can mimic the effect of setting the space between the cells, but not otherwise.

As someone noted, margin specifications are ignored for table cells:

CSS 2.1 Specification – Tables – Visual layout of table contents

Internal table elements generate rectangular boxes with content and borders. Cells have padding as well. Internal table elements do not have margins.

What's the "right" way then? If you are looking to replace the cellspacing attribute of the table, then border-spacing (with border-collapse disabled) is a replacement. However, if per-cell "margins" are required, I am not sure how that can be correctly achieved using CSS. The only hack I can think of is to use padding as above, avoid any styling of the cells (background colours, borders, etc.) and instead use container DIVs inside the cells to implement such styling.

I am not a CSS expert, so I could well be wrong in the above (which would be great to know! I too would like a table cell margin CSS solution).

Cheers!

Solution 3

If you can't use padding (for example you have borders in td) try this

table { 
           border-collapse: separate;
           border-spacing: 2px;
}

Solution 4

margin does not work unfortunately on individual cells, however you could add extra columns between the two cells you want to put a space between... another option is to use a border with the same colour as the background...

Solution 5

I realize this is quite belated, but for the record, you can also use CSS selectors to do this (eliminating the need for inline styles.) This CSS applies padding to the first column of every row:

table > tr > td:first-child { padding-right:10px }

And this would be your HTML, sans CSS!:

<table><tr><td>data</td><td>more data</td></tr></table>

This allows for much more elegant markup, especially in cases where you need to do lots of specific formatting with CSS.

Share:
423,649

Related videos on Youtube

Tom
Author by

Tom

Updated on July 08, 2022

Comments

  • Tom
    Tom almost 2 years

    In my HTML document, I have a table with two columns and multiple rows. How can I increase the space in between the first and second column with css? I've tried applying "margin-right: 10px;" to each of the cells on the left hand side, but to no effect.

  • Levitikon
    Levitikon over 12 years
    This is great and all for padding, but what about for margin? I want to add space outside of the cell that contains a border. Margin CSS seems to have no affect on cells.
  • ravi
    ravi almost 12 years
    Levitikon, that's right - margins do not work for reasons above. The only way to get what you want, AFAIK, is to wrap the contents of the cell in a DIV, add the margin and border to that DIV, rather than the cell.
  • Hayden Thring
    Hayden Thring almost 11 years
    this is good, simple, and effective, thanks for reminding me +1
  • Gaurav Ramanan
    Gaurav Ramanan almost 11 years
    You can check more about border-collapse : separate here css-tricks.com/almanac/properties/b/border-collapse
  • Gaurav Ramanan
    Gaurav Ramanan almost 11 years
    A far better approach to it would be to use border-collapse : seperate
  • Simon Robb
    Simon Robb over 10 years
    This is not a good approach for separation of structure and styling. It's not a case of whether or not CSS is required - CSS should be used for something like this.
  • Rune Jeppesen
    Rune Jeppesen almost 10 years
    shouldn't be <td style="width:10px;"></td>
  • trebor
    trebor over 9 years
    Remember, the simplest solutions are the best. Method with inline style is also good :)
  • Necreaux
    Necreaux over 8 years
    This also worked for css tables (display:table*) made out of divs.
  • Asons
    Asons over 8 years
    This solution is already posted, no need for 2 answers saying the same thing.
  • stewbasic
    stewbasic about 8 years
    It seems like border has the same problem as padding - it adds space between the cell content and boundary, not outside the boundary.
  • Tony DiNitto
    Tony DiNitto almost 8 years
    Just in case someone was copy/pasting the border-collapse technique the above comment, it should be border-collapse: separate (there was a typo in the spelling of separate above)
  • Mawg says reinstate Monica
    Mawg says reinstate Monica almost 8 years
    That is masking the problem, not answering it. No one will learn how to do it properly by doing that
  • Mawg says reinstate Monica
    Mawg says reinstate Monica almost 8 years
    I upvoted, but you might have explained why it is better to use %
  • Muhammad Awais
    Muhammad Awais almost 8 years
    Normally % is used to make responsive.
  • Asons
    Asons almost 8 years
    I posted a CSS solution a long time ago (updated it today): stackoverflow.com/a/21551008/2827823
  • Mawg says reinstate Monica
    Mawg says reinstate Monica almost 8 years
    Plus one - maybe you could edit your answer to say so? Not veryone who reads might read the comments.. If you want to add a link to something about responsive design, even better :-)
  • Sølve T.
    Sølve T. almost 6 years
    Also, if you want to define the amount of space between the borders, you will have to use border-spacing: 5px;
  • Lee Blake
    Lee Blake about 5 years
    border-spacing also adds space between rows. Unfortunately, there is not a separate property for vertical and horizontal spacing.
  • OldTinfoil
    OldTinfoil over 4 years
    border-spacing takes two arguments - one for horizontal and one for vertical.
  • Linh
    Linh almost 4 years
    It working for my specific case. Very creative. Thank you