Absolute positioning and hiding overflow
P粉451614834
2023-08-21 19:30:21
<p>We have two DIVs, one nested inside the other. If the outer DIV is not set to be absolutely positioned, then the absolutely positioned inner DIV will not respect the overflow hiding of the outer DIV. </p>
<p><br /></p>
<pre class="brush:css;toolbar:false;">#first {
width: 200px;
height: 200px;
background-color: green;
overflow: hidden;
}
#second {
width: 50px;
height: 50px;
background-color: red;
position: absolute;
left: 250px;
top: 250px;
}</pre>
<pre class="brush:html;toolbar:false;"><div id="first">
<div id="second"></div>
<div id="third"></div>
</div></pre>
<p><br /></p>
<p>Is there a way to make the inner DIV follow the overflow hiding of the outer DIV without setting the outer DIV to be absolutely positioned (as this would break our overall layout)?
Also, relative positioning is not an option for our inner DIV since we need to "highlight" a table TD. </p>
<p><br /></p>
<pre class="brush:css;toolbar:false;">#first {
width: 200px;
height: 200px;
background-color: green;
}
#second {
width: 50px;
height: 400px;
background-color: red;
position: relative;
left: 0px;
top: 0px;
}</pre>
<pre class="brush:html;toolbar:false;"><table id="first">
<tr>
<td>
<div id="second"></div>
</td>
</tr>
</table></pre>
<p><br /></p>
<p>Are there any other options? </p>
What about using
position: relative
for external divs? In the example of hiding the inner div. Since top or left are not specified, it will not move within the layout.Set the outer
<div>
toposition: relative
and the inner<div>
toposition: absolute
. This should work for you.