title: Bootstrap4 sticky-top (sticky navbar) not working properly
body: <p>I have made a navbar in my html page and added the Bootstrap 4 class <code>sticky-top</code> to it. </p>
  
  <p>The problem is that, it sticks to the top till half of the screen is scrolled, but then the sticky property doesn't work and the navbar goes upward as the page is scrolled.</p>
  
  <p>Why is it happening that it works for limited margin and then suddenly stops working?</p>
  
  <p>I tried to use <code>navbar-fixed-top</code> class, 
  also I tried <code>position:sticky; top:0; width:100%</code>, but in vain.</p>
  
  <p><strong>My Html for</strong> <code>navbar</code>:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;nav class="navbar navbar-expand-lg navigation navbar-fixed-top" style="position:sticky; top: 0; width:100%;" id="bootnavbar"&gt;
        &lt;div class="container"&gt;
              &lt;a class="navbar-brand" href="index.php"&gt;&lt;i class="fas fa-home"&gt;&lt;/i&gt;&lt;/a&gt;
              &lt;button class="navbar-toggler  ml-auto custom-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" style="font-size:0.85em;"&gt;
                    &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
              &lt;/button&gt;
  
              &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt;
                    &lt;ul class="navbar-nav mr-auto"&gt;
  
                          &lt;li class="nav-item dropdown"&gt;
                                &lt;a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
                                      About Us
                                &lt;/a&gt;
                                &lt;ul class="dropdown-menu" aria-labelledby="navbarDropdown"&gt;
                                      &lt;li&gt;&lt;a class="dropdown-item" href="about.php"&gt;About&lt;/a&gt;&lt;/li&gt;
                                      &lt;div class="dropdown-divider"&gt;&lt;/div&gt;
                                      &lt;li&gt;&lt;a class="dropdown-item" href="boac.php"&gt;Board of Advisory Comittee&lt;/a&gt;&lt;/li&gt;
                                      &lt;li&gt;&lt;a class="dropdown-item" href="bos.php"&gt;Board of Studies&lt;/a&gt;&lt;/li&gt;
                                      &lt;div class="dropdown-divider"&gt;&lt;/div&gt;
                                      &lt;li&gt;&lt;a class="dropdown-item" href="admission.php"&gt;Admission&lt;/a&gt;&lt;/li&gt;
                                      &lt;li class="nav-item dropdown"&gt;
                                            &lt;a class="dropdown-item dropdown-toggle" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
                                                  Academic
                                            &lt;/a&gt;
                                            &lt;ul class="dropdown-menu" aria-labelledby="navbarDropdown1"&gt;
                                                  &lt;li&gt;&lt;a class="dropdown-item disabled" href="faculty.php"&gt;Faculty&lt;/a&gt;&lt;/li&gt;
                                                  &lt;li&gt;&lt;a class="dropdown-item" href="activities.php"&gt;Activities&lt;/a&gt;&lt;/li&gt;
                                            &lt;/ul&gt;
                                      &lt;/li&gt;
                                      &lt;li&gt;&lt;a class="dropdown-item" href="facilities.php"&gt;Facilities&lt;/a&gt;&lt;/li&gt;
                                &lt;/ul&gt;
                          &lt;/li&gt;
  
                          &lt;li class="nav-item dropdown"&gt;
                                &lt;a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
                                      Courses
                                &lt;/a&gt;
                                &lt;ul class="dropdown-menu" aria-labelledby="navbarDropdown"&gt;
                                      &lt;li&gt;&lt;a class="dropdown-item" href="regular_courses.php"&gt;Regular Courses&lt;/a&gt;&lt;/li&gt;
                                      &lt;li&gt;&lt;a class="dropdown-item" href="short_term_courses.php"&gt;Short Term Courses&lt;/a&gt;&lt;/li&gt;
                                &lt;/ul&gt;
                          &lt;/li&gt;
  
                          &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link" href="gallery.php" role="button" aria-haspopup="true" aria-expanded="false"&gt;
                                      Gallery
                                &lt;/a&gt;
                          &lt;/li&gt;
  
                          &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link" href="placements.php" role="button" aria-haspopup="true" aria-expanded="false"&gt;
                                      Training &amp; Placements
                                &lt;/a&gt;
                          &lt;/li&gt;
                          &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link" href="alumni.php" role="button" aria-haspopup="true" aria-expanded="false"&gt;
                                      Alumni
                                &lt;/a&gt;
                          &lt;/li&gt;
  
                          &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link" href="feedback.php" role="button" aria-haspopup="true" aria-expanded="false"&gt;
                                      Feedback
                                &lt;/a&gt;
                          &lt;/li&gt;
  
                    &lt;/ul&gt;
              &lt;/div&gt;
        &lt;/div&gt;
  &lt;/nav&gt;
  
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"&gt;&lt;/script&gt;
  &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;script src="js/bootnavbar.js"&gt;&lt;/script&gt;
  &lt;script&gt;
        $(function() {
              $('#bootnavbar').bootnavbar();
        })
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>My CSS for</strong> <code>navbar</code>:</p>
  
  <pre class="lang-css prettyprint-override"><code>.navigation {
      background: #020031;
      padding: 0px 23px;
      font-size: 17px;
      z-index: 500;
      box-shadow: 0px 10px 15px -9px;
  }
  
  .navbar-brand {
      color: #fff;
  }
  
  .navbar-brand:hover {
      color: #fff;
  }
  
  .navigation .nav-item .nav-link {
      color: #fff;
      margin-right: 10px;
      padding: 16px;
      text-transform: capitalize;
      font-weight: 500;
  }
  
  .navigation li a:hover {
      background: #f6783a;
  }
  
  .custom-toggler.navbar-toggler {
      border-color: rgb(255, 255, 255);
  }
  
  .custom-toggler .navbar-toggler-icon {
      background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgb(255, 255, 255)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
  }
  
  .nav-switch {
      display: none;
  }
  
  .nav-section {
      background: #020031;
  }
  
  .nav-section .nav-right {
      float: right;
      padding-top: 23px;
  }
  
  .nav-section .nav-right a {
      color: #fff;
      margin-left: 30px;
      font-size: 16px;
  }
  
  .main-menu {
      list-style: none;
  }
  
  .main-menu li {
      display: inline;
  }
  
  .main-menu li a {
      display: inline-block;
      color: #fff;
      font-size: 13px;
      text-transform: uppercase;
      font-weight: 500;
      padding: 25px 20px;
      margin-right: -5px;
      -webkit-transition: all 0.4s ease 0s;
      -o-transition: all 0.4s ease 0s;
      transition: all 0.4s ease 0s;
  }
  
  .main-menu li a:hover {
      background: #f6783a;
  }
  
  .main-menu li.active a {
      background: #f6783a;
  }
  
  </code></pre>
  
  <p><strong>bootnavbar.css</strong></p>
  
  <pre class="lang-css prettyprint-override"><code>.dropdown-menu {
      margin-top: 0;
  }
  .dropdown-menu .dropdown-toggle::after {
      vertical-align: middle;
      border-left: 4px solid;
      border-bottom: 4px solid transparent;
      border-top: 4px solid transparent;
  }
  .dropdown-menu .dropdown .dropdown-menu {
      left: 100%;
      top: 0%;
      margin:0 20px;
      border-width: 0;
  }
  
  .dropdown-menu &gt; li a:hover,
  .dropdown-menu &gt; li.show {
      background: #007bff;
      color: white;
  }
  .dropdown-menu &gt; li.show &gt; a{
      color: white;
  }
  
  @media (min-width: 768px) {
      .dropdown-menu .dropdown .dropdown-menu {
          margin:0;
          border-width: 1px;
      }
  }
  </code></pre>
  
  <p><strong>bootnavbar.js</strong></p>
  
  <pre><code>(function($) {
      var defaults={
          sm : 540,
          md : 720,
          lg : 960,
          xl : 1140,
          navbar_expand: 'lg'
      };
      $.fn.bootnavbar = function() {
  
          var screen_width = $(document).width();
  
          if(screen_width &gt;= defaults.lg){
              $(this).find('.dropdown').hover(function() {
                  $(this).addClass('show');
                  $(this).find('.dropdown-menu').first().addClass('show').addClass('animated fadeIn').one('animationend oAnimationEnd mozAnimationEnd webkitAnimationEnd', function () {
                      $(this).removeClass('animated fadeIn');
                  });
              }, function() {
                  $(this).removeClass('show');
                  $(this).find('.dropdown-menu').first().removeClass('show');
              });
          }
  
          $('.dropdown-menu a.dropdown-toggle').on('click', function(e) {
            if (!$(this).next().hasClass('show')) {
              $(this).parents('.dropdown-menu').first().find('.show').removeClass("show");
            }
            var $subMenu = $(this).next(".dropdown-menu");
            $subMenu.toggleClass('show');
  
            $(this).parents('li.nav-item.dropdown.show').on('hidden.bs.dropdown', function(e) {
              $('.dropdown-submenu .show').removeClass("show");
            });
  
            return false;
          });
      };
  })(jQuery);
  </code></pre>