iOS input focused inside fixed parent stops position update of fixed elements
Asked Answered
T

3

37

The following happens on Mobile Safari iOS 6.1.2

Steps to reproduce

Create a position: fixed element with an <input type="text"> element inside it.

Actual result

  1. Input - not focused

    The position of the fixed elements is correct when input is not focused.

  2. Input - focused

    When the input is focused, the browser enters a special mode in which it does not update the position of the fixed elements any more (any fixed positioned element, not just the input's parent) and moves the whole viewport down in order to make the input's parent element sit in the center of the screen.

    See live demo: http://jsbin.com/oqamad/1/

Expected result

The position of the fixed elements is always respected.

Fix or workaround?

Any clues as how to force Safari to properly display the fixed elements would be helpful.

I would prefer a workaround which does not involve using position: absolute and setting an onscroll event handler.

Tutuila answered 4/3, 2013 at 10:11 Comment(6)
Remy Sharp made a live video of the bug in action back in May 2012 on iOS 5: youtube.com/watch?v=lrnvZDwgJRcTutuila
It is an unfixable bug as of now. Fixed positioning is just starting to gain more expected support amongst mobile devices.Hodgkinson
You can use js to aling the div to bottom of the page. That's what I did for my footer. Set a timeout function or call the reposition function on the scroll event.Standice
2 years later, the issue still persist.... WTF Safari iOS?Ideate
Possible duplicate of several SO questions. See gist.github.com/avesus/… for details.Sn
@ValentinAgachi , i too have the same prob, did u get any answer #48662628Traditional
S
8

It's a well known bug on Safari, both on iPad and iPhone.

A workaround it's to change the position to absolute to all elements set with fixed position.

In case you are using Modernizr you could also target mobile devices this way:


jQuery code


$(document).ready(function() {

  if (Modernizr.touch) {

      $(document)

        .on('focus', 'input', function(e) {
            $('body').addClass('fixfixed');
        })

        .on('blur', 'input', function(e) {
            $('body').removeClass('fixfixed');
        });

      }

});

CSS


If I want to target just the header and footer for example, that are set with fixed position, when the class 'fixfixed' is added to the body I can change the position to absolute to all elements with fixed position.

.fixfixed header, .fixfixed footer {
  position: absolute;
}
Schroer answered 25/11, 2013 at 23:48 Comment(4)
Changing from fixed to absolutely will cause a flicker on the screen because the element will no longer be where it's meant to be.Gi
That's not enough to downvote my answer. You can avoid the flickering in many ways, one of them is adding -webkit-transform: translate3d(0,0,0); to your container. And btw, I replied to this answer 2 years ago, so depending on browser it might not work.Schroer
Is there any modern solution for this problem?Lowerclassman
Is there any ES6 solution for this?Menado
V
1

Position:Fixed has a lot of well known-bugs, you can check them here: Remysharp article. Probably you can still fix some of them using the answers from this question: CSS “position: fixed;” into iPad/iPhone?

Good luck!

Vaticide answered 16/5, 2013 at 3:25 Comment(0)
Z
0

Yep, this is specifically an iOS safari issue since v5. If you use Chrome on iOS you'll notice it will work OK. The bodge I use to fix this is to create and add a style element to the body and subsequently delete it and then perform the focus on the element.

Don't ask me why this works, just too many hours of trial and error!

I've also noticed that after the first time this is fixed you don't have to perform the style trick for subsequent fixed positioned elements

Zug answered 27/6, 2013 at 23:58 Comment(1)
Can you provide a code example so we can upvote if it works? ThanksAntislavery

© 2022 - 2024 — McMap. All rights reserved.