Search code examples
htmlcssheadernavbar

Image above fixed navbar


I want to add an image above my navigation bar, but when I scroll down my page, the image on top should get scrolled and hidden like normal, but nav bar must be fixed and attached replacing the image (should be on top). What CSS code should I apply?

Here is my code:

<img src='image.jpeg' alt='image'>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
    </ul>

Solution

    • You can add a function for window.scroll even.
    • What I did below is, When the scroll is greater than height of image added active class to navbar.

    // height of image
    const imageHeight = 45;
    
    
    window.onscroll = function() {
      scrollFunction()
    };
    // adding active class after scroll of image height and removing it when scroll is less than it. 
    function scrollFunction() {
      if (document.body.scrollTop > imageHeight || document.documentElement.scrollTop > imageHeight) {
        document.getElementById("navbar").classList.add("active");
      } else {
        document.getElementById("navbar").classList.remove("active");
      }
    }
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    body {
      overflow-x: hidden;
    }
    
    .fluid-img {
      width: 100%;
      object-fit: cover;
    }
    
    .heightOfImage {
      height: 45px; // image height
    }
    
    nav {
      padding: 10px;
      width: 100%;
      background: #f5f5f5;
    }
    
    nav.active {
      position: fixed;
      top: 0;
      left: 0;
    }
    
    ul {
      list-style: none;
      display: flex;
      text-transform: capitalize;
      gap: 5px;
    }
    
    p {
      text-align: justify;
    }
    <main>
      <img class="fluid-img heightOfImage" src="https://images.unsplash.com/photo-1671726805766-de50e4327182?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" alt="image from unsplash">
      <nav id="navbar">
        <ul>
          <li>page</li>
          <li>page</li>
          <li>page</li>
          <li>page</li>
        </ul>
      </nav>
      <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
        content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
        versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
      <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
        content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
        versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    </main>

    • Second Approach:
    • Use position:sticky
    nav {
      padding: 10px;
      width: 100%;
      background: #f5f5f5;
      /*This will keep navbar at top after image height scroll*/
      position: sticky;
      top: 0;
    }
    
    • Below using position:sticky, please consider its support to other browser as well. support table

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    body {
      overflow-x: hidden;
    }
    
    .fluid-img {
      width: 100%;
      object-fit: cover;
    }
    
    .heightOfImage {
      height: 45px; // image height
    }
    
    nav {
      padding: 10px;
      width: 100%;
      background: #f5f5f5;
      /*This will keep navbar at top after image height scroll*/
      position: sticky;
      top: 0;
    }
    
    ul {
      list-style: none;
      display: flex;
      text-transform: capitalize;
      gap: 5px;
    }
    
    p {
      text-align: justify;
    }
    <main>
      <img class="fluid-img heightOfImage" src="https://images.unsplash.com/photo-1671726805766-de50e4327182?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" alt="image from unsplash">
      <nav id="navbar">
        <ul>
          <li>page</li>
          <li>page</li>
          <li>page</li>
          <li>page</li>
        </ul>
      </nav>
      <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
        content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
        versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
      <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here,
        content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various
        versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    </main>