我看过其他一些答案,但无法让它们适用于我的特定情况。我已经简化并包含了下面的整个代码块。基本上它只是在蓝色框中显示文本。我想知道如何使框居中。
<!DOCTYPE html>
<html>
<head>
<style type="text/css"><!--
#content {
width:500px;
padding: 10px 10px 10px;
background-color: #D1ECFF;
position: relative;
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
}
--></style>
</head>
<body>
<div id="content">
<h2>Create</h2>
</div>
</body>
</html>
因为#content
有宽度,所以可以将左右边距设置为auto
。这将使元素在其父元素中居中。一小段代码 margin: 0 auto;
将上/下边距设置为零,将左/右边距设置为自动。
http://jsfiddle.net/vL5r2/
#content {
margin: 0 auto;
width:500px;
...
我是一名优秀的程序员,十分优秀!