Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position: fixed with vertical space

Tags:

css

What I have is a rather basic issue with position: fixed.

Here's a sample:

* {
    margin: 0;
    padding: 0;
}
.nav {
    width: 100%;
    height: 50px;
    background: #000;
    position: fixed;
}

.content {
    background: #ccc;
    width: 100%;
    height: 5000px;
}
<div class="nav"></div>
<div class="content">test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br /></div>

What I want is the scrolling to start below the black bar (with a fixed position).

like image 274
ienes Avatar asked Mar 19 '11 16:03

ienes


4 Answers

Add padding to second div equal to height of second div.

.content {
    padding-top: 50px;
    background: #ccc;
    width: 100%;
    height: 5000px;
}

When you say scrolling below the back bar, it sounds like you want the content to begin below the back bar. So add some padding to second div to account for presence of fixed div.

like image 140
mrtsherman Avatar answered Nov 15 '22 23:11

mrtsherman


Here's a more flexible way of accomplishing this that doesn't require the height of the fixed div to be known (though it is less semantic).

Simply duplicate the markup for the fixed element. Set the position of the first of the duplicated pair to fixed and the visibility of the second to hidden (also make sure the position of the second element is unset or relative). Here's an example:

* {
    margin: 0;
    padding: 0;
}
.nav {
    width: 100%;
    height: 50px;
    background: #000;
}
.fixed{position:fixed}
.filler{visibility:hidden}

.content {
    background: #ccc;
    width: 100%;
}
<div class="nav fixed"></div>
<div class="nav filler"></div>
<div class="content">
  
  First<br />
  Second<br />
  Third<br />
  Fourth<br />
  Fifth<br />
  Sixth<br />
  Seventh<br />
  Eigth<br />
  Ninth<br />
  
  <br /><br /><br /><br />
  <br /><br /><br /><br />
  <br /><br /><br /><br />

  Last<br />

</div>
like image 40
Trevor Avatar answered Nov 15 '22 22:11

Trevor


Does this do it?

http://jsfiddle.net/Vqncx/

I just gave the 'content' DIV relative positioning and a y-axis from the top equal to the height of the 'nav' and then gave the 'nav' a z-index to keep it on top of the 'content'.

.nav {
 width: 100%;
 height: 50px;
 background: #000;
 position: fixed;
 z-index: 5;
}

.content {
 background: #ccc;
 width: 100%;
 height: 5000px;
 position: relative;
 top:50px;
}
like image 35
Yammi Avatar answered Nov 15 '22 22:11

Yammi


You just need to add a top margin to your .content div equal to the size of the .nav block + some padding:

.content {
    margin-top: 60px;
    background: #ccc;
    width: 100%;
    height: 5000px;
}
like image 22
kdtong Avatar answered Nov 15 '22 21:11

kdtong