user1369195
user1369195

Reputation: 93

centered div web layout

I don't have much HTML/CSS experience, but I am trying to get a simple layout going where I have a centered div acting as the page container. I have tried looking up other examples, but I can't figure out why mine doesn't work (the header appears but is left justified, I want it centered):

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;
}

Upvotes: 0

Views: 736

Answers (4)

Sri
Sri

Reputation: 2273

Check this demo

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; (valid) instead of margin: 0, auto; (invalid)

Upvotes: 0

Ladineko
Ladineko

Reputation: 1981

You have a , between margin:0 and auto; this is an invalid CSS term

margin: 0, auto;

needs to be

margin: 0 auto;

Upvotes: 0

psur
psur

Reputation: 4519

Remove comma in margin:

margin: 0 auto;

instead of

margin: 0, auto;

Upvotes: 0

pktangyue
pktangyue

Reputation: 8524

margin: 0, auto;

should be

margin: 0 auto;

Upvotes: 2

Related Questions