开发者

Centering a fixed header and sidebar?

开发者 https://www.devze.com 2023-03-31 05:23 出处:网络
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 thou

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 a开发者_StackOverflow中文版nd 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>


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! :)


Not sure if i get you right - you mean something like this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <style type="text/css">
        body {margin:0;padding:0;}
    .wrapper {position:relative;margin:0 auto;width:600px;}
#header {
position: fixed;
top: 0;
left: 0;
width:100%;
height: 150px;
background: #ccc;
z-index: 100;
}
#content {
padding-top: 150px;
width:400px;
background:green;
overflow:auto;
}
#sidebar {
width: 200px;
padding-top: 150px;
float:left;
background:red;
position:fixed;
margin-left:400px;
top:0;
height:100%;
}

    </style>
</head>
<body>
<div id="header">
    <div class="wrapper">
        header
    </div>
</div>
<div class="wrapper">
    <div id="content">
        content start<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content<br>
        content end
    </div>
    <div id="sidebar">
        sidebar
    </div>
</div>
</body>
</html>

Note that position:fixed does not work in ie6.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号