Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position: fixed the navbar when using flexbox

Tags:

css

flexbox

Starting with this layout: http://codepen.io/RebelOne/pen/wodbpR

.navbar {
  display: flex;
  justify-content: space-between;

When I add position: fixed to the navbar, it collapses down and doesn't fill the width of the wrapper. Does anyone know how to fix this? http://codepen.io/RebelOne/pen/bBWyLZ?editors=1100

.navbar {
  display: flex;
  justify-content: space-between;
  background-color: yellow;
  position: fixed;
  top: 0;
  z-index: 99;
  }
like image 843
RebelOne Avatar asked Nov 23 '16 19:11

RebelOne


2 Answers

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

like image 80
Geeky Avatar answered Oct 05 '22 07:10

Geeky


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;
}
like image 45
Ankur Kunal Avatar answered Oct 05 '22 07:10

Ankur Kunal