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

前端 未结 4 1324
攒了一身酷
攒了一身酷 2020-12-09 19:49

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 flo

相关标签:
4条回答
  • 2020-12-09 19:55
     #header {
            top:0;
            width:100%;
            position:fixed;
            background-color:#FFF;
        }
    
        #content {
            position:static;
            margin-top:100px;
        }
    
    0 讨论(0)
  • 2020-12-09 19:57

    to set a div at position fixed you can use

    position:fixed
    top:0;
    left:0;
    width:100%;
    height:50px; /* change me */
    
    0 讨论(0)
  • 2020-12-09 20:01

    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;
        }
    
    0 讨论(0)
  • 2020-12-09 20:15

    The postition:absolute; tag positions the element relative to it's immediate parent. I noticed that even in the examples, there isn't room for scrolling, and when i tried it out, it didn't work. Therefore, to pull off the facebook floating menu, the position:fixed; tag should be used instead. It displaces/keeps the element at the given/specified location, and the rest of the page can scroll smoothly - even with the responsive ones.

    Please see CSS postion attribute documentation when you can :)

    0 讨论(0)
提交回复
热议问题