2014-03-03 146 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),知道爲什麼這是必要的 –