我尝试了一切,但没有找到解决方案。最好的方法是什么?
感谢您的帮助。
您必须使用JavaScript,因为当将要素放置在绝对的位置时,它们不会影响父母,就好像它们被从文件流中删除一样。
there是使用的功能
function adjustHeight() {
var column = document.querySelector(".column");
var children = document.querySelectorAll(".column .child");
var maxHeight = 0;
children.forEach(function(child) {
const rect = child.getBoundingClientRect();
if (rect.bottom > maxHeight) {
maxHeight = rect.bottom
}
})
column.style.height = maxHeight + "px"
}
adjustHeight()
.column {
border: 1px solid red;
position: relative;
/* or not */
}
.child {
position: absolute;
top: 20px;
left: 50px;
width: 40px;
height: 40px;
background: green;
}
.child.child2 {
top: 70px;
background: yellow;
}
<div class="column">
<div class="child"></div>
<div class="child child2"></div>
</div>