Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

position: sticky jumps when selecting option

I'm trying to use the css style, position: sticky. It works great except for when the select element becomes active. The page scrolls back to the position of the sticky element.

<div style="position: sticky; position: -webkit-sticky; top: 0;">
    <select>
        <option>Option</option>
    </select>
</div>

https://codepen.io/anon/pen/vrxVoy

I couldn't find anything online that addressed this. Has anyone ran into this before and have a solution?

Thanks!

Browser: Chrome 67.0.3396.79

OS: Windows 7 Home Premium.

Sticky works as expected in Firefox.

Bug report found: https://bugs.chromium.org/p/chromium/issues/detail?id=805800#c9

like image 795
Josh Avatar asked Jun 11 '18 18:06

Josh


People also ask

Why my position sticky is not working?

Sticky Element Has Parent(s) with overflow PropertyIf the sticky element has a parent or ancestor with overflow: hidden , overflow: auto , or overflow: scroll , then position: sticky will not work properly.

How does the position sticky property work?

Setting position sticky Position sticky alternates the position of an element between relative and fixed based on the viewer's scroll position. A sticky element is relative to the document flow until a defined scroll position is reached, then it switches to behaving like a fixed element within its direct parent.

Is position sticky supported?

Position Sticky is supported by all major modern browsers, except for old IE. For Safari browsers you will need to add the -webkit prefix.


1 Answers

The problem is because it is applied to a form element. When you click on the element, the browser is programmed to go to the location of it if it's base location is off-screen. Firefox is programmed differently and only does this if the element itself is off-screen.

To fix it, try using position:fixed; instead. Since there isn't anything above the element there's no reason to use sticky.

like image 184
commandertuna Avatar answered Oct 20 '22 22:10

commandertuna