1

I have an html file being served by Express, which also fetches data from an api. When I click a link in the navbar and switch routes (or the page is reloaded), the top navbar moves right, then left, and I can't figure out how to fix it.

If you look at the JSFiddle (link below), you'll notice that I have links to other pages, like /profile, /about, etc. Each time one of these pages loads, the navbar shifts (it's adjusting for the vertical scroll bar disappearing, then reappearing).

https://jsfiddle.net/h7bjyk63/5/

To mimic the api call, I added a setTimeout. To reproduce the issue and see what I'm talking about, you will probably need to run this code locally on your machine, and then refresh the page.

The strange thing is that this issue only occurs when there's some kind of delay (like an api call, or setTimeout). If I remove the delay and immediately load the content, everything works fine.

Some css code is commented out. The only key element I want to add later is position: fixed to fix the navbar to the top of the page.

How do I prevent the navbar from moving around?

Farid
  • 1,557
  • 2
  • 21
  • 35

2 Answers2

1

The browser first renders the page without the scrollbar because it simply doesn't have to. Then you dynamically add few long paragraphs into the DOM, which makes the scrollbar to appear. This is what's causing your content 'shifting'.

The scrollbar is adding up to the width of your page. To prevent it from doing so, you need to do this:

html{
    overflow-y: scroll;
}
DVN-Anakin
  • 1,549
  • 1
  • 8
  • 12
  • Thanks for the help! The downside to this solution is that the scroll bar will always be there, even when scrolling isn't necessary (a short page). – Farid Jun 17 '20 at 03:14
0

I finally found something that worked, although I'm not 100% sure it's the correct way to do things. I just changed the width under the navbar--site-header class to 100vw instead of 100%.

DVN-Anakin's answer helped me understand the problem (and one possible solution), and this answer provided some additional good solutions.

Farid
  • 1,557
  • 2
  • 21
  • 35