根据子元素的高度(绝对位置)增加父元素的高度(相对位置)

Red*_*ddy 6 html css

如何根据具有绝对位置的子元素高度来增加具有相对位置的父元素的高度。

在我下面的示例中,.parent元素的高度显示为0px

PS:我不想使用任何脚本

预期的:

在此输入图像描述

我得到什么:

在此输入图像描述

jsFiddle

HTML:

<div class="parent">
    <div class="child" style="top:20px;">Hello</div>
    <div class="child" style="top:40px;">Some content</div>
    <div class="child" style="top:60px;">Some more content</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.parent{position:relative;background:green;height:100%;border:1px solid #000000;width:250px;}
.child{position:absolute;}
Run Code Online (Sandbox Code Playgroud)

kia*_*ian 1

在父类中更改height:100%height:100px或任意数量

因为绝对位置没有任何高度,所以您应该为父级定义高度。

.parent{
    position:relative;
    background:green;
    height:100px;
    border:1px solid #000000;
    width:250px;
 }
Run Code Online (Sandbox Code Playgroud)