IE7中的奇怪浮动行为

Kee*_*ter 12 css css-float internet-explorer-7

我想创建一个带有标题栏的简单框,其中包含标题和一些工具按钮.我有以下标记:

<div style="float:left">
    <div style="background-color:blue; padding: 1px; height: 20px;">
        <div style="float: left; background-color:green;">title</div>
        <div style="float: right; background-color:yellow;">toolbar</div>
    </div>
    <div style="clear: both; width: 200px; background-color: red;">content</div>
</div>
Run Code Online (Sandbox Code Playgroud)

这在Firefox和Chrome中呈现得很好:

http://www.boplicity.nl/images/firefox.jpg

但是,IE7完全混乱并将右侧浮动元素放在页面右侧:

http://www.boplicity.nl/images/ie7.jpg

这可以修复吗?

Mar*_*mic 23

在最外面的div中指定宽度.如果内容div中的宽度意味着这是您的框的总宽度,只需将其添加到最外层的div,并(可选)将其从内容中删除,如下所示:

<div style="float:left; width: 200px;">
    <div style="background-color:blue; padding: 1px; height: 20px;">
        <div style="float: left; background-color:green;">title</div>
        <div style="float: right; background-color:yellow;">toolbar</div>
    </div>
    <div style="clear: both; background-color: red;">content</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这解决了它.但是,可能不会预先知道内容大小.在我的网站上,它通常包含宽度可以增长的表格. (7认同)