Set cellpadding and cellspacing in CSS?

前端 未结 28 1082
暖寄归人
暖寄归人 2020-11-22 02:15

In an HTML table, the cellpadding and cellspacing can be set like this:

相关标签:
28条回答
  • 2020-11-22 02:20
    table th,td {
        padding: 8px 2px;
    }
    table {
        border-collapse: separate;
        border-spacing: 2px;
    }
    
    0 讨论(0)
  • 2020-11-22 02:20

    For tables, cellspacing and cellpadding are obsolete in HTML 5.

    Now for cellspacing you have to use border-spacing. And for cellpadding you have to use border-collapse.

    And make sure you don't use this in your HTML5 code. Always try to use these values in a CSS 3 file.

    0 讨论(0)
  • 2020-11-22 02:22

    Basics

    For controlling "cellpadding" in CSS, you can simply use padding on table cells. E.g. for 10px of "cellpadding":

    td { 
        padding: 10px;
    }
    

    For "cellspacing", you can apply the border-spacing CSS property to your table. E.g. for 10px of "cellspacing":

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

    This property will even allow separate horizontal and vertical spacing, something you couldn't do with old-school "cellspacing".

    Issues in IE ≤ 7

    This will work in almost all popular browsers except for Internet Explorer up through Internet Explorer 7, where you're almost out of luck. I say "almost" because these browsers still support the border-collapse property, which merges the borders of adjoining table cells. If you're trying to eliminate cellspacing (that is, cellspacing="0") then border-collapse:collapse should have the same effect: no space between table cells. This support is buggy, though, as it does not override an existing cellspacing HTML attribute on the table element.

    In short: for non-Internet Explorer 5-7 browsers, border-spacing handles you. For Internet Explorer, if your situation is just right (you want 0 cellspacing and your table doesn't have it defined already), you can use border-collapse:collapse.

    table { 
        border-spacing: 0;
        border-collapse: collapse;
    }
    

    Note: For a great overview of CSS properties that one can apply to tables and for which browsers, see this fantastic Quirksmode page.

    0 讨论(0)
  • 2020-11-22 02:22

    You can simply do something like this in your CSS, using border-spacing and padding:

    table {
      border-collapse: collapse;
    }
    
    td, th {
      padding: 1em;
      border: 1px solid blue;
    }
    <table>
      <tr>
        <th>head_1</th>
        <th>head_2</th>
        <th>head_3</th>
        <th>head_4</th>
      </tr>
      <tr>
        <td>txt_1</td>
        <td>txt_2</td>
        <td>txt_3</td>
        <td>txt_4</td>
      </tr>
    </table>

    0 讨论(0)
  • 2020-11-22 02:23

    Setting margins on table cells doesn't really have any effect as far as I know. The true CSS equivalent for cellspacing is border-spacing - but it doesn't work in Internet Explorer.

    You can use border-collapse: collapse to reliably set cell spacing to 0 as mentioned, but for any other value I think the only cross-browser way is to keep using the cellspacing attribute.

    0 讨论(0)
  • 2020-11-22 02:24

    Try this:

    table {
        border-collapse: separate;
        border-spacing: 10px;
    }
    table td, table th {
        padding: 10px;
    }
    

    Or try this:

    table {
        border-collapse: collapse;
    }
    table td, table th {
        padding: 10px;
    }
    
    0 讨论(0)
提交回复
热议问题