2012-09-20 72 views
1

我有一个父DIV和2次DIV如下:如何设置父div高度自适应内部div高度?

<div class="parent"> 
    <div style="height:100px; float:left;" > 
     aaa 
    </div> 
    <div style="height:200px; float:left;"> 
     bbb 
    </div> 
</div>​ 

如何设置“父” CSS来适应内div的最大高度?在这种情况下:200px

PS:height=100%height=auto都不起作用。

谢谢。

回答

2

你也可以“溢出:隐藏“在父元素上,尽管如果你想让事情突破那个div(例如负边距,箱形阴影等),你可能会遇到问题。

<div class="parent" style="overflow: hidden;"> 
    <div style="height:100px; float:left;" > 
     aaa 
    </div> 
    <div style="height:200px; float:left;"> 
     bbb 
    </div> 
</div>​ 
+0

应该说...这意味着你不需要“清除”div。 – timkeay

+0

我不认为这对父母的身高有任何影响,是吗? 尝试添加'' - 父母身高似乎仍然是0px .... – peterp

+0

@timkeay'overflow:hidden;'对我有用,谢谢 – JerryCai

3

父div不占用其内容的高度,因为内部div是浮动的。您必须在关闭父div之前清除浮动。试试这个:

<div class="parent"> 
    <div style="height:100px; float:left;" > 
     aaa 
    </div> 
    <div style="height:200px; float:left;"> 
     bbb 
    </div> 
    <div style="clear:both"></div> 
</div>​ 

编辑:你可能想看看这个W3C文章有关花车和结算的深入信息:http://www.w3.org/wiki/Floats_and_clearing

1

我接着peterp给出的链接,并想引用他们提到的方法,它为我工作。这种方法是先在他们的名单和描述为只是 :)

使外divfloat

<div class="parent" style="float:left;"> 
0

您需要添加的风格= “明确:既”

<div class="parent"> 
     <div style="height:100px; float:left;" > 
      asljkdh   
     </div> 
     <div style="height:200px; float:left;"> 
      bbb 
     </div> 
     <div style="clear:both"></div> <!-- Add this Line--> 
    </div>​