60

The code:

<select>
    <option value="1">Home</option>
    <option value="2">About</option>
    <option value="3">Services</option>
    <option value="4">Contact</option>
</select>

When I touch select, the iPhone zooms in that element (and does not zoom out after deselecting).

How can I prevent this? Or zoom back out? I can't use user-scalable=no because I actually need that functionality. It's for iPhone, select menu.

Mat
  • 202,337
  • 40
  • 393
  • 406
Atadj
  • 7,050
  • 19
  • 69
  • 94

12 Answers12

80

This can be prevented by setting font-size:16px to all input fields.

Supra
  • 1,612
  • 1
  • 18
  • 36
  • 1
    This worked for me perfectly. By far the easiest solution, and it's playing nice with iOS's intentions: to zoom when the text would otherwise be too small to read on mobile. – Alan Aug 13 '14 at 21:47
  • Perfect, this is the de-facto solution if you want to prevent the zoom on input focus but want to retain user pinch functionality! – hellojebus May 20 '15 at 18:27
  • 13
    This doesn't seem to work any more :/ ... I've tested it on an iPhone 6 with iOS 9.0.2 ... I went up to 20px for input fields, but it still insists on zooming to make the field fill the screen – Just Lucky Really Oct 17 '15 at 15:22
  • 1
    Same here, does not work for me on Iphone 5 with 9.2.1 – Alex Feb 23 '16 at 16:46
  • @Stretch I think you must have had something wrong and made an incorrect assumption. It still works fine for me on iOS 9.3.4 – Joshua Goossen Aug 24 '16 at 13:50
  • 2
    It does still works today with iPhone 5S and iOS 9.3.2 – Tien Do Oct 06 '16 at 04:21
  • 2
    @Alex It works for me with the same iOS version on iPhone 5S. And on this device, setting font-size to 16px only on `input:active` seems to do the trick without altering the apparent font-size. The design in unchanged, but the auto zoom is disabled. – Eria Oct 11 '16 at 08:28
  • @Eria Seems quite unreliable then. – Alex Oct 11 '16 at 08:35
  • 1
    "Redesign your website" is not a good answer to a bug introduced against the web standards by an opinionated development team. Safari should fix this. It is forced "accessibility" behavior that is going to result in more and more people turning zoom off, which in turn, actually ends up hurting accessibility. – dudewad May 23 '20 at 05:52
75

UPDATE: This method no longer works on iOS 10.


It depend from the Viewport, you can disable it in this way:

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>

add user-scalable=0 and it should work on your inputs as well.

Andrea Turri
  • 6,480
  • 7
  • 37
  • 63
  • 28
    I need zoom functionality on website. I just don't want it for forms. – Atadj Jun 16 '12 at 14:34
  • 21
    try to give the `font-size: 16px` to: `input[type="text"], input[type="number"], textarea`. – Andrea Turri Jun 16 '12 at 14:46
  • 2
    It works starting from font-size: 40px! :) (viewport set to width=735px). Thanks! – Atadj Jun 16 '12 at 16:48
  • 5
    This disables zoom, use font-size for a better user experience. – Charlie Brown May 05 '14 at 14:30
  • 1
    Unfortunately, this no longer works on iOS 10 Safari. It seems to ignore user-scalable because, well... Apple decides what it wants apparently. – Nick Bedford May 17 '17 at 23:07
  • 3
    @AndreaTurri At least edit your answer to save people time. – aleclarson Jun 16 '17 at 21:57
  • i'm using Bootstrap 4.x setting font-size-base:1rem and adding user-scalable=0 in the viewport meta tag of the index HTML file works for me. – Amir Mehrabi May 30 '20 at 21:23
  • @NickBedford it's not that Apple decides what it wants. It's that users should decide what they want! Disabling pinch-to-zoom is bad UX on 99.9% of apps, but a double-digit percentage of them try to do it anyways. The asker understands this (they want user zoom, just not the annoying autozoom for text fields). – hegel5000 Aug 08 '23 at 18:22
25

For iOS, you can avoid zooming of input elements by simply allocating a font size to them that's considered sufficient by the OS (>=16px), thus avoiding the need to zoom, e.g.:

input, select, textarea {
    font-size: 16px;
}

It's a solution also utilized by various frameworks and allows you to avoid the use of a meta tag.

codingmechanic
  • 275
  • 4
  • 7
3

This might be helpful to look at:

Disable Auto Zoom in Input "Text" tag - Safari on iPhone

You'd basically need to capture the event of tapping on a form element, then not run the default iOS action of zooming in, but still allowing it to zoom for the rest of the page.

Edit:

The link mentions,

2) You can dynamically change the META viewport tag using javascript (see Enable/disable zoom on iPhone safari with Javascript?)

To elaborate:

  1. Viewport meta tag is set to allow zooming
  2. User taps on form element, changes meta tag to disable zooming
  3. Upon pressing done, viewport is changed to allow zoom

And if you can't change the tag when clicking on a form element, put a div that mimics the form element that when you press it, it changes the tag, then calls the input.

Community
  • 1
  • 1
Charlie
  • 11,380
  • 19
  • 83
  • 138
  • It contains 3 solutions - none satisfy me :( (2 don't actually work in my case). – Atadj Jun 16 '12 at 14:40
  • Edited with a possible solution – Charlie Jun 16 '12 at 15:20
  • Thanks @Charlie. Your solution indeed works like expected but another person suggested something simpler that is excellent in my situation. I only needed to raise SELECT's font-size to 40px (viewport width=735px). Now iPhone navigation is clearly readable and it doesn't zoom. – Atadj Jun 16 '12 at 16:49
3

The most up voted answer to set the font-size does not work for me. Using javascript to identify the client together with the meta tags in the answers here, we can prevent the zooming behavior of iPhone on input focus while otherwise keeping the zooming functionality intact.

$(document).ready(function ()
{
    if (/iPhone/.test(navigator.userAgent) && !window.MSStream)
    {
        $(document).on("focus", "input, textarea, select", function()
        {
            $('meta[name=viewport]').remove();
            $('head').append('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">');
        });

        $(document).on("blur", "input, textarea, select", function()
        {
            $('meta[name=viewport]').remove();
            $('head').append('<meta name="viewport" content="width=device-width, initial-scale=1">');
        });
    }
});

It seems like we have to replace the meta tag with new values on the blur-event, just to remove it does not seem to trigger an updated behavior.

Note that the UI is still initializing the zoom, but it quickly zooms back out again. I believe this is acceptable and iPhone users must already be accustomed to that the browser is having some dynamic zooming going on anyway in applicable scenarios.

Community
  • 1
  • 1
Alex
  • 14,104
  • 11
  • 54
  • 77
3
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>

Not working anymore on iOS10.0.1

font-size:16px works

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
atorgfr
  • 161
  • 6
  • I tried something similar, and it worked on ios 10.3.3. i set minimum and maximum scale to 1, and omitted user-scalable (assuming it defaults to yes) – Hugo Silva Nov 10 '17 at 02:59
  • the issue with this code is if you set the user-scale to 0, the user won't be able to zoom at all which is not a user friendly! – Midz Elwekil Nov 05 '19 at 13:30
2

Setting the font size works perfectly for input elements, but not for select elements for me. For select tags, I need to actively disable viewport zoom when the user starts interacting with the select element, and then reenable it on finish.

//the mousedown event seems to be the one that does the trick, versus 'focus', because focus fires after the zoom already happens.
$('select').mousedown(function(){
  $('meta[name=viewport]').remove();
  $('head').append('<meta name="viewport" content="width=device-width, maximum-scale=1.0, user-scalable=0">');
})

$('select').focusout(function(){
  $('meta[name=viewport]').remove();
  $('head').append('<meta name="viewport" content="width=device-width, initial-scale=yes">' );
})
gregkerzhner
  • 736
  • 7
  • 18
0

Just found a simple fix if you're using Bootstrap:

As mentioned in w3s: You can quickly size labels and form controls within a Horizontal form by adding .form-group-lg to the element.

<form class="form-horizontal">
  <div class="form-group form-group-lg">
    <label class="control-label">Large Label</label>
    <div>
      <input class="form-control" type="text">
    </div>
  </div>

See second example on this page: http://www.w3schools.com/bootstrap/bootstrap_forms_sizing.asp

Tested it in Safari and Chrome on an iPhone SE and it works like a charm!

0

So here is the final fix which works well for me.


    @media screen and (-webkit-min-device-pixel-ratio:0) { 
      select,
      textarea,
      input {
        font-size: 16px !important;
      }
    }

Adeel Malik
  • 144
  • 5
0

here is the jQuery Solution works well for me.

device_type = (ua.match(/iPad/i) || ua.match(/iPhone/)) ? "touchstart" : "click";
if(device_type === "touchstart" ){
$('head').append('<style type="text/css">input,  select, textarea {font-size: 16px;}</style>');
}
jalalBK
  • 31
  • 2
0

Use maximum-scale=1 instead of user-scalable=no to prevent the form zooming issue without breaking the user’s ability to pinch zoom.

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

daxmacrog
  • 8,913
  • 1
  • 15
  • 16
  • As of October 2020, this seems to break the ability to pinch zoom (presumably because the scale would be > 1) – allicarn Oct 05 '20 at 23:42
0

We ran into this issue at my work and found a similar answer to @alex. We can manipulate the viewport tag if it is an iOS device:

document.addEventListener('DOMContentLoaded', event => {
  if (/iPhone/.test(navigator.userAgent) && !window.MSStream) {
    const metaViewportTag = document.head.querySelector('meta[name="viewport"]')
    metaViewportTag.content = 'width=device-width, initial-scale=1, maximum-scale=1'
  }
})

This prevents zooming form controls on focus in iOS and still allows Android to work as normal.

souporserious
  • 2,079
  • 2
  • 27
  • 47