Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a menubar fixed on the top while scrolling

I'd like to make a menubar, which is fixed on the top of the page while scrolling. Something like the top menu in Facebook.

Also, I want a div holding the logo float at the left of menubar, and a nav float at the right of the menubar.

like image 664
Jensen Avatar asked Dec 09 '11 17:12

Jensen


People also ask

How do you make a scrolling header sticky?

The best way to do this is to set a new CSS class for the fixed position that will get assigned to the relevant div when scrolling goes past a certain point. If the trigger point is unknown but should be whenever the sticky element reaches the top of the screen, offset(). top can be used.

How do you stick the menu on top in CSS?

To create a sticky navbar, you use the position: fixed; CSS property to "stick" your navbar to the viewport, and position: sticky; to make it stick to its parent element.


2 Answers

This should get you started

 <div class="menuBar">
        <img class="logo" src="logo.jpg"/>
        <div class="nav"> 
            <ul>
                <li>Menu1</li>
                <li>Menu 2</li>
                <li>Menu 3</li>
            </ul> 
        </div>
    </div>



body{
    margin-top:50px;}
.menuBar{
    width:100%;
    height:50px;
    display:block;
    position:absolute;
    top:0;
    left:0;
    }
.logo{
    float:left;
    }
.nav{
    float:right;
    margin-right:10px;}
.nav ul li{
    list-style:none;
    float:left;
    }
like image 125
Dominic Green Avatar answered Oct 02 '22 00:10

Dominic Green


 #header {
        top:0;
        width:100%;
        position:fixed;
        background-color:#FFF;
    }

    #content {
        position:static;
        margin-top:100px;
    }
like image 45
ceth Avatar answered Oct 02 '22 00:10

ceth