2010-07-06 128 views
3

您好我有一个问题,我想不通浮动div。我知道很多人有同样的问题,但我还没有找到一个正常的解决方案。也许你可以帮助我? 我希望左边的div在它的高度增长,当右边的div增长的高度。右边的那个会动态增长,因为文本或其他东西会有不同的大小。浮动DIV高度问题

这是代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"> 


<style> 

#content 
{ 
width:600px; 
height:auto; 
overflow:hidden; 
background-color:#FF3399; 
} 

#content1 
{ 
width:300px; 
background-color:#3333CC; 
float:left; 
} 

#content2 
{ 
width:300px; 
overflow:hidden; 
background-color:#CCFF66; 
} 


</style> 


</head> 

<body> 

<div id="content"> 

    <div id="content1"> 

    1 

    </div> 

    <div id="content2"> 
    2 
    <br/> 
    <br/> 
    <br/> 
    <br/> 
    <br/> 

    </div> 

</div> 



</body> 
</html> 

回答

0

我通常使用jQuery的这个片段..

function equalHeight(group) { 
    var tallest = 0; 
    group.each(function() { 
     var thisHeight = $(this).height(); 
     if(thisHeight > tallest) { 
      tallest = thisHeight; 
     } 
    }); 
    group.height(tallest); 
} 

equalHeight($('.your-divs')); 
0

可以吗?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 

无标题文档

<style> 

     #content 
     { 
      width:600px; 
      height:auto; 
      overflow:hidden; 
      background-color:#FF3399; 
     } 

     #content1 
     { 
      width:300px; 
      background-color:#3333CC; 
     } 

     #content2 
     { 
      width:300px; 
      overflow:hidden; 
      float: right; 
      background-color:#CCFF66; 
     } 


    </style> 


</head> 

<body> 

    <div id="content"> 



     <div id="content2"> 
      2 
      <br/> 
      <br/> 
      <br/> 
      <br/> 
      <br/> 

     </div> 
     <div id="content1"> 

      1 
      <div style="clear: both"></div> 
     </div> 


    </div> 



</body> 

0

这个解决办法是什么我一直在寻找,但我有我想放置一个div在与内容1的底部一个非常重要的问题一个静态的高度和宽度,但我不能这样做,因为该div只是不会去那里的div也可以是一种行为,你不能使用position:absolute,因为content1 div高度不会是静态的,所以它的div不会一直在一个地方。我使用的解决方案帮了我很多,我只是想在content1的底部添加div。所以看起来这个content1在顶部有两个div,第一个在顶部,另一个在底部。

<style> 

    #content 
    { 
     width:600px; 
     height:auto; 
     overflow:hidden; 
     background-color:#FF3399; 
    } 

    #content1 
    { 
     width:300px; 
     background-color:#3333CC; 
    } 

    #content2 
    { 
     width:300px; 
     overflow:hidden; 
     float: right; 
     background-color:#CCFF66; 
    } 


</style> 

<div id="content"> 



    <div id="content2"> 
     2 
     <br/> 
     <br/> 
     <br/> 
     <br/> 
     <br/> 

    </div> 
    <div id="content1"> 

     1 
     <div style="clear: both"></div> 
    </div> 


</div> 

:因为它修复了DIV高度的问题,我提到使用此代码是非常重要的