Position Sticky stops working, after I set a sibling element to Position Absolute

<header>
<div class="logo"></div>
<div class="header__links"></div>
<nav class="nav"></nav>
<header>
<header>
<div class="logo"></div>
<div class="header__links"></div>
<nav class="nav"></nav>
<header>
this is the structure. header is position relative. .header__links is set to position: sticky because i want it to stick on top of my page. Things are working fine so far. But when I set nav to position: absolute, the sticky element stops working like it should.
6 Replies
Jochem
Jochem10mo ago
The sticky sticks to the nearest parent with position relative or absolute, so it's working as described in the spec
feanor
feanorOP10mo ago
I'm not changing the parent. Its relative all the time. Its only when I make the sibling nav absolute when it stops working.
No description
feanor
feanorOP10mo ago
disabled absolute and sticky is working again.
No description
Kevin Powell
Kevin Powell10mo ago
Any chance you could share the working version with us? My thinking is, the position absolute is pulling that element out of the flow so the links move up. If you have white background on them, it's just hiding that behind the links. I made a really basic example here, and it's still working for me, but you can see how the list goes on top of the thing with position absolute on it https://codepen.io/kevinpowell/pen/rNbOwxE?editors=1100
feanor
feanorOP10mo ago
I've copied the code in this codepen, the problem is on the mobile view, when menu is opened. https://codepen.io/rafaaay/pen/xxewrLZ
Kevin Powell
Kevin Powell10mo ago
Ah, I get it now. The problem is, the position: sticky is on your .header__links... which is going to "stick" to the header. Even now on the larger viewport, the links will scroll away once you scroll enough. Same with if you turn position: absolute off. It will stick for a little, but it then scrolls away. Position sticky doesn't stick to the viewport, it sticks to it's parent element. When you position: absolute the nav, the header's height is defined by the links section, so it just scrolls off with it. When you turn off position: absolute, it goes back into the flow, so the height of the entire header is bigger... now, as you scroll, it sticks to the header, until the bottom of the header scrolls out of view. If you move the position sticky directly to the header it should solve the issue
Want results from more Discord servers?
Add your server