Search code examples
centermargin

CSS centering margin:0 auto; issue


Sorry if this was asked before but I read and did not see my mistake.I try to center the #wrapper with margin:0 auto; but nothing happens...the width is set also.

This is the CSS: I want the hole page to be centerd

@charset "utf-8";
body {
    margin-left:0px;
    margin-top:0px;
    margin-right:0px;
    margin-bottom:0px;
    padding:0;
    background-color:#000000;
    text-align:center

}
#wrapper {
    width: 901px;
    ***margin: 0 auto;***
}
#header {
    background-image: url(images/banner.jpg);
    background-repeat: no-repeat;
    float: left;
    height: 233px;
    width: 901px;
}
#menu {
    font-family: Arial, Helvetica, sans-serif;
    background-image: url(images/menu.gif);
    background-repeat: no-repeat;
    text-align: center;
    word-spacing: 70px;
    padding-top: 17px;
    float: left;
    height: 33px;
    width: 901px;
}
#main {
    float:left;
    height:auto;
    width:901px;
}
#leftcol {
    float:left;
    height:auto;
    width:300px;
    padding-top:5px;
    padding-right:0px;
    padding-bottom:0px;
    padding-left:30px;
}
#rightcol {
    float:right;
    height:auto;
    width:500px;
    padding-right:35px;
    margin-right:0px;
}
#footer {
    font-family:Arial, Helvetica, sans-serif;
    font-size:x-small;
    float:left;
    height:250px;
    width:900px;
    color:#666666;
    clear:both
}

Also the html:

this is the basic html file

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div id="wrapper">Content for  id "wrapper" Goes Here</div>
<div id="header">Content for  id "header" Goes Here</div>
<div id="menu">Content for  id "menu" Goes Here</div>
<div id="main">
  <div id="leftcol">Content for  id "main" Goes Here</div>
  <div id="rightcol">Content for  id "rightcol" Goes Here</div>
  <div id="footer">Content for  id "footer" Goes Here</div>
</div>
</body>
</html>

Solution

  • You need to give body (your element's parent) a width :

    html, body{
        width:100%;
    }
    

    Besides, considering your markup, if you want the whole site to be centered, you should use your #wrapper as an actual wrapper, meaning it should include the other content nodes :

    <body>
    <div id="wrapper">
        <div id="header">Content for  id "header" Goes Here</div>
        <div id="menu">Content for  id "menu" Goes Here</div>
        <div id="main">
            <div id="leftcol">Content for  id "leftcol" Goes Here</div>
            <div id="rightcol">Content for  id "rightcol" Goes Here</div>
            <div id="footer">Content for  id "footer" Goes Here</div>
        </div>
    </div>
    </body>