父div高度不能自适应子div高度的解决方案,自适应div


复制代码 代码如下:

<div id="parent">
<div id="content"> </div>
</div>

当content内容多时,即使parent设置了高度100%或auto,在不同浏览器下还是不能完好的自动伸展。解决方案如下:

复制代码 代码如下:

<div id="parent"> 
<div id="content"></div> 
<div style="font: 0px/0px sans-serif;clear: both;display: block"> </div> 
</div>

在层的最下方产生一个高度为1的空格,可解除这个问题。

另外还有这样写的:<div style="height:1px; margin-top:-1px;clear:both;overflow:hidden;" />或者<br style="clear:both;" />。


CSS控制,子div的高度自适应父div的高度

我以前也遇到过这个问题,我用的是js解决了这个问题。你可以试一下
document.getElementById("aa").style.height=document.getElementById("bb").offsetHeight;
 

div高度不可以自适应

在外面的部分,是不是设置了float:left了?
看下面的示例代码吧。一旦设置了float,相当于到了另外一个位面了。
<!DOCTYPE html>
<html>
<head>
<meta charset="gbk">
</head>
<body>
<div style="width:200px;height:auto;border:2px solid red;">
<div style="width:100px;height:100px;background-color:green">正常div</div>
<div style="width:100px;height:100px;background-color:blue">这是不设置float:left的情况</div>
</div>

<div style="width:200px;height:auto;border:2px solid red;">
<div style="width:100px;height:100px;background-color:green">正常div</div>
<div style="width:100px;height:100px;background-color:blue;float:right">这是设置float:left的情况</div>
</div>

<div style="width:200px;height:200px;;border:2px solid red;float:left">
<div style="width:100px;height:100px;background-color:green">正常div</div>
<div style="width:100px;height:100px;background-color:blue;float:right">这是设置float:left的情况</div>
</div>
<div style="clear:both"></div>
</body>
</html>
 

评论关闭