A sticky navbar stays visible at the top of the viewport as the user scrolls down. Use position: sticky combined with top: 0.
Steps
- Set
position: stickyon the navbar element. - Set
top: 0to anchor it to the top of the viewport. - Add a
z-indexso it appears above other content. - Give it a background color so content doesn't show through.
Tips
- The parent element must not have
overflow: hidden, or sticky won't work. - Use
box-shadowto add depth when scrolling.