3

我有这个html:

<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 仍然向左对齐。想知道我做错了什么。

谢谢你的帮助!

4

2 回答 2

2

如果您对身体施加约束,则 div 只会在其中居中。

宽度/高度应该在 div 上设置,而不是在 body 上。

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

演示

于 2013-02-01T20:03:21.090 回答
0

没有必要对 body 有一个固定的宽度大小,如果 body 是固定宽度的,内部 div 将不会居中,并且取决于它的大小。

对于 div 居中应该删除 body 的固定宽度

body {
background-image: url('stjohnhusimages/green gradient.png');
background-repeat: no-repeat;
background-size: cover;
text-align:center;
}
于 2013-02-01T20:08:55.557 回答