Reputation: 3313
How to make nav
and right
block fixed, also keep the whole container centre
center?
without using js
http://jsfiddle.net/zujg22st/9/
I know position fixed
is fixed to browser not to parent, so it can't be simply using fixed in relative div like position absolute
<div class="center">
<div class="nav">nav</div>
<div class="left">
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<!--..... a lot ->
</div>
<div class="right">right</div>
</div>
css
.center {
width: 400px;
background-color: red;
margin: 0 auto;
}
.nav {
width: 400px;
height: 30px;
background-color: yellow;
}
.left, .right {
width: 150px;
display: inline-block;
vertical-align: top;
}
.left {
background-color: green;
}
.right {
background-color: blue;
height: 40px;
}
Upvotes: 0
Views: 171
Reputation: 318
So what we're going to want to do here is separate the this into two parts. We'll create a header at the top of the page and a body with a fixed right div. The nav bar at the top we can make a fixed header like so:
CSS:
#header{
position:relative;
height:30px;
width:400px;
margin: 0 auto;
}
HTML:
<div id="header">
<div class="nav">nav</div>
</div>
The body will look like:
HTML:
<div class="center">
<div class="left">
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
<div class="left-el">left-el</div>
</div>
<div class="right">right</div>
</div>
CSS:
#header{
position:relative;
height:30px;
width:400px;
margin: 0 auto;
}
.centerheader{
width: 400px;
margin: 0 auto;
position:relative;
}
.center {
width: 400px;
background-color: red;
margin: 0 auto;
}
.nav {
position:fixed;
width: 400px;
height: 30px;
background-color: yellow;
}
.left, .right {
width: 150px;
display: inline-block;
vertical-align: top;
}
.left {
background-color: green;
}
.right {
position:fixed;
background-color: blue;
height: 40px;
}
.right {
margin-left: 3px;
position: fixed;
}
body{
margin: 0 auto;
}
As a side note I modified the underlying body {margin: 0 auto;}
. In jsfiddle there's a margin around the body and it causes ugliness when you have a header.
Upvotes: 2
Reputation: 8531
A child div cannot be fixed
relative to it's parent. But here's what I've got so far. I think it will help you.
Set position: fixed;
to the .nav
. Add same amount of margin-top
as .nav
s height to both .left
and .right
. In this case it's 30px
. Then set position: absolute;
the .right
div.
Upvotes: -1