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
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>
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.
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 excepttable-column-group
andtable-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 ontable-*-group
,table-row
,table-column
,table-cell
, andtable-caption
elements is undefined.
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.