调整父div的大小以匹配绝对定位的子div高度

调整父div的大小以匹配绝对定位的子div高度

问题描述:

我有一个CSS问题:

我在容器中有一个绝对定位的div。容器不会调整到内容的高度。为什么?

I have an absolute positioned div in a container. The container won't resize to the height of the content. Why?

您需要使用JavaScript。使用jQuery你可以做

You need to use JavaScript for this. With jQuery you can do

var parentHeight = $('#parent').height(),
    childHeight = $('#child').height();

if (parentHeight <= childHeight) {
    $('#parent').height(childHeight);
}