您当前的位置:首页 > 建站知识 > 编程知识

height:auto;无效时怎么办?

发布时间: 2015-06-26 │ 浏览:3292 

如下代码:
<div style="width:1000px;text-align:left;margin:0 auto;background-color:#FFFFFF;position:relative;">
  <div style="float:left;width:200px;text-align:left;">

 左

  </div>
  <div style="float:right;width:800px;text-align:left;">

 右<br>右<br>右<br>右<br>右<br>右<br>右<br>

  </div>
</div>

要求让外层div高度自动随着内层div的内容自动调整高度,于是把外层改成:
<div style="width:1000px;height:auto;text-align:left;margin:0 auto;background-color:#FFFFFF;position:relative;">
无效,再改成:
<div style="width:1000px;height:100%;text-align:left;margin:0 auto;background-color:#FFFFFF;position:relative;">
还是无效。百思不得其解,最后在论坛中找到答案,原来自适应高度也可以和height属性无关。正确的设置代码为:

<div style="width:1000px;overflow:auto;zoom:1;text-align:left;margin:0 auto;background-color:#FFFFFF;position:relative;">

关键属性是:【overflow:auto;】,而其中的【zoom:1;】是为IE设定的。