2012-09-20 413 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>​