【问题标题】:IE6/7 divs float without width problem when cleared (in a fixed width parent div)IE6/7 div 清除时浮动没有宽度问题(在固定宽度的父 div 中)
【发布时间】:2020-07-09 17:41:24
【问题描述】:

HTML:

<div id="container">
  <div id="div1">div one floats to the left</div>
  <div id="div2">div two floats to the left</div>
</div>

CSS:

#container{ width:200px; background:gray; }
#div1 { float:left; background:red;}
#div2 { float:left; background:green; clear:left;}

在 IE6/7 中:

注释:

我希望 #div1 和 #div2 等子 div 的宽度根据其内容自动设置。

由于使用 inline-block 似乎 line-height 不起作用,我使用 float。

我尝试在#div1 之后添加一个空的clear div,而不是在#div2 中添加一个clear,这样就可以了。

还有其他更简单的解决方案吗?有什么错误?感谢您的帮助。

【问题讨论】:

  • 解决什么问题?这是你想要的图片还是你得到的图片?
  • 绿色的div2在IE6/7下多行显示,这不是我想要的。

标签: html css css-float


【解决方案1】:

采用您的解决方案...

只是不要在浮动 div 上清除,您是在告诉它浮动 也要被清除。有点混乱!

<style>
    #container{ width:200px; background:gray;}
    #div1 { float:left; background:red;}
    #div2 { float:left; background:green; }
</style>


<div id="container">
  <div id="div1">div one floats to the left</div>
  <br style="clear:left" />
  <div id="div2">div two floats to the left</div>
  <br style="clear:left" />
</div>

如果您不放置第一个&lt;br /&gt;,ie6/7 将尝试将第二个 div 放在剩下的小空间中。如果你清除 div,它会将它放在一个新行上,但给它的长度与它仍然在第一个 div 的右侧一样。

在我看来,您的解决方案既好又简单,您可以使用它。否则,这里有两种选择:

备选方案 1

内联块有效...如果您使用非块元素开头(不确定为什么)。

<style>
    #container{ width:200px; background:gray;}
    #div1 {  background:red; display:inline-block; line-height:30px;}
    #div2 { background:green; display:inline-block;width:auto; }
</style>
<div id="container">
  <span id="div1">div1</span>
  <span id="div2">div2 doesnt float blablabla</span>
</div>

备选方案 2

使用表格(不是很好,但仍然有效!)

<style>
    #container{ width:200px; background:gray;}
    #div1 {  background:red;}
    #div2 { background:green; }
</style>
<div id="container">
  <table><tr><td id="div1">div one floats to the left</td></tr></table>
  <table><tr><td id="div2">div two floats to the left</td></tr></table>
</div>

【讨论】:

  • 谢谢克拉兹!我把内联块误认为是内联的。内联块导致 100% 宽度而内联自动宽度。 line-height 适用于 inline-block,但不适用于 inline。也许我们可以使用内联来模拟行高。
猜你喜欢
  • 2012-10-03
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多