IE7-/Win: A box with position:absolute next to a float may disappear

IE7-/Win: 在浮动元素后的定义了“position:absolute”的元素框可能会消失

原文:http://www.brunildo.org/test/IE_raf3.html

猫鱼:“box”一词译为“框(元素框)”而不是“盒子”,虽然“box model”很多地方译为“盒模型”。

猫鱼:为了更直观,本猫增加了在Opera和IE 7/6 内的截图,同时调整了原例中示例元素的宽度,其他未变。

猫鱼:截图中IE 6的效果使用的是 IE Tester v 0.3.2

In the following test cases a relative positioned box contains, in the source code:

在下面的示例的源代码中,一个相对定位框中包含:

In IE7-/Win, depending on the source order of these contained elements and sometimes on other factors like the hasLayout status of the clear and the width of the float, the absolutely positioned box may disappear. Before any case the contained elements and their source order are stated, as well as the result.

在IE 7-/WIN(Windows IE 7.0及更早的版本)中,有时候由于某些原因,绝对定位的元素框会消失,例如触发了hasLayout属性的清除浮动元素、设定了宽度的浮动元素等等。导致这样的问题,其原因是源文件内包含一定顺序的元素。

Notes: The relative container has a fixed width, so it has hasLayout. Without this, there are further problems besides the disappearing of the AP box. hasLayout also makes the container to enclose the float even in absence of the clear element. Such enclosing is obtained in other browsers with a :after content.

注:包含容器设定固定的宽度,为了在IE内触发hasLayout属性。如果不这样的话,除了绝对定位元素消失以外,还会有其他的问题。hasLayout也会使容易在没有清除元素的情况下包含浮动元素。对于其他浏览器通过使用“:after”来闭合浮动元素。

猫鱼:关于hasLayout属性的M$官方资料[英文]

猫鱼:关于闭合浮动元素的问题,可以阅读闭合浮动元素的方法与差别

Test 0: AP box only. OK (Note: RP box has no in-flow content, its height collapses.)

Test 0: 只有绝对定位框. OK (Note: 绝对定位框没有普通流(in-flow指positon:static或relative)的内容所以高度被压缩,只剩下边框。)

Test 1: AP box, float, clear (with hasLayout.) AP box doesn't show up.

Test 1: 绝对定位框, 浮动框, 清除框(触发了hasLayout属性),绝对定位框无法正常显示

图1:Test 1 在不同浏览器内的实际情况

Test 1

Test 1b: AP box, float, clear (with NO hasLayout.) OK.

Test 1b: 绝对定位框, 浮动框, 清除框(没有hasLayout) 正常。

Test 2: float, AP box, clear (with hasLayout.) AP box doesn't show up.

Test 2: 浮动框, 绝对定位框, 清除框(有hasLayout),绝对定位框无法正常显示(现象同图1)

Test 2b: float, AP box, clear (with NO hasLayout.) OK.

Test 2b: 浮动框, 绝对定位框, 清除框(没有hasLayout) 正常。

Test 3: float, clear (with hasLayout), AP box. OK.

Test 3: 浮动框, 清除框(有hasLayout), 绝对定位框 正常。

Test 4: AP box, float (narrower than container.) OK.

Test 4: 绝对定位框,浮动框(小于容器) 正常。

Test 4a: AP box, float width:100%. AP box doesn't show up (in IE6-.)

Test 4a: 绝对定位框,浮动框(width:100%)。绝对定位框不显示(IE 6-)

图4a:Test 4a 在不同浏览器内的实际情况

Test 4a

Test 4b: AP box, float width:100% margin-right:-3px. OK.

Test 4b: 绝对定位框,浮动框(width:100%; margin-right:-3px)正常。

Test 5: float (narrower than container), AP box. OK

Test 5: 浮动框(小于容器),绝对定位框 正常。

Test 5a: float width 100%, AP box. AP box doesn't show up (in IE6-.)

Test 5: 浮动框(width:100%),绝对定位框。绝对定位框不显示(IE 6-)(同图4a)。

Test 5b: float width 100% margin-right:-3px, AP box. OK.

Test 4b: 浮动框(width:100%; margin-right:-3px),绝对定位框。正常。

The above not working cases (1, 2 in IE7-; 4a, 5a in IE6-) are fixed if there is an extra box between the AP box and the float.

如果一个额外的元素框位于绝对定位框和浮动框之间,肯定会出错(例1、2 在 IE7- 内; 例4a、5a 在 IE6-内)

Test 6: Like 1, with an extra box between AP and float. OK.

Test 6: 类似Test 1,在绝对定位框和浮动框之间有个额外的框。正常

 

Test 7: Like 2, with an extra box between AP and float. OK.

Test 7: 类似Test 2,在绝对定位框和浮动框之间有个额外的框。正常

 

Test 8: Like 4a, with an extra box between AP and float. OK.

Test 8: 类似Test 4a,在绝对定位框和浮动框之间有个额外的框。正常

 

Test 9: Like 5a, with an extra box between AP and float. OK.

Test 9: 类似Test 5a,在绝对定位框和浮动框之间有个额外的框。正常

 

The extra box that fixes the problem can be empty.

额外的元素框(可以是空元素)就可以解决这个问题。

Test 6b: Like 1, plus an empty box between AP and float. OK.

Test 6b: 类似Test 1,在绝对定位框和浮动框之间插入空元素框。正常

Test 7b: Like 2, plus an empty box between AP and float.

Test 7b: 类似Test 2,在绝对定位框和浮动框之间插入空元素框。正常

The extra box that fixes the problem, can be put around the AP box or around the float, instead of between them.

额外的框解决了这个问题,还可以不放在他们之间,而是让这个框包围绝对定位框或者包围浮动框。

Test 10: Like 2, with an extra box around the AP box

Test 10: 类似Test 2,额外的框包围绝对定位框。正常

Test 11: Like 2, with an extra box around the float

Test 11: 类似Test 2,额外的框包围浮动框。正常

To fix the problem the extra box must "separate" the AP from the float.

解决这个问题的关紧就是要“分隔”绝对定位框和浮动框。

Test 12: Like 2, plus a box before the float and the AP. AP box doesn't show up.

Test 12: 类似Test 2,把额外的框放在绝对定位框和浮动框之前。绝对定位框不显示。

 

图12:Test 12 把额外的框放在绝对定位框和浮动框之前。绝对定位框不显示。

Test 12

Conclusions 结论

An AP box sometimes disappears when in the source code it is the preceding or the following sibling of a float. This condition doesn't always trigger the problem, but seems necessary for the problem to occur. So any box (even an extra dummy div) "separating" the AP box from the float in the source code (either being between them, or wrapping one of them) fixes the problem.

当一个绝对定位的框的前一个或者后一个兄弟元素是浮动框的时候,这个绝对定位框可能会消失。虽然这种情况不是一定出现,但是也要重视解决。因此,任意的元素框(甚至可以是空的div)将浮动框和绝对定位框在源代码中“分隔”开(放在两个框之间或者包围其中之一都可以),就可以解决这个问题。

Some cases (4a, 5a) require as further trigger that the float has 100% width. They are easily fixed giving to the float a -3px negative margin on the side opposite to the float direction (4b, 5b). These cases do not occur in IE7 (which suggests that a different bug could be involved.)

在某些测试中(4a, 5a)设定浮动元素的宽度(width)为100%才会出现此问题。可以设定与浮动方向相对方向的-3px边距(4b, 5b)来解决这个问题。在IE 7内不存在这个错误现象(说明这是由不同的bug引发的)。

Other cases (1, 2) require as further trigger a clear element with hasLayout. They could be fixed avoiding hasLayout on the clear element (1b, 2b), or omitting it altogether (4, 5). However these modifications are not always applicable. Moreover they may both cause other problems. See IE7- double top padding on a clear with no hasLayout and related bugs. Omission of the clear causes the margin-bottom of the float to be ignored (examples at floats and containers.)

另一些测试(1, 2)则是在清除浮动的元素触发了hasLayout属性时才发生。这种情况可以通过避免触发清除元素的hasLayout或者去除该元素(4, 5)来解决。然而这些修改并不一定适用。此外,他们还可以引起其他问题。参阅:IE7内没有hasLayout属性的清除元素造成双倍上补白(top padding)的问题[英]。浮动元素的下边距(margin-bottom)消失的问题原因也很明确:浮动和容器[英]

As said previously, there are cases when the problem does not occur, even in presence of the triggering conditions. This could be related to the fact that the problem is "time-dependent", i.e. it depends on the timing of data arrival and on the rendering speed, see IE7-/Win: Time dependence of the disappearing of absolutely positioned boxes near to floats.

正如以前所说的,有时触发条件存在也没有发生问题。这可能是这一事实有关的问题是“时变” ,即它取决于数据的到达时间和渲染的速度,参阅:IE7-/Win: 浮动框旁的绝对定位框消失依赖于时间[英]

回到猫窝Blog