How to Make Fixed Content Go Above iOS Keyboard
In this article
The short answer: on iOS, a position: fixed element at the bottom of the screen does not move when the on-screen keyboard appears. The keyboard slides up over your fixed footer, hiding it. To make fixed content sit above the keyboard, you need a small amount of JavaScript that listens for the keyboard's appearance and shifts the element up by the keyboard's height, or you need to switch from fixed to a layout that the browser resizes for you. The most reliable approach in modern iOS Safari is to use the visualViewport API, which reports the visible area of the page and changes when the keyboard opens. This guide walks through the working method, the CSS-only alternatives, and the traps that make it fail.
What You'll Need
- A web page you control (HTML, CSS, and JavaScript).
- An iPhone or iPad running a current version of iOS, plus Safari for testing. Chrome on iOS uses the same WebKit engine, so behaviour is nearly identical.
- A way to view the page on the device, such as a local server on your network or a staging URL.
- Basic familiarity with CSS positioning and JavaScript event listeners.
Note that menu labels and settings in iOS and Safari can differ slightly by version, device, and region. The APIs below are stable, but always test on the exact iOS version you support.
Step-by-Step
- Reproduce the problem first. Put a fixed bar at the bottom of a test page:
.bar { position: fixed; bottom: 0; left: 0; right: 0; }. Add an input field above it. Open the page in iOS Safari, tap the input, and watch the keyboard cover the bar. This confirms the fixed element is anchored to the layout viewport, not the visual viewport. - Decide between a JavaScript fix and a layout change. If the bar must stay pinned to the bottom of the screen at all times, use the
visualViewportmethod below. If the bar only needs to sit above the keyboard while the user types, consider making the page a flex column with a scrollable middle and a non-fixed footer; iOS resizes the layout viewport when the keyboard opens in many cases, and the footer moves up naturally. The flex approach is simpler but changes the page structure. - Add the visualViewport listener. In your script, check for support:
if (window.visualViewport) { ... }. Then attach handlers toresizeandscrollonwindow.visualViewport. Inside the handler, compute the keyboard height aswindow.innerHeight - visualViewport.height - visualViewport.offsetTop. Clamp it to zero so a negative value never pushes the bar down. - Apply the offset to the fixed element. Set
element.style.transform = 'translateY(-' + keyboardHeight + 'px)'or adjust itsbottomvalue. Usingtransformis usually smoother because it does not trigger layout. Reset the transform totranslateY(0)when the keyboard height is zero, which happens when the keyboard closes. - Handle the focus and blur events as a backup. Some iOS versions fire
visualViewportevents late. Addfocusinandfocusoutlisteners on the document. On focus of an input, run the same height calculation after a short delay (for example 300 ms) so the keyboard has finished animating. On blur, reset the offset. - Keep the input visible too. When the bar moves up, the focused input may still be hidden behind the keyboard if it sits low on the page. Call
element.scrollIntoView({ block: 'center' })on the focused input after the keyboard opens, or scroll the container manually. Test with the input near the bottom of a long form. - Test rotation and split view. Rotate the device and open the keyboard again. The visual viewport height changes, so the handler must recalculate. On iPad, test with the keyboard undocked or split, where the keyboard does not span the full width; the offset calculation still works because it uses height, but verify the bar does not overlap the keyboard's top edge.
- Add a CSS fallback for browsers without visualViewport. Wrap the JavaScript in a feature check. For older iOS versions, a common fallback is to listen for
focuson inputs and apply a fixed offset equal to a typical keyboard height, but this is approximate. Better to accept that very old versions will not move the bar and keep the bar's content reachable by other means, such as a button that scrolls the page. - Verify with the keyboard's accessory bar. iOS shows a suggestion bar above the keyboard. The visual viewport includes that bar, so your offset will account for it automatically. Confirm the bar does not sit under the suggestions.
- Clean up listeners when the page unloads. Remove the
resize,scroll,focusin, andfocusouthandlers if your page is a single-page app that swaps views. Leftover listeners cause the bar to jump or stay offset after navigation.
Common Mistakes
- Using
window.innerHeightalone. On iOS,innerHeightoften does not change when the keyboard opens, so a resize listener onwindownever fires. You must usevisualViewport. - Forgetting
offsetTop. When the page is scrolled, the visual viewport's top offset is non-zero. Ignoring it makes the calculated keyboard height too large and pushes the bar too far up. - Animating
bottominstead oftransform. Changingbottomtriggers layout on every frame and can stutter. Usetransformor a CSS transition ontransform. - Assuming Android behaves the same. Android browsers vary; some resize the layout viewport, some do not. The visualViewport method works on modern Android Chrome too, but test separately.
- Relying on
position: sticky. Sticky positioning does not lift an element above the keyboard; it sticks within its scroll container. It is not a substitute for the offset calculation.
Tips
- Debounce the resize handler slightly (for example 50 ms) to avoid recalculating during the keyboard's slide animation, then run a final calculation on
resizeendif available. - Store the last known keyboard height and only update the transform when it changes by more than a pixel or two. This prevents jitter.
- Use
env(safe-area-inset-bottom)in your CSS for the bar's padding so it clears the home indicator on newer iPhones, and add the keyboard offset on top of that. - If the bar contains a button, make sure the button remains tappable after the transform. Transforms do not break hit testing, but overlapping elements can. Check the z-index.
- Test with the keyboard's predictive text on and off; the bar height changes and your offset should follow.
Troubleshooting
If the bar does not move at all, confirm that window.visualViewport is defined in the device's Safari. If it is undefined, the iOS version is too old for this API; use the focus/blur fallback or restructure the page. If the bar moves but overshoots, log visualViewport.height and offsetTop while the keyboard is open and compare with window.innerHeight; the difference is your true keyboard height. If the bar moves only after a delay, the resize event is firing late; add the focusin handler with a short timeout. If the bar jumps back down while typing, a scroll event is resetting the transform; make sure your handler reads the current values rather than a cached height. If the bar covers the input, the scrollIntoView call is missing or the input is inside a container with its own scroll; scroll the correct ancestor. If the bar behaves correctly in Safari but not in an in-app browser, remember that in-app browsers on iOS are still WebKit but may have different viewport behaviour; test in the actual app and, if it fails, consider opening the page in Safari.
Frequently Asked Questions
Does position: fixed ever move above the keyboard on its own?
No. Fixed elements are positioned relative to the layout viewport, which iOS does not resize when the keyboard appears. You must offset the element manually or change the layout.
Can I do this with CSS only?
Not reliably for a truly fixed bar. A flex-column layout with a scrollable middle section lets the footer move with the resized viewport in some cases, but iOS does not always resize the layout viewport, so JavaScript is the dependable route.
Will this work in Chrome on iOS?
Yes, because Chrome on iOS uses the same WebKit engine as Safari. The visualViewport API behaves the same. Test on the specific iOS version you target.
Why does my calculated keyboard height keep changing?
The visual viewport includes the keyboard's accessory bar and can change during the keyboard animation. Debounce your handler and clamp the value to a sensible range.
Does the keyboard height differ between iPhone and iPad?
Yes, and it changes with rotation and with the keyboard undocked or split on iPad. Always recalculate on resize and orientation change rather than hard-coding a height.
What if I cannot use JavaScript at all?
Then you cannot reliably lift a fixed bar above the keyboard. Restructure the page so the input and its controls are near the top of the visible area, or use a full-screen modal for text entry, which iOS scrolls into view automatically.