我所要做的應該是這樣的:
...但我inner_top
DIV(與紅色背景)是不水平居中,結束了這樣看:
衝突似乎使用display: inline-block'
inner_top
股利,這似乎使垂直爲中心雖然,除了兩個div之間的奇怪間距。
我怎麼能水平和垂直居中另一個div內的兩個div?
這裏是我的代碼:
<!DOCTYPE html>
<html>
<head>
<title>
CSS sucks!!!
</title>
<style type = "text/css">
#outer {
width: 250px;
height: 500px;
border: 5px solid black;
/* for vertically-centering inner divs: */
display: table-cell;
vertical-align: middle;
}
#inner_top {
width: 75px;
height: 175px;
background-color: red;
/* horizontally-centered: */
margin: 0 auto;
/* vertically-center both this and the bottom div:
(but now horizontal-alignment doesn't work on this div!) */
display: inline-block;
}
#inner_bottom {
width: 150px;
height: 150px;
background-color: blue;
/* horizontally-centered: */
margin: 0 auto;
}
</style>
</head>
<body>
<div id = "outer">
<div id = "inner_top">
</div> <!-- end of inner top -->
<div id = "inner_bottom">
</div> <!-- end of inner_bottom -->
</div> <!-- end of outer div -->
</body>
</html>
當我刪除'display:inline-block'時,該示例適用於我... –
以上爲+1。 http://jsfiddle.net/JSWorld/MhbmY/ –
哈哈,太棒了!我不敢相信我沒有嘗試過......呃,我覺得自己像個假人。 @MrLister - 如果您以此作爲答案,我會接受。 –