SnookerFan SnookerFan - 1 year ago 88
CSS Question

CSS - Make Content Appear Beneath a Fixed DIV Element

My intention is to create a menu at the top of the page which remains at the top of the page even when the user scrolls (like Gmail's recent feature which has the commonly-used buttons scrolling down with the user so that it allows them to perform operations on messages without having to scroll to the top of the page).

I would also like to set the content below said menu to appear below it - at present, it appears behind it.

I am aiming for something like this:

| MENU | <--- Fixed menu - stays at top even when scrolling.
| HERE |
| |
| |
| |
| |
| |
| |
+¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬+ <--- Bottom of page.

I hope to have the menu at the top which never moves and which stays at the top of the page, even when the user scrolls down. I am also looking to have the main content begin beneath the menu when the user is at the top of the page, but when the user scrolls down, then it doesn't matter if the menu goes over the top of the content.


  • I wish to have a fixed position menu at the top of the page which follows the user when scrolling.

  • Content must appear BELOW the menu ONLY when the user is at the top of the page.

    • When the user scrolls down, the menu may overlap the content.

Can somebody please explain how to achieve this?

Thank you.


CSS Code:

clear: both;
position: fixed;
width: 85%;
background-color: #78AB46;
top: 5px;

HTML Code:

<div id="floatingMenu">
<a href="">Test 1</a>
<a href="">Test 2</a>
<a href="">Test 3</a>

At present, I can get the menu to appear at the top of the page and centered by placing it inside my
. However, the content goes behind the menu. I have set
clear: both;
and this has not helped.

Answer Source

What you need is an extra spacing div (as far as I understood your question).

This div will be placed between the menu and content and be the same height as the menu div, paddings included.


<div id="fixed-menu">
    Navigation options or whatever.
<div class="spacer">
<div id="content">


    position: fixed;
    width: 100%;
    height: 75px;
    background-color: #f00;
    padding: 10px;

    width: 100%;
    height: 95px;

See my example here.

This works by offsetting the space that would have been occupied by the nav div, but as it has position: fixed; it has been taken out of the document flow.

The preferred method of achieving this effect is by using margin-top: 95px;/*your nav height*/ on your content wrapper.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download