0

我没有太多的 HTML/CSS 经验,但我试图获得一个简单的布局,其中我有一个居中的 div 作为页面容器。我已经尝试查找其他示例,但我无法弄清楚为什么我的不起作用(标题出现但左对齐,我希望它居中):

html:

<!DOCTYPE html>
<html>
<head>
  <title>title</title>
  <script type="text/javascript" src="script.js"></script>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div id="container">
    <div id="header"></div>
  </div>
</body>
</html>

CSS:

body {
    background: #fff;
    font-family: Helvetica, Arial, sans-serif;
    margin: 0;
    padding: 0;
    text-align: center;
}

#container {
    background: #bbb;
    width: 800px;
    margin: 0, auto;
}

#header, #footer {
    background: #333;
    height: 40px;
}
4

4 回答 4

2
margin: 0, auto;

应该

margin: 0 auto;
于 2013-01-30T10:58:20.737 回答
0

删除逗号margin

margin: 0 auto;

代替

margin: 0, auto;
于 2013-01-30T10:59:06.387 回答
0

你有一个 , 之间margin:0auto;这是一个无效的CSS 术语

margin: 0, auto;

需要是

margin: 0 auto;
于 2013-01-30T10:59:14.500 回答
0

检查这个演示

body {
    background: #fff;
    font-family: Helvetica, Arial, sans-serif;
    width: 100%; /* You need to mention the width here (Good way)*/
    padding: 0;
    text-align: center;
}

#container {
    background: #bbb;
    width: 800px;
    margin: 0 auto;  /* You are doing wrong here (margin: 0, auto;) */
}

#header, #footer {
    background: #333;
    height: 40px;
}

margin: 0 auto;(有效)而不是margin: 0, auto;(无效)

于 2013-01-30T11:04:49.817 回答