简体   繁体   English

在IE7中居中绝对定位的div

[英]Centering an absolute positioned div within IE7

I have a relatively simple layout as seen in the simplified version of the code below: 我有一个相对简单的布局,如以下代码的简化版所示:

<div id="protocol_index_body_wrapper">
    <div id="protocol_index_body">
    </div>
</div>

The CSS CSS

#protocol_index_body_wrapper {
    background: url("/images/stripe.png") repeat scroll 0 0 transparent;
    position: absolute;
    top: 120px;
    left: 0px;
    right: 0px;
    bottom: 10px;
}
#protocol_index_body {
    width: 960px;
    margin: 0 auto;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;

}

在此处输入图片说明

The absolute positioning is needed to make sure that both div's expand to the full height of the window. 需要绝对定位以确保两个div都扩展到窗口的整个高度。 The expected behavior is seen in the image above and is present in IE8, Firefox, and Chrome. 上图中显示了预期的行为,并且该行为出现在IE8,Firefox和Chrome中。 However, in IE7 the div which should be centered is flush against the left side. 但是,在IE7中,应居中的div与左侧齐平。 Any ideas how to fix this? 任何想法如何解决这一问题?

Have you tried specifically assigning an auto value to the margin-left and margin-right? 您是否尝试过专门为左边距和右边距分配自动值? This will force both margins to be equal and center the div provided your div is not at 100% width of the page. 如果您的div不在页面的100%宽度上,这将强制两个边距相等并且使div居中。

{
    margin-left: auto;
    margin-right: auto;
}

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

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