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

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

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

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

你能帮我做这个的CSS吗?


当前回答

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

overflow:auto; 

更新:

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

家长:

display: table;

孩子:

display: table-row;

其他回答

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

下面的代码对我有用。

css

.parent{
    overflow: auto;
} 

html

<div class="parent">
    <div class="child1">
    </div>
    <div class="child2">
    </div>
    <div id="clear" style="clear:both;"></div>
</div>

我使用这个CSS来父,它工作:

min-height:350px;
background:url(../images/inner/details_middle.gif) repeat-y 0 0 ;
padding:5px 10px;   
text-align:right;
overflow: hidden;
position: relative;
width: 100%;

使用height: auto或在父元素上使用min-height都可以。为了避免水平滚动条,可以通过在子元素上使用box-sizing: border-box或在父元素上使用overflow: hidden来避免由于填充或边框而导致的水平滚动条。 至于水平滚动条由于宽度的身体,如果你是使用宽度:100vw代替宽度:100%。

Add

clear:both; 

添加到父div的css中,或者在父div的底部添加一个div,即清除:both;

这是正确答案,因为overflow:auto;可能工作于简单的网页布局,但会混乱的元素开始使用的东西,如负边距等