如何让一个元素同时拥有相对位置和绝对位置?

How to make an element have a position of both relative and absolute?

所以我有一个 parent 元素和一个 child 元素,而 child 元素是 child 元素的 parent 元素,如下所示:

<div class="father">

    <div class="child">
        
        <div class="child-of-child"></div>


    </div>
    
</div>

和 CSS 像这样:

.father{
    position: relative;
}

.child{
    position: absolute;
}

.child-of-child{
    position: absolute;
}

但我希望名为 child 的 div 对父亲来说是 position: absolute;,就像现在这样,我也希望它对 position: relative; div 命名为 child-of-child 以便我可以将 child-of-child 移到 child

只要.child绝对定位,任何.child-of-child都可以绝对定位.child。这意味着 .child 不需要被赋予 position: relative 以使 .child-of-child 相对于它绝对定位。

您可以自信地使用您的 CSS。