2014-03-03 142 views
0

这是失败的jsfiddle link。我想有buttonCenter位于黑盒子下面的图片中格:Div定位 - Css

enter image description here

如何做我必须要改变的CSS类buttonCenter

#buttonCenter { 
    width: 250px; 
    height: 100px; 
    margin-right:auto; 
    margin-left:auto; 
    margin-top:100px; 
    background-color:gray; 
} 

谢谢提前

回答

4

您将需要position: relative;并添加z-index(仅适用于一个更安全的一侧)以及..

#buttonCenter { 
    width: 250px; 
    height: 100px; 
    margin-right:auto; 
    margin-left:auto; 
    margin-top:100px; 
    background-color:gray; 
    border: 1px solid #000; 
    z-index: 1; 
    position: relative; 
} 

Demo

虽然想告诉你的是,定位只是奇怪,你是漂浮在没有更好的理由的元素。

例如,要为#row1#row2不需要作为它们占用整个水平空间#buttonsContainer施加float: left;

不要使用id识别每个元素,更好地利用类,这样就可以控股共同风格的元素之间的共同class,因为你不能在一个文档中使用相同的id,他们应该是唯一的。

而且,使用的是巨大的利润,可以考虑使用position: absolute;代替

+1

正确的,良好的工作。 +1 – Ruddy

+0

非常感谢,很好的回答! – anilca

+0

@anilca您好,如果您希望摆脱不必要的浮动,请确保使用'clear:both;'清除它们,尤其是使用浮动元素的#buttonsContainer容器,请阅读我的答案[here]( http://stackoverflow.com/questions/12871710/why-clear-both-css),知道为什么这是必要的 –