title: how to create a sticky navbar css and javascript
body: <p>I'm trying to make a sticky navbar on scroll with CSS and JavaScript, but it's show as a normal navbar, and it's not sticking when scrolling. How can I make it stick when it is scrolling? i added the header to the code</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>window.onscroll = function() {
    myFunction()
  };
  
  var navbar = document.getElementById("navbar");
  var sticky = navbar.offsetTop;
  
  function myFunction() {
    if (window.pageYOffset &gt;= sticky) {
      navbar.classList.add("sticky")
    } else {
      navbar.classList.remove("sticky");
    }
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#navbar {
    overflow: hidden;
    background-color: #333;
  }
  
  
  /* Navbar links */
  
  #navbar a {
    float: left;
    display: block;
    color: #f2f2f2;
    text-align: center;
    padding: 14px;
    text-decoration: none;
  }
  
  .sticky {
    position: fixed;
    top: 0;
    width: 100%;
  }
  
  .sticky+.content {
    padding-top: 60px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="header"&gt;
   &lt;h2&gt;S I U&lt;/h2&gt;
   &lt;p&gt;T h e  f u t u r e  &amp;  B e y o n d&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;div id="navbar"&gt;
    &lt;a href="#home"&gt;Home&lt;/a&gt;
    &lt;a href="#news"&gt;News&lt;/a&gt;
    &lt;a href="#contact"&gt;Contact&lt;/a&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>