IE CSS Bug系列:不正确的浮动伸缩Bug

开发 前端
IE 浏览器不支持很多 CSS 属性是出了名的,即便在支持的部分中,也是有很多 Bug 的。Zoffix Znet 整理了 IE 中的 CSS 问题,有简单的问题示例,也有解决方法。 这个系列共有 58 个指南,70 个解决方案。

影响的IE版本

这个 bug 影响 IE7、IE6

表现

跟随在其他浮动元素之后的浮动元素,设置clear属性时不能正确伸缩

教程编写时间

2009.8.18 21:17:12

描述

这是我在 Gérard Talbot 收集整理的 IE7 Bug 页面发现的bug之一(我大部分案例都是从那来的)。这个bug影响IE7跟IE6,表现就是设置了 clear 属性的浮动元素并不能正确地伸缩。我们来看一下:

演示

示例在这个独立页面

HTML代码:

  1. <div> 
  2.     Here is a &lt;div&gt; having float: left and clear: left. As expected, 
  3.     it takes, it occupies as much width it requires from the body's content 
  4.     area. 
  5. </div> 
  6.   
  7. <div> 
  8.     This is the same type of &lt;div&gt;: a left-floated &lt;div&gt; with 
  9.     clear: left. This &lt;div&gt; should use, should take, should occupy 
  10.     the whole content area of the body node (expected results). However, in 
  11.     Internet Explorer 7, this left-floated &lt;div&gt; with clear: left only 
  12.     takes, only occupies a very narrow chunk of the body's content area 
  13.     (actual results). More text filling. More text filling. More text 
  14.     filling. More text filling. More text filling. 
  15. </div> 
  16.   
  17. <div> 
  18.     Here, a third &lt;div&gt; having float: left and clear: left. Change 
  19.     your browser window viewport's width. 
  20. </div> 
  21.   
  22. <ul> 
  23.     <li> 
  24.     Here is a &lt;div&gt; having float: left and clear: left. As expected, 
  25.     it takes, it occupies as much width it requires from the body's content 
  26.     area. 
  27.     </li> 
  28.   
  29.     <li> 
  30.         This is the same type of &lt;div&gt;: a left-floated &lt;div&gt; with 
  31.         clear: left. This &lt;div&gt; should use, should take, should occupy 
  32.         the whole content area of the body node (expected results). However, in 
  33.         Internet Explorer 7, this left-floated &lt;div&gt; with clear: left only 
  34.         takes, only occupies a very narrow chunk of the body's content area 
  35.         (actual results). More text filling. More text filling. More text 
  36.         filling. More text filling. More text filling. 
  37.     </li> 
  38.   
  39.     <li> 
  40.         Here, a third &lt;div&gt; having float: left and clear: left. Change 
  41.         your browser window viewport's width. 
  42.     </li> 
  43. </ul> 

CSS 代码:

  1. div, li { 
  2.     background-color#ddd
  3.     border1px solid green
  4.     clearleft
  5.     colorblack
  6.     floatleft

在IE6跟IE7中可以发现第二、第三个<li>及<div>不能正确地伸缩。它们被“截”短了,过早伸缩了。据Sjaak Prieste所说(Gérard Talbot在这个bug中称赞了他),原因是这样的,IE首先把该浮动元素与上一个浮动元素渲染在同一行中,然后发现’clear’属性,清除浮动将其移到下一行,但没有重新排布文本。

我的演示中既有<div>也有<li>的原因是,这两种情况的处理方法有点区别。在“解决方案”中会有更多说明。

解决方案

以下是上述bug的解决方法(以类型排序)

解决方法(对清除进行标记)

该方法的时间

Tue Aug 18 21:17:26 2009

可修复的的版本

所有受该bug影响的版本

描述

我找不到一个像样的办法,如果谁有比较好的、相对简洁的办法,请评论给我!下面是我的方法:

修复bug的演示在这个独立页面

HTML Code:

  1. <div> 
  2.     Here is a &lt;div&gt; having float: left and clear: left. As expected, 
  3.     it takes, it occupies as much width it requires from the body's content 
  4.     area. 
  5. </div> 
  6. <span class="ie_fix"></span> 
  7. <div> 
  8.     This is the same type of &lt;div&gt;: a left-floated &lt;div&gt; with 
  9.     clear: left. This &lt;div&gt; should use, should take, should occupy 
  10.     the whole content area of the body node (expected results). However, in 
  11.     Internet Explorer 7, this left-floated &lt;div&gt; with clear: left only 
  12.     takes, only occupies a very narrow chunk of the body's content area 
  13.     (actual results). More text filling. More text filling. More text 
  14.     filling. More text filling. More text filling. 
  15. </div> 
  16. <span class="ie_fix"></span> 
  17. <div> 
  18.     Here, a third &lt;div&gt; having float: left and clear: left. Change 
  19.     your browser window viewport's width. 
  20. </div> 
  21.   
  22. <ul> 
  23.     <li> 
  24.         <div>Here is a &lt;div&gt; having float: left and clear: left. As expected, 
  25.         it takes, it occupies as much width it requires from the body's content 
  26.         area.</div> 
  27.     </li> 
  28.   
  29.     <li> 
  30.         <div>This is the same type of &lt;div&gt;: a left-floated &lt;div&gt; with 
  31.         clear: left. This &lt;div&gt; should use, should take, should occupy 
  32.         the whole content area of the body node (expected results). However, in 
  33.         Internet Explorer 7, this left-floated &lt;div&gt; with clear: left only 
  34.         takes, only occupies a very narrow chunk of the body's content area 
  35.         (actual results). More text filling. More text filling. More text 
  36.         filling. More text filling. More text filling.</div> 
  37.     </li> 
  38.   
  39.     <li> 
  40.         <div>Here, a third &lt;div&gt; having float: left and clear: left. Change 
  41.         your browser window viewport's width.</div> 
  42.     </li> 
  43. </ul> 

CSS Code:

  1. div { 
  2.     background-color#ddd
  3.     border1px solid green
  4.     clearleft
  5.     colorblack
  6.     floatleft
  7. .ie_fix { display: inline-block; } 
  8. .ie_fix { displayblock; } 

看下这边我做的事。在示例中的div部分,我在各div之间插入一个额外的<span>元素,并且通过 {display: inline-block;}给它一个”布局”(layout),然后设置其display属性为block。

因为<li>元素之间插入<span>元素不大合适,在示例的<ul>部分我将每个<li>的内容用一个<div>包起来,然后将那个div设为浮动(注意这边将 li 的浮动删掉了)。

在正常的浏览器里,最初的示例中浮动元素的伸缩可以完全适应其包围元素的变化,而我们的修复版本并不能做到。因而这个解决方法并不完美,不过也许可以帮助到你。

原文链接:http://haslayout.net/css/Incorrect-Float-Shrink-Wrap-Bug

译文链接:http://blog.jobbole.com/47426/

责任编辑:陈四芳 来源: 伯乐在线
相关推荐

2013-10-30 09:57:43

IECSS

2013-10-31 11:12:56

IECSS

2013-10-28 14:01:03

IECSS

2013-10-31 10:59:23

IECSS

2013-10-29 15:20:38

IECSS

2013-10-29 10:32:59

IECSS

2009-08-13 10:12:07

IE的CSS Bug

2010-09-03 09:55:10

CSS伪类hover

2010-08-17 15:38:49

CSS兼容IE7IE8

2023-03-16 23:54:19

服务器vmtoolsd组件

2018-04-09 09:04:59

固态硬盘映射表

2012-10-15 18:19:25

打印机打印机安装

2015-06-29 09:44:55

2011-08-17 13:18:39

Oracle 10g配SID

2010-08-18 09:55:38

IE6

2009-03-28 09:50:02

IE8微软浏览器

2011-07-29 16:55:44

Java 7

2014-11-14 14:03:17

微软安全漏洞bug

2020-08-19 14:22:09

程序员测试互联网

2023-11-15 17:23:30

测试软件开发
点赞
收藏

51CTO技术栈公众号