How to position: fixed the navbar when using flexbox

Display:flex and position:fixed does not go well together.

you can make little changes and make it look like this

Check the following snippet

/* Clear formatting*/

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
/* Remove formatting from links*/

a {
  color: inherit;
  text-decoration: none;
}
/* Set a max width to the content*/

.wrapper {
  max-width: 960px;
  margin: 0 auto;
  background-color: pink;
  display: flex;
  flex-direction: column;
  /*height: 100vh;  */
}
.parent {
  position: fixed;
  top: 0;
  left: 0;
  margin: auto;
  width: 100%;
}
.navbar {
  display: flex;
  justify-content: space-between;
  background-color: yellow;
  /*   */
}
.logo {
  padding: 20px;
}
.menu {
  display: flex;
  align-items: center;
}
.menu ul {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  list-style: none;
  background-color: grey;
}
.menu li {
  padding: 20px;
  margin-left: 20px;
  background-color: orange;
  color: rgba(255, 0, 0, 0.9);
  font-size: 16px;
  font-weight: bold;
}
<div class="parent">
  <div class="navbar">
    <div class="logo">
      <picture>
        <img src="http://fillmurray.com/50/50" alt="logo">
      </picture>
    </div>
    <div class="menu">
      <ul>
        <li><a href="#home">Home</a>
        </li>
        <li><a href="#about">About</a>
        </li>
        <li><a href="#contact">Contact</a>
        </li>
      </ul>
    </div>
  </div>
</div>

<div class="wrapper">
  <div style="height: 2000px"></div>
</div>

Hope it helps


Add the following css property in navbar class

    .navbar {
  display: flex;
  justify-content: space-between;
  background-color: yellow;
  position: sticky;
  top: 0;
  z-index: 100;
  }

Tags:

Css

Flexbox