# Navbar change position

**URL:** <https://forum.freecodecamp.org/t/navbar-change-position/225420>\
**Category:** General\
**Created:** [September 10, 2018, 12:22pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420 "2018-09-10T12:22:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esetessy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/esetessy/32/34689_2.png) [@Esetessy](https://forum.freecodecamp.org/u/Esetessy)\
**Post date:** [September 10, 2018, 12:22pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/1 "2018-09-10T12:22:43Z")

</div>

Hello everyone, Please I need assistance on my page. I want the navbar to change its position to the top when I scroll down and it should be fixed.

> **[page](https://codepen.io/Esther-Itolima/full/xaYbjz/)**
>
> ...

---

<div class="post-metadata">

**Author:** ![pedrobslisboa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pedrobslisboa/32/80894_2.png) [@pedrobslisboa](https://forum.freecodecamp.org/u/pedrobslisboa)\
**Post date:** [September 10, 2018, 1:16pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/2 "2018-09-10T13:16:00Z")

</div>

Hey ya,

The problem is the z-index of the element.

```auto
.sticky{
  position: fixed; 
  top:0;
  width: 100%; 
  z-index:1;
}

```

---

<div class="post-metadata">

**Author:** ![Esetessy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/esetessy/32/34689_2.png) [@Esetessy](https://forum.freecodecamp.org/u/Esetessy)\
**Post date:** [September 10, 2018, 1:35pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/3 "2018-09-10T13:35:04Z")

</div>

using your code it will automatically move to the top without scrolling but that is not exactly what I want.  
I want a situation where it will retain the previous position which is under the image then when I scroll pass the image it will automatically move to the top.

---

<div class="post-metadata">

**Author:** ![pedrobslisboa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pedrobslisboa/32/80894_2.png) [@pedrobslisboa](https://forum.freecodecamp.org/u/pedrobslisboa)\
**Post date:** [September 10, 2018, 1:35pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/4 "2018-09-10T13:35:12Z")

</div>

I’ve made some changes that make a soft transition.

Here I’ve moved the nav to inside the hero-image, then I can have a hierarchy and I’ve given the class not-fixed to the navbar:

```auto
<div class="hero-image">
  <!-- <img src="https://images2.imgbox.com/24/1c/FsJ4AwKi_o.jpg" alt=""> -->
  <div class="portfolio">
    <h2 class="portfolio-title"><span class="portfolio-title--small">Hi there, I'm</span> Esther Itolima
      <hr>
    </h2>
    <p class="portfolio-intro">A frontend web developer with great passion for tech and fascinated <br> to solve real world problem with software.</p>
  </div>
  <nav id="navbar" class="not-fixed">
            <ul>
              <li><a href="#follow-me">Follow me</a></li>
              <li><a href="#contact-me">Contact me</a></li>
              <li><a href="#concepts">Concepts</a></li>
              <li><a href="#about-me">About Me</a></li>
            </ul>
          </nav> 
</div>

```

Then I’ve add those attrs to the not-fixed, this will make your nav to stay at the bottom!  
And I’ve also put position: relative, with this the nav bar will only be absolute at hero-image. The last change is that I’ve set #navbar to be always 100% width.

```auto
.hero-image{
  ...
  position: relative;
}

#navbar{
  width: 100%; 
  ...
}

.not-fixed{
  position:absolute;
  bottom:0;
}

.sticky{
  position: fixed; 
  width: 100%; 
  top:0;
  z-index:1;
}

```

And the last one, I’ve changed sticky to be based on about-section, then I’ve removed the #navbar heigth so when it reachs the top of the about-section minus 75 px (Same as the top of the navbar) it will be fixed.

```auto
var about = document.getElementById("about-section");
var sticky = about.offsetTop - 75;

function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky");
    navbar.classList.remove("not-fixed")
  } else {
    navbar.classList.add("not-fixed")
    navbar.classList.remove("sticky");
  }
}

```

I’ve forked your code, so you can see all the changes here: [https://codepen.io/pedrobslisboa/pen/LJQVBE](https://codepen.io/pedrobslisboa/pen/LJQVBE)

See ya, keep coding

---

<div class="post-metadata">

**Author:** ![pedrobslisboa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pedrobslisboa/32/80894_2.png) [@pedrobslisboa](https://forum.freecodecamp.org/u/pedrobslisboa)\
**Post date:** [September 10, 2018, 2:04pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/5 "2018-09-10T14:04:13Z")

</div>

Hey Esther, did it work?

Give us feedback, if you have any doubt just ask.  
If you understood, please mark the post as the solution to close the topic 🙂

---

<div class="post-metadata">

**Author:** ![Esetessy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/esetessy/32/34689_2.png) [@Esetessy](https://forum.freecodecamp.org/u/Esetessy)\
**Post date:** [September 10, 2018, 2:26pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/6 "2018-09-10T14:26:48Z")

</div>

Yessss! it worked…thanks so much!

---

<div class="post-metadata">

**Author:** ![pedrobslisboa](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pedrobslisboa/32/80894_2.png) [@pedrobslisboa](https://forum.freecodecamp.org/u/pedrobslisboa)\
**Post date:** [September 10, 2018, 2:33pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/7 "2018-09-10T14:33:19Z")

</div>

You’re welcome 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:58pm UTC](https://forum.freecodecamp.org/t/navbar-change-position/225420/8 "2021-01-16T17:58:31Z")

</div>

Closed on January 16, 2021, 5:58pm UTC


