在IE 8或9中,负上边距不起作用

时间:2011-03-31 22:00:18

标签: css internet-explorer

我有margin-top:-200px的div。我希望div在它上方的div上方/后方移动。

到目前为止,IE以外的所有浏览器都很好用。 margin-top:200px有效,所以我知道这不是一个崩溃的保证金问题。

我不知道这里有什么错误吗?

5 个答案:

答案 0 :(得分:25)

IE不喜欢负边距并且不能正确渲染它们。相对或绝对地定位元素并改为使用top: -200px

注意:定位它们可能会显着改变布局,您可能需要重新设计样式。

答案 1 :(得分:14)

负边距隐藏div ... 这是诀窍 使用zoom:1,position:relative

<强>问题:

.container{
padding: 20px;
}
.toolbar{
margin-top: -10px ;
}

在工具栏div的IE红色区域隐藏自己。即使我们使用zoom:1。要摆脱这个问题我们需要添加位置:相对也是。

<强>解决方案:

所以你的css课程将成为

.container{
padding: 20px;
}
.toolbar{
margin-top: -10px ;
zoom: 1;
position: relative;
}

http://trickyclicks.com

答案 2 :(得分:3)

如果以上情况没有帮助:请确保您的违规div附近有一个div。现在向违规div添加100%的宽度并将其浮动到左侧。像这样。摆脱了我所有的负面边缘,即困境......

div.container {}
div.offender /*inside div.container*/ {
  width: 100%;
  float: left;
  margin-bottom: -20px;   /* ie fix */
  zoom: 1;                /* ie fix */
  position: relative;     /* ie fix */
  display: block;
}

答案 3 :(得分:2)

给予相对位置。内联样式是可取的。如果使用外部css,可能会出现问题。

答案 4 :(得分:1)

为了支持IE中的负边距,我修复了display: table;的类似问题。 zoom: 1;position: relative;等其他修补程序并不总是有效(至少在我的经验中)。如果您只想将此样式添加到IE,我建议使用https://modernizr.com/

// using comment to show that .no-cssreflections belongs to the html tag
/*html*/.no-cssreflections .container { display: table; } 
相关问题