简体   繁体   English

垂直负边距在 IE8 中不起作用?

[英]Vertical negative margin not working in IE8?

first time asking my own question here:)第一次在这里问我自己的问题:)

I'm working on a website and I tried extending it vertically to the bottom of the client window using the usual trick:我在一个网站上工作,我尝试使用通常的技巧将它垂直扩展到客户端 window 的底部:

html, body { height: 100%; }
.container { min-height: 100%; }

I then wanted to add some headers above the main content and a sticky footer at the bottom.然后我想在主要内容上方添加一些标题,并在底部添加一个粘性页脚。 I wrapped both of these in their own containers and pulled the header onto the top like so:我将这两个包装在自己的容器中,然后将 header 拉到顶部,如下所示:

.top { position: relative; z-index: 1; height: 168px; }
.end { height: 58px; }

Knowing the height of the header and sticky footer, I then set out to adjust the total height of the page such that it would fill up the client window exactly (no scrollbars) unless the content was too long.知道了 header 和粘性页脚的高度后,我开始调整页面的总高度,以便它将完全填满客户端 window(没有滚动条),除非内容太长。 I did this with negative margins:我以负边距做到了这一点:

.container { overflow: hidden; min-height: 100%; margin-top: -164px; margin-bottom: -58px; }
.container-in { margin-top: 164px; margin-bottom: 58px; }

The second container is within the first one and it's where I put the actual content of each page.第二个容器在第一个容器内,它是我放置每个页面的实际内容的地方。

So, this works just fine on Firefox 4/5 - Absolutely nothing is off, it's just as intended.因此,这在 Firefox 4/5 上运行良好 - 绝对没有任何问题,正如预期的那样。 Chrome also seems ok.铬似乎也不错。 However, on IE8, it's ignoring the negative margin on.container (I checked with developer tools).但是,在 IE8 上,它忽略了 .container 上的负边距(我使用开发人员工具进行了检查)。 The container begins after the.top, and as a result there's a 164px gap between the.top and the.container-in because of the.container-in's margin.容器在.top 之后开始,因此在.top 和.container-in 之间存在164px 的间隙,因为.container-in 的边距。

And the funny thing is - if I switch IE8 to IE7 compatibility mode, this problem no longer occurs, Negative margins behave just fine in IE7 mode, but of course a bunch of other stuff breaks.有趣的是 - 如果我将 IE8 切换到 IE7 兼容模式,则不再出现此问题,负边距在 IE7 模式下表现得很好,但当然还有其他一些东西会中断。 so telling IE to use compatibility mode isn't an option.所以告诉 IE 使用兼容模式不是一种选择。

Any ideas on how to work around this problem/use a different solution to obtain the same effect in all browsers (IE7 not required)?关于如何解决这个问题/使用不同的解决方案在所有浏览器中获得相同效果的任何想法(不需要 IE7)? Am I doing anything wrong?我做错什么了吗?

EDIT: After some more fun and games I discovered that by replacing the negative margins with negative top: coordinates (and setting all containers as relative) it works perfectly on IE8 but now it leaves a 222px gap below the html container in firefox (according to firebug).编辑:经过更多的乐趣和游戏后,我发现通过用负 top: 坐标替换负边距(并将所有容器设置为相对)它在 IE8 上完美运行,但现在它在 firefox 中的 html 容器下方留下 222px 的间隙(根据萤火虫)。 Confused!使困惑!

EDIT2: I believe I know what's wrong here, technically speaking. EDIT2:从技术上讲,我相信我知道这里出了什么问题。 Internet Explorer 8 thinks the negative margin is 'overflow', and since overflow: hidden, it kills the margin. Internet Explorer 8 认为负边距是“溢出”,并且由于溢出:隐藏,它会杀死边距。 If I remove overflow: hidden it no longer has this behavior, but it breaks the rest of the design.如果我删除溢出:隐藏它不再有这种行为,但它打破了设计的 rest。 Anyone has any ideas yet?有人有任何想法吗?

About Conditional Comments .关于条件注释 How To Create an IE-Only Stylesheet .如何创建仅限 IE 的样式表

Try to set style only for IE8 with negative top coordinates:尝试仅为具有负顶部坐标的 IE8 设置样式:

<!--[if gte IE 8]>
<style>
.container { top: -164px; }
</style>
<![endif]-->

Don't have time to go beyond this: min-height is buggy in IE7.没有时间 go 超出此范围:最小高度在 IE7 中是错误的。 Go here for more info on that: http://www.webdevout.net/browser-support-css Go 在这里了解更多信息: http://www.webdevout.net/browser-support-css

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

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