Make a nav bar stick

前端 未结 11 1310
别跟我提以往
别跟我提以往 2020-11-29 19:44

Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make a nav bar stick Make

相关标签:
11条回答
  • 2020-11-29 19:58

    Just use z-index CSS property as described in the highest liked answer and the nav bar will stick to the top.

    Example:

    <div class="navigation">
     <nav>
       <ul>
        <li>Home</li>
        <li>Contact</li>
       </ul>
     </nav>
    

    .navigation {
       /* fixed keyword is fine too */
       position: sticky;
       top: 0;
       z-index: 100;
       /* z-index works pretty much like a layer:
       the higher the z-index value, the greater
       it will allow the navigation tag to stay on top
       of other tags */
    }
    
    0 讨论(0)
  • 2020-11-29 20:02

    You can do it with CSS only by creating your menu twice. It's not ideal but it gives you the opportunity have a different design for the menu once it's on top and you'll have nothing else than CSS, no jquery. Here is an example with DIV (you can of course change it to NAV if you prefer):

    <div id="hiddenmenu">
     THIS IS MY HIDDEN MENU
    </div>
    <div id="header">
     Here is my header with a lot of text and my main menu
    </div>
    <div id="body">
     MY BODY
    </div>
    

    And then have the following CSS:

    #hiddenmenu {
    position: fixed;
    top: 0;
    z-index:1;
     }
    #header {
    top: 0;
    position:absolute;
    z-index:2;
     }
    #body {
    padding-top: 80px;
    position:absolute;
    z-index: auto;
     }
    

    Here is a fiddle for you to see: https://jsfiddle.net/brghtk4z/1/

    0 讨论(0)
  • 2020-11-29 20:03

    Just Call this code and call it to your nave bar for sticky navbar

      .sticky {
            /*css for  stickey navbar*/
            position: sticky;
            top: 0; 
            z-index: 100;
        }
    
    0 讨论(0)
  • 2020-11-29 20:06

    CSS:

    .headercss {
        width: 100%;
        height: 320px;
        background-color: #000000;
        position: fixed;
    }
    

    Attribute position: fixed will keep it stuck, while other content will be scrollable. Don't forget to set width:100% to make it fill fully to the right.

    Example

    0 讨论(0)
  • 2020-11-29 20:08

    To make header sticky, first you have to give position: fixed; for header in css. Then you can adjust width and height etc. I would highly recommand to follow this article. How to create a sticky website header

    Here is code as well to work around on header to make it sticky.

    header { 
       position: fixed; 
       right: 0; 
       left: 0; 
       z-index: 999;
    }
    

    This code above will go inside your styles.css file.

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