2011-01-08 70 views
0

我需要居中一些相对于容器的绝对定位元素。阅读了大量文章后,似乎像下面这样的结构将会完成这项工作。唯一的问题是IE7。不知何故,item1(div元素)的宽度在IE7上收缩为0(即使宽度已明确指定)。 item2在所有浏览器中工作正常。我的问题是为什么在这种情况下,IE7不尊重block元素的宽度样式?你知道任何解决方法或修复?需要帮助水平居中绝对定位的DIV

<div style="position: relative; width: 500px; height: 400px; border: thin dotted green;"> 
    <div style="position: absolute; top: 0px; left: 50%; height: 0px;"> 
     <div id="item1" style="position: relative; display: inline-block; left: -50%; border: thin dotted green; width: 300px; height: 30px;"></div> 
    </div> 
    <div style="position: absolute; top: 50px; left: 50%; height: 0px;"> 
     <input id="item2" type="button" value="Button" style="position: relative; display: inline-block; left: -50%;"> 
    </div> 
</div> 

我使用上述代码制作了jsfiddle。感谢您的输入。

回答

1

添加min-width = 300px;所以这行代码看起来像

<div style="position: relative; display: inline-block; left: -50%; border: thin dotted green; width: 300px;min-width:300px; height: 30px;"></div> 
+0

谢谢亚历克斯。这工作完美。不知道为什么IE7不占用宽度,但最小宽度的伎俩。 – 2011-01-09 00:04:47