Absolute position table cell (td) relative to table row (tr)

前端 未结 2 565
南旧
南旧 2020-12-18 14:56

Is it possible to absolute position table cell (td) relative to table row (tr) containing that td.

For example consider html as below:

相关标签:
2条回答
  • 2020-12-18 15:31

    You can't move a cell away from the table (that I know of).

    0 讨论(0)
  • 2020-12-18 15:32

    The browsers are very strict when it comes to tables. It does not work well when you get out of the scope of how tables are designed to work.

    However, you can use a trick with fixed positioning to cheat the browser into not taking in account the missplaced table cell, since it is absolutelly off the normal flow:

    • Add a transform property to the table row, so it will act as a fixed position container. Choose one that will not have any visual impact, like transform: scale(1,1);

    • Set the table cell as position: fixed, and then you can move it relatively to the transformed row:

    tr {
      position:relative;
      transform:scale(1,1);
    }
    
    td.last{
      position:fixed;
      left: 10px;
      top: 40px;
    }
    <table>
     <tr>
       <td>td 1</td>
       <td>td 2</td>
       <td class="last">td 3</td>
     </tr>
        <tr>
       <td>td 1</td>
       <td>td 2</td>
       <td class="last">td 3</td>
     </tr>
        <tr>
       <td>td 1</td>
       <td>td 2</td>
       <td class="last">td 3</td>
     </tr>
    </table>

    0 讨论(0)
提交回复
热议问题