这是我在修复布局时经常遇到的一个问题。我有一个绝对定位的 DIV,我在里面放置了一个子 DIV,它也需要绝对定位。但我真的希望这个子 DIV 相对于父级的行为......这可能吗?或者我需要创建一个wrap-DIV?
<div class="container" style="position:relative;">
<div class="parent" style="position:absolute;">
<!-- this child div will behave relative to .container -->
<div class="child" style="position:absolute;"></div>
</div>
</div>
这是我宁愿避免的“包装”解决方案:
<div class="container" style="position:relative;">
<div class="parent" style="position:absolute;">
<div class="wrapper" style="position:relative;">
<!-- this child div will behave relative to .wrapper-->
<div class="child" style="position:absolute;"></div>
</div>
</div>
</div>
绝对定位的元素相对于其未定位的最近祖先的边缘进行定位(即不是位置:静态)。
由于父元素is位置:绝对,子项将相对于其边缘定位。
所以你似乎在问如何获得你已经拥有的行为。您的第一个示例中的注释似乎不正确。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)