Home > Software design >  Fixed navbar without breaking flexbox
Fixed navbar without breaking flexbox

Time:05-29

Adding position: fixed; to the navbar breaks margin-left: auto; for the navbar menu icon:

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.navbar {
  position: fixed;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div >

  </div>
  <div >
    <nav >
      <img src="/img/Mitcho Icon black yellow.png" alt="logo" > <span >Mitcho</span>
      <div ></div>
    </nav>
    <main >
      <div >
        <h1 >Hello!</h1>
        <span >
                    I'm Mitchell Orutu,
                </span>
        <div >
          <span >I Love</span>
          <div ></div>
          <span >Creating</span>
          <div ></div>
          <span >Amazing Experiences</span>
        </div>
        <p >A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" >
          <span >Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>

CodePudding user response:

1 preferred solution

You need only add right:0 and width: 100%; to your .navbar class.

.navbar {  
  position: fixed;  
  right:0;
  width:100%;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;  
}

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.navbar {  
  position: fixed;  
  right:0;
  width:100%;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;  
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div >

  </div>
  <div >
    
      <nav >
        <img src="/img/Mitcho Icon black yellow.png" alt="logo" > <span >Mitcho</span>
        <div ></div>
      </nav>
    
    <main >
      <div >
        <h1 >Hello!</h1>
        <span >
                    I'm Mitchell Orutu,
                </span>
        <div >
          <span >I Love</span>
          <div ></div>
          <span >Creating</span>
          <div ></div>
          <span >Amazing Experiences</span>
        </div>
        <p >A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" >
          <span >Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>
      

2 solution

I wrap the nav tag with a div and assign this div a width and padding. Then i remove the position:fixed from the nav element.

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.fixed {
  position: fixed;  
  width:100%;
  padding:0px 30px 0px 0px;
}

.navbar {  
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;  
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div >

  </div>
  <div >
    <div >
      <nav >
        <img src="/img/Mitcho Icon black yellow.png" alt="logo" > <span >Mitcho</span>
        <div ></div>
      </nav>
    </div>
    <main >
      <div >
        <h1 >Hello!</h1>
        <span >
                    I'm Mitchell Orutu,
                </span>
        <div >
          <span >I Love</span>
          <div ></div>
          <span >Creating</span>
          <div ></div>
          <span >Amazing Experiences</span>
        </div>
        <p >A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" >
          <span >Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>
      

CodePudding user response:

Can fix it by slapping on a width: 100%:

/* Resets */

 :root {
  --pblack: #1E293B;
  --pyellow: #FFE934;
  --pwhite: white;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--pblack);
  color: var(--pwhite);
  font-family: raleway;
}

a {
  color: white;
  text-decoration: none;
}


/* Resets end */


/* Styling starts */

.logo {
  width: 32px;
  margin-right: auto;
}

.logo-type {
  display: none;
}

.container {
  margin: 1rem;
  display: flex;
  flex-direction: column;
  position: relative;
  height: 90vh;
}


/* .nav-holder { position: fixed;
} */

.navbar {
  position: fixed;
  width: 100%;
  background: transparent;
  display: flex;
  align-items: center;
  z-index: 999;
}

.nav-menu {
  position: relative;
  background: var(--pyellow);
  width: 1.5rem;
  height: 2px;
  margin-left: auto;
}

.nav-menu::before {
  content: " ";
  position: absolute;
  top: -7px;
  right: 0;
  background: var(--pyellow);
  width: .5rem;
  height: 2px;
}

.nav-menu::after {
  content: " ";
  position: absolute;
  top: 7px;
  right: 0;
  background: var(--pyellow);
  width: 1rem;
  height: 2px;
}

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* border: 2px solid red; */
  text-align: center;
}

.greeting {
  font-size: 5rem;
  margin-bottom: 0;
}

.hero-name {
  font-size: 1.5rem;
}

.hero-anim {
  position: relative;
  margin: 6rem;
  /* border: 2px solid red; */
  width: 60%;
  /* height: 100px; */
}

.h-love {
  position: absolute;
  top: 0;
  left: 0;
}

.x-anim {
  position: absolute;
  top: 0;
  right: 0;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(45deg);
}

.x-anim::after {
  content: "";
  display: inline-block;
  background: var(--pyellow);
  width: 2px;
  height: 1rem;
  transform: rotate(90deg);
}

.h-creating {
  position: absolute;
  top: 20px;
  left: 20px;
  font-size: 2rem;
  margin: 0;
  color: var(--pyellow);
}

.h-amazing {
  position: absolute;
  top: 65px;
  left: 45px;
}

.dash-anim {
  position: absolute;
  top: 75px;
  left: 0px;
  width: 1.5rem;
  height: 2px;
  background: var(--pyellow);
}

.h-details {
  margin-left: 3rem;
  margin-right: 3rem;
  margin-top: 10rem;
}

.know-more-btn {
  position: relative;
  border: 1px solid var(--pyellow);
  border-radius: 50px;
  padding: .5rem 2.5rem .5rem 1rem;
  width: max-content;
  display: flex;
  align-items: center;
}

.btn-arrow {
  position: absolute;
  right: 2px;
  font-size: 2rem;
}

.sect2 {
  background-color: #FFE934;
  width: 100%;
  height: 100vh;
}
<body>
  <div >

  </div>
  <div >
    <nav >
      <img src="/img/Mitcho Icon black yellow.png" alt="logo" > <span >Mitcho</span>
      <div ></div>
    </nav>
    <main >
      <div >
        <h1 >Hello!</h1>
        <span >
                    I'm Mitchell Orutu,
                </span>
        <div >
          <span >I Love</span>
          <div ></div>
          <span >Creating</span>
          <div ></div>
          <span >Amazing Experiences</span>
        </div>
        <p >A multidisciplinary designer and frontend developer. I'm obsessed with creating amazing experiences and have fun while doing it.</p>
        <a href="#" >
          <span >Know More</span>
          <i class='bx bxs-chevron-right-circle btn-arrow' style='color:#ffffff'></i>
        </a>
      </div>

  • Related