2
votes

I'm trying to implement a navbar that has a 0.5em margin-top. As of now (you can take a look at this codepen: https://codepen.io/diegomengue/pen/yXboQg), when you scroll the page, the margin goes missing. Is there a way to keep the margin sticky with the navbar?

I'm using bootstrap 4.

Code:

HTML:
<body>
  <nav class='navbar navbar-toggleable-xl mx-auto sticky-top'>
    <ul class='navbar-nav mx-auto'>
      <li class='nav-item'><a href='#'>Sobre mim</a></li>
      <li class='nav-item'><a href='#'>Portfólio</a></li>
      <li class='nav-item'><a href='#'>Contato</a></li>
    </ul>
  </nav>
</body>

CSS:
body {
  height: 1000px;
}
.navbar{
  margin-top: 0.5em;
  background-color: #4B589D;
  width: 87%;
  border-radius: 3px;
  box-shadow: 0 0 10px grey;
}

li {
  padding: 0.3em 0.3em 0.3em 0.3em;
  margin: 0 0.3em 0 0.3em;
}

a {
  color: white;
}

Thank you very much!

1

1 Answers

10
votes

Add

.sticky-top { top: 0.5em; }

to your CSS.

https://codepen.io/anon/pen/GEmMrJ