2013-02-01 11 views
3

的div我有這個網站:定心體內

<body> 

<div id="maindiv" class="rounded"> 
something written 
</div> 

</body> 

這個CSS:

body { 
background-image: url('stjohnhusimages/green gradient.png'); 
background-repeat: no-repeat; 
width:1024px; 
height:1375px; 
background-size: cover; 
text-align:center; 
} 

#maindiv { 
width:900px; 
height:1275px; 
margin-left: auto; 
margin-right: auto; 
margin-top: 40px; 
background-color:#404853; 
} 

但主要的div還是會被左對齊。想知道我做錯了什麼。

感謝您的幫助!

+2

它看起來並不像它,http://jsfiddle.net/tqT6W/你能後的截圖或者活生生的例子? – Brandon

+0

是啊,它也似乎並沒有左對我http://codepen.io/anon/pen/qLIKt –

+0

是否有'圓形'類的任何CSS? – paul

回答

2

如果你對自己的身體施加了一個約束,那麼div就只會在這個範圍內居中。

寬度/高度應在div上設置,而不是在body上設置。

body { 
background-image: url('stjohnhusimages/green gradient.png'); 
background-repeat: no-repeat; 
background-size: cover; 
text-align:center; 
} 

Demo

0

是沒有必要有固定的寬度尺寸的身體,如果身體固定寬度內的div不會居中並且將取決於它的大小。

對於DIV居中應該清除體內的固定寬度

body { 
background-image: url('stjohnhusimages/green gradient.png'); 
background-repeat: no-repeat; 
background-size: cover; 
text-align:center; 
}