简体   繁体   English

IE7忽略绝对定位div后的div中的边距

[英]IE7 ignoring margin in a div following an absolute positioned div

I have two divs inside a container, the first one has absolute positioning. 我在容器内有两个div,第一个具有绝对定位。 In ie7, the second div apparently ignores the top margin. 在ie7中,第二个div明显忽略了上边距。 Padding seems to work fine, but for visual reasons I have to use margin. 填充似乎工作正常,但出于视觉原因,我必须使用保证金。

I know the culprit is the absolute positioned div because if i remove it the following div works fine. 我知道罪魁祸首是绝对定位div,因为如果我删除它,以下div工作正常。

This is only happening in ie7 (not even in ie6). 这只发生在ie7(即使在ie6中)。

Help! 救命!

Edit: I just found a solution which consists of giving the parent div padding-top just for ie7. 编辑:我刚刚找到了一个解决方案,它包括为ie7提供父div填充顶部。 So I would just like to know why does this happen, and if there is one, a cleaner solution, but I dont need more dirty hacks.. 所以我想知道为什么会发生这种情况,如果有的话,一个更清洁的解决方案,但我不需要更多的肮脏黑客..

This is what we call margin collapsing. 这就是我们所说的保证金崩溃。 You could try to positioning the second div too. 您也可以尝试定位第二个div。

You could find more information about margin collapsing. 您可以找到有关边距折叠的更多信息。

Just check the conditions below if you have any of them in your code before start reading the whole article. 在开始阅读整篇文章之前,如果您的代码中有任何条件,请检查以下条件。

BODY elements never take part in margin collapsing, since they are considered magical, which means sometimes a strange gap does not show up in Internet Explorer when it does in other browsers, when the collapse happens with the top of the BODY. BODY元素从不参与边缘折叠,因为它们被认为是神奇的,这意味着当在其他浏览器中,当崩溃发生在BODY的顶部时,Internet Explorer中有时会出现奇怪的间隙。 This is usually easy to solve; 这通常很容易解决; just prevent the margin collapse for the other browsers, and it works in Internet Explorer too. 只是防止其他浏览器的边距崩溃,它也适用于Internet Explorer。 (Note that the HTML element's margins never collapse in any browser, this is correct behaviour.) (请注意,HTML元素的边距永远不会在任何浏览器中崩溃,这是正确的行为。)

In rare cases, margin collapsing where an inner element has a bottom border and an outer container has a bottom border, can cause the background of an intermediate element to spill into the container in Internet Explorer. 在极少数情况下,边距折叠,其中内部元素具有底部边框,外部容器具有底部边框,可能导致中间元素的背景溢出到Internet Explorer中的容器中。

The more problematic bug is caused by Internet Explorer's strange hasLayout behaviour. 更有问题的错误是由Internet Explorer的奇怪的hasLayout行为引起的。 This is a fundamental bug in Internet Explorer 7- and affects several other things as well, but this article will only deal with margin collapsing. 这是Internet Explorer 7中的一个基本错误,也会影响其他几个方面,但本文仅讨论边距折叠问题。 Setting certain styles on an element makes it "have layout" (a concept unique to Internet Explorer, and not compliant with any standards). 在元素上设置某些样式使其“具有布局”(Internet Explorer独有的概念,并且不符合任何标准)。 The most common style that causes a problem is width. 导致问题的最常见的风格是宽度。 When an element hasLayout it suddenly assumes a minimum height of 1em, and if set to something less in Internet Explorer 6, such as 0.5em, it still uses 1em. 当一个元素hasLayout时,它突然假定最小高度为1em,如果在Internet Explorer 6中设置为较小的值,例如0.5em,它仍然使用1em。

An element has layout when one of the following conditions is true: 当满足下列条件之一时,元素具有布局:

  • It has a width and/or a height specified 它具有指定的宽度和/或高度
  • It is an inline-block (display: inline-block) 它是一个内联块(显示:内联块)
  • It has absolute positioning (position: absolute) 它具有绝对定位(位置:绝对)
  • It is a float (float: left, float: right) 它是一个浮点数(浮点数:左,浮点数:右)
  • It is a table element 它是一个表元素
  • It is transformed (style="zoom: 1") 它被转换(style =“zoom:1”)

Height usually does not cause a problem, since setting height will prevent collapsing in other browsers anyway. 高度通常不会导致问题,因为设置高度无论如何都会阻止其他浏览器崩溃。 However, triggering hasLayout on a nested element where the parent has prevented margin collapsing using borders or padding, can cause margins to disappear, or to collapse through the parent irrespective of the padding or borders. 但是,在嵌套元素上触发hasLayout,其中父级已使用边框或填充阻止边距折叠,可能导致边距消失,或者无论填充或边框如何都会通过父级折叠。 Generally, hasLayout is a mess, and it is best to avoid it in places where margins are critical. 通常,hasLayout是一个混乱,最好在边距很关键的地方避免它。

I hope this will help you to go through with your problems. 我希望这能帮助你解决问题。

In my case nothing above helps. 就我而言,上面没有任何帮助。 I use the additional DIV between these elements. 我在这些元素之间使用了额外的DIV。 This additional DIV has clear: both, flot: none etc. See http://starikovs.com/2010/12/24/ie7-margin-top-and-absolute-pos-element/ for more info. 这个额外的DIV有明确的:两者,flot:none等。有关详细信息,请参阅http://starikovs.com/2010/12/24/ie7-margin-top-and-absolute-pos-element/

I used position:static; 我使用的位置:静态; on the larger div (with position:relative on the content inside) on the ignored margin for an IE7 fix. 对于IE7修复程序忽略的边距上较大的div(位置:内部内容的相对值)。 The smaller left hand side column remained position:absolute. 较小的左侧列保持位置:绝对。 This was used on a Jquery vertical tab. 这是在Jquery垂直选项卡上使用的。

http://code.google.com/p/jquery-vert-tabs/downloads/detail?name=jQuery%20Vertical%20Tabs%201.1.4.zip&can=2&q= http://code.google.com/p/jquery-vert-tabs/downloads/detail?name=jQuery%20Vertical%20Tabs%201.1.4.zip&can=2&q=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM