Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save avesus/957889b4941239490c6c441adbe32398 to your computer and use it in GitHub Desktop.
Save avesus/957889b4941239490c6c441adbe32398 to your computer and use it in GitHub Desktop.
September 2017 iOS WebKit input focus position: fixed

Mobile Safari does not support position: fixed when an input focused and virtual keyboard displayed.

To force it work the same way as Mobile Chrome, you have to use position: absolute, height: 100% for the whole page or a container for your pseudo-fixed elements, intercept scroll, touchend, focus, and blur events.

The trick is to put the tapped input control to the bottom of screen before it activates focus. In that case iOS Safari always scrolls viewport predictably and window.innerHeight becomes exactly visible height.

Open https://avesus.github.io/docs/ios-keep-fixed-on-input-focus.html in Mobile Safari to see how it works.

Please avoid forms where you have several focusable elements because more tricks to fix position will be necessary, those were added just for demonstration purposes.

Note that for rotation and landscape mode, additional tricks are necessary. I'm working on a framework called Tuff.js which will provide a full-screen container helping mobile web developers to build web applications much faster. I've spend almost a year on the research.

By the way, to prevent scrolling of the whole window when virtual keyboard is active, you can use this super simple trick: https://codepen.io/WinterJoey/pen/WvwyqX

@ThariqS
Copy link

ThariqS commented Mar 7, 2018

Thank you, this is super useful

@bardzh
Copy link

bardzh commented Nov 1, 2019

this doesn't work anymore!

@z1kman
Copy link

z1kman commented Aug 4, 2022

this does not work for situations where the screen has a scrollbar. For such cases, you can use window.visualViewport.height.

@lightrow
Copy link

lightrow commented May 1, 2024

the world needs another browser deprecation countdown website, but this time for safari instead of IE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment