Click here to Skip to main content
15,887,289 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create a floating navbar, but it's not moving whenever I scroll! So if you can see what's wrong with it, please help me. I'd appreciate it. Thanks 💕

What I have tried:

/* Navigation bar */
#yui_3_17_2_1_1690045090912_908
{
  width:100vh:
position:fixed; 
  transform-origin: left top: 
left:0; 
  text-align: left;
  z-index:1000;
}
Posted
Updated 22-Jul-23 14:40pm
Comments
Mike Hankey 22-Jul-23 19:20pm    
position: fixed is holding it and not allowing movement.

1 solution

Hard to tell as you do now show any html. So I'll point you to a tutorial...

This video shows you the stick navbar + more: Easily change sticky nav styling on scroll - YouTube[^]. Your answer is in the first 60 seconds:
CSS
{
    position: sticky; /* fixed; */
    top: 0;
}

Look at his html. That is important too:
HTML
<header>
    // navbar goes here...
</header>

<main>
    // page code goes here
</main>

You could have a sidebar, footer, etc, all of which could be inside/outside of main, but donot contain the header.

Watch the video, see what he is doing. I have a feeling that you will want the next part.
 
Share this answer
 
v3
Comments
Andre Oosthuizen 23-Jul-23 5:12am    
"position: fixed;" is not correct unless shown intentionally?
Graeme_Grant 23-Jul-23 5:14am    
Do you read this differently???

"I want to create a floating navbar, but it's not moving whenever I scroll!"
Andre Oosthuizen 23-Jul-23 5:35am    
:) Which is why I questioned the 'fixed' property, should be 'sticky' - <pre data-showheader="false">{
    position: sticky;
    top: 0;
}


Ok, yeah, now I get it... seems it is currently 'sticking' to the top, although the OP used 'fixed', they want it to scroll with the form or am I wrong? See my fiddle - MyFiddle[^]
Graeme_Grant 23-Jul-23 5:42am    
What I posted is correct. Watch the video. Here is more information: position - CSS: Cascading Style Sheets | MDN[^].

Quote:

A stickily positioned element is an element whose computed position value is sticky. It's treated as relatively positioned until its containing block crosses a specified threshold (such as setting top to value other than auto) within its flow root (or the container it scrolls within), at which point it is treated as "stuck" until meeting the opposite edge of its containing block.

Example: position: sticky | MDN[^]

I would use sticky for within scrollable content, not at the top of scrolling content.
Andre Oosthuizen 23-Jul-23 5:57am    
See my new comment:).

In the video, within the first 60 seconds (did not watch entire video), the poster used 'position: sticky' though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900