我有一个类似这样的页面结构:

<body>
  <div id="parent">
    <div id="childRightCol">
      /*Content*/
    </div>
    <div id="childLeftCol">
      /*Content*/
    </div>
  </div>
</body>

我想为父div扩大高度时,内部div的高度增加。

编辑: 一个问题是,如果子内容的宽度扩展超过浏览器窗口的宽度,我目前的CSS把一个水平滚动条上的父div。我希望滚动条是在页面级别。目前我的父div被设置为溢出:auto;

你能帮我做这个的CSS吗?


当前回答

与其设置height属性,不如使用min-height。

其他回答

试着给父母的高度max-content。

.parent{
  height: max-content;
}

https://jsfiddle.net/FreeS/so4L83wu/5/

与其设置height属性,不如使用min-height。

试试这个方法,对我很管用。

overflow:auto; 

更新:

还有一个有效的解决方案:

家长:

display: table;

孩子:

display: table-row;

使用像自清除div这样的东西非常适合这种情况。然后你只需在父类上使用一个类…如:

<div id="parent" class="clearfix">

如果你在#childRightCol和#childRightCol中的内容是向左或向右浮动的,只需将其添加到css中:

#childRightCol:before { display: table; content: " "; }
#childRightCol:after { display: table; content: " "; clear: both; }
#childLeftCol:before { display: table; content: " "; }
#childLeftCol:after { display: table; content: " "; clear: both; }