Search code examples
csswidthpositioncenterfixed

Centering a fixed header and sidebar?


I am trying to create a layout using a fixed header and sidebar but I wan't to make the layout in the center of the browser.. Obvisously this is a problem as fixed usually doesn't allow this. I thought about putting a wrapper around everything and using margin: 0px auto; etc. but that didn't work, is it possible or not?

   #header {
position: fixed;
top: 0;
left: 0;
width: 100%; 
height: 150px;
background-color: #FFF;
z-index: 100;
}


#content {
width: 1112px;
overflow: auto;
padding-top: 150px;

}


#sidebar {
position: fixed;
top: 0;
left: 0;
width: 275px;
height: 100%;
z-index: 100;

}

Had in mind something like this but it didn't work:

body {
    margin:0px 0px; padding:0px;
    text-align:center;  
}


#wrapper {
text-align: left;
margin: 0px auto;
}

divs:

<div id="wrapper">
<div id="header"></div>
<div id="content"></div>
<div id="sidebar"></div>
</div>

Solution

  • Try this:

    CSS:

    body {
    margin:0; 
    padding:0; 
    }
    
    
    #wrapper {
    margin: 0 auto 0 auto;
    width:1112px;
    min-height:600px;
    height:auto;
    background-color:#009900;
    }
    
    #header {
    position: fixed;
    width: 1112px; 
    height: 150px;
    background-color: #ccc;
    z-index: 100;
    }
    
    
    #content {
    position:absolute;
    top:150px;
    margin-left:275px;
    overflow: auto;
    width:837px;  /* 1112px - 275px = 837px */
    background-color:#CC9900; 
    }
    
    
    #sidebar {
    position: fixed;
    top:150px;
    width: 275px;
    height: 100%;
    min-height:200px; /* just do simulate content */
    z-index: 100;
    background-color:#96F;
    }
    

    HTML:

    <div id="wrapper">
    <div id="header">Fixed header</div>
    <div id="content">
    <p>Dummy Text</p><br /><br /><br />
    <p>Dummy Text</p><br /><br /><br />
    <p>Dummy Text</p><br /><br /><br />
    <p>Dummy Text</p><br /><br /><br />
    <p>Dummy Text</p><br /><br /><br />
    <p>Dummy Text</p><br /><br /><br />
    <p>Dummy Text</p><br /><br /><br />
    <p>Dummy Text</p><br /><br /><br />
    </div>
    <div id="sidebar">Fixed sidebar</div>
    </div>
    

    If I understand your question, I think this is a possible way to do it. Try it, and tell me so! :)