On phones, this site shows a small dock at the bottom of the screen with two buttons: Menu and Contact us. Scrolling down on a phone, the dock jumped. It took four commits to fix, and three of them were fixing the fix.
Why a fixed element moves
The dock was position: fixed with a bottom offset. On iOS Safari the address bar sits at the bottom of the screen and collapses as you scroll down. A bottom-anchored fixed element follows the bottom of the visible area, so it rode the toolbar down and back up.
First fix: anchor from the top
CSS has viewport units that ignore the toolbar. 100svh is the viewport height with the toolbar showing, and it never changes while you scroll. So we stopped anchoring from the bottom and placed the dock from the top at 100svh, then pulled it up by its own height. On an iPhone in Safari it held still.
Second fix: only where the toolbar is at the bottom
Chrome on Android puts its collapsing bar at the top. There, the bottom edge of the fixed layer already stays put, and the new top anchor is what made the dock move. The fix that helped one browser broke the other.
So the bottom anchor became the default again, and the top anchor applies only under iOS WebKit, detected with @supports (-webkit-touch-callout: none). We also replaced a fixed safe-area subtraction with a min() of two positions: just above the toolbar when it shows, and clear of the home indicator when the small and large viewports are the same height (a home-screen web app, or an in-app browser with no toolbar).
Third fix: portrait only
In landscape, the iPhone's collapsing bar is at the top too. The bottom anchor already holds still there, and it keeps the home-indicator inset. So the iOS rule moved inside @media (orientation: portrait).
Fourth fix: not on iPads
iPadOS has no bottom toolbar, and a portrait iPad still shows the mobile dock. The iOS rule was capped to phone widths so a full-screen iPad keeps the bottom anchor. In the same commit we stopped relying on a utility class to emit the translate property and declared it in the rule itself.
The CSS ended up like this:
.site-dock {
bottom: calc(env(safe-area-inset-bottom) + 0.75rem);
}
@supports (-webkit-touch-callout: none) {
@media (orientation: portrait) and (max-width: 500px) {
.site-dock {
bottom: auto;
top: min(
calc(100svh - 0.75rem),
calc(100lvh - env(safe-area-inset-bottom) - 0.75rem)
);
--tw-translate-y: -100%;
translate: var(--tw-translate-x, 0) var(--tw-translate-y);
}
}
}What it still does not cover
Chrome for iOS, and Safari with its address bar set to the top, collapse a bar that CSS cannot detect. On those the dock may still move. Narrow iPad windows (Slide Over, or a one-third Split View) are as narrow as a phone, so they still get the top anchor, and we have not checked them on a device. The stylesheet comment says all of this, so the next person does not think it is solved everywhere.
What to take from it
- A fixed element anchored to the bottom follows the toolbar.
svhdoes not move. - Where the toolbar collapses decides which anchor holds still, and that differs by browser, orientation, and device.
- Test a mobile fix on more than the phone that reported it. Our first fix was right for one browser and wrong for the next.
- Write down what the fix cannot detect. An honest comment beats a false sense of done.