Overlay on top of tbody

前端 未结 3 1967
后悔当初
后悔当初 2021-01-24 04:26

I am trying to create a loading overlay on top of tbody (and only tbody). My current solution is to add tr as the last element of tbody and set

相关标签:
3条回答
  • 2021-01-24 04:58

    you have to remove the position relative on tbody and out it on table instead.

    table {
      width: 100%;
      position: relative;
    }
    
    .overlay {
      position: absolute;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(200, 200, 200, 0.7);
    }
    <table>
      <thead>
        <tr>Label</tr>
      </thead>
      <tbody>
        <tr><td>Data</td></tr>
        <tr><td>Data</td></tr>
        <tr class="overlay">
          <td><div>My overlay</div></td>
        </tr>
      </tbody>
    </table>

    0 讨论(0)
  • 2021-01-24 05:13

    IMHO: In browsers which support CSS3 position:relative on table elements should work. As you have rightly revealed: it works only in Firefox. Not in IE, Edge and Chrome.

    Sources:

    https://www.w3.org/TR/css-position-3/#valdef-position-relative

    https://www.w3.org/TR/css-position-3/#property-index

    Property name: position Applies to: all elements except table-column-group and table-column

    https://developer.mozilla.org/en-US/docs/Web/CSS/position#relative About 'stacking context' but that is not the subject of this question

    This value (position: relative) creates a new stacking context when the value of z-index is not auto. Its effect on table-*-group, table-row, table-column, table-cell, and table-caption elements is undefined.

    Related questions:

    1. Overlay on top of tbody
    2. Bug in most browsers?: Ignoring position relative on 'tbody', 'tr' and 'td'?
    0 讨论(0)
  • 2021-01-24 05:21

    Using CSS calc should help with this. Support is fairly good for the property too.

    You just need to balance the calc with the top property.

    So your CSS would become:

    table {
      width: 100%;
      position: relative;
    }
    
    .overlay {
      position: absolute;
      top: 25px;
      width: 100%;
      height: -webkit-calc(100% - 25px);
      height: calc(100% - 25px);
      background-color: rgba(200, 200, 200, 0.7);
    }
    

    Here's a working example:

    table {
      width: 100%;
      position: relative;
    }
    
    .overlay {
      position: absolute;
      top: 25px;
      width: 100%;
      height: -webkit-calc(100% - 25px);
      height: calc(100% - 25px);
      background-color: rgba(200, 200, 200, 0.7);
    }
    <table>
      <thead>
        <tr><td>Label</td></tr>
      </thead>
      <tbody>
        <tr><td>Data</td></tr>
        <tr><td>Data</td></tr>
        <tr class="overlay">
          <td>My overlay</td>
        </tr>
      </tbody>
    </table>

    Updated answer using CSS Calc.

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