CSS table layout: why does table-row not accept a margin?

后端 未结 10 2236
余生分开走
余生分开走 2020-12-12 23:34



        
相关标签:
10条回答
  • 2020-12-13 00:02

    How's this for a work around (using an actual table)?

    table {
        border-collapse: collapse;
    }
    
    tr.row {
        border-bottom: solid white 30px; /* change "white" to your background color */
    }
    

    It's not as dynamic, since you have to explicitly set the color of the border (unless there's a way around that too), but this is something I'm experimenting with on a project of my own.

    Edit to include comments regarding transparent:

    tr.row {
        border-bottom: 30px solid transparent;
    }
    
    0 讨论(0)
  • 2020-12-13 00:02

    Fiddle

     .row-seperator{
       border-top: solid transparent 50px;
     }
    
    <table>
       <tr><td>Section 1</td></tr>
       <tr><td>row1 1</td></tr>
       <tr><td>row1 2</td></tr>
       <tr>
          <td class="row-seperator">Section 2</td>
       </tr>
       <tr><td>row2 1</td></tr>
       <tr><td>row2 2</td></tr>
    </table>
    
    
    #Outline
    Section 1
    row1 1
    row1 2
    
    
    Section 2
    row2 1
    row2 2
    

    this can be another solution

    0 讨论(0)
  • 2020-12-13 00:03

    The closest thing I've seen would be to set border-spacing: 0 30px; to the container div. However, this just leaves me with space on the upper edge of the table, which defeats the purpose, since you wanted margin-bottom.

    0 讨论(0)
  • 2020-12-13 00:03

    If you want a specific margin e.g. 20px, you can put the table inside a div.

    <div id="tableDiv">
        <table>
          <tr>
            <th> test heading </th>
          </tr>
          <tr>
            <td> test data </td>
          </tr>
        </table>
    </div>
    

    So the #tableDiv has a margin of 20px but the table itself has a width of 100%, forcing the table to be the full width except for the margin on either sides.

    #tableDiv {
      margin: 20px;
    }
    
    table {
      width: 100%;
    }
    
    0 讨论(0)
提交回复
热议问题