【发布时间】:2017-12-10 17:03:46
【问题描述】:
虽然这看起来很基本,但我无法让它工作。
简单地说,我有一个父元素——相对位置。 然后是一个子元素 - 绝对位置。
如果孩子的身高大于父母的身高,我该如何匹配父母的身高。意思是,如何让父级展开。
这是我正在查看的 js 小提琴 https://jsfiddle.net/wthwawcv/
<div class="parent">
dsfsdfsdfsdf<br>
hellow .. line 2 <br>
hello .. line 3
<div class="child">
this is the child element to be diaplayed
</div>
</div>
.parent {
width:100%;
background: #ff0000;
width:200px;
position: relative;
overflow: hidden;
}
.child {
width:40px;
height:100px;
background: #ffff00;
position: absolute;
right: 0;
top: 0;
}
【问题讨论】:
-
如果子元素是绝对/固定定位的,这对于纯 HTML/CSS 是不可能的。查看此 Stack 问题以了解解决方法:Auto height on parent container with absolute/fixed children