425

I am creating a mobile web page that is basically a big form with several text inputs.

However (at least on my Android cellphone), every time I click on some input the whole page zooms there, obscuring the rest of the page. Is there some HTML or CSS command to disable this kind of zoom on moble web pages?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Martín Fixman
  • 9,055
  • 9
  • 38
  • 46
  • 15
    I agree that it shouldn't be disabled for most sites, but there are some use cases where you may want to disable the default zooming - such as mobile web games where you may want to override zooming to do something else. – Luke Jan 15 '14 at 03:16
  • 2
    For Android Firefox users, there is the [Always Zoom for Firefox](https://addons.mozilla.org/en-us/firefox/addon/always-zoom/) add-on. Highly recommended. – Colin D Bennett Mar 10 '15 at 03:55
  • 3
    From **iOS 10**, `user-scalable=no` is ignored. See [this](http://stackoverflow.com/questions/37808180/disable-viewport-zooming-ios-10-safari) – Raptor Dec 06 '16 at 02:57
  • 6
    I am disabling zoom for a canvas project. Zooming could mess up the algorithms behind the animation. I agree, developers must consider usability prior to disabling zoom. Adjust font sizes and page structure accordingly. The `vmin` CSS measurement unit is helpful here. I also recommend using percentages and `vh` and `vw`. – www139 Dec 25 '16 at 18:48
  • We use a chromium browser control for our kiosk desktop application. If users can do pinch zoom it can get us in lots of troubles. Unfortunately the solutions below didn't work for me, and now I am trying to use --disable-pinch command. – A Khudairy Feb 20 '19 at 09:24

15 Answers15

600

This should be everything you need:

<meta name="viewport" 
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
6infinity8
  • 298
  • 4
  • 10
kgutteridge
  • 8,727
  • 1
  • 18
  • 23
  • 33
    this also disables the user's ability to zoom in general, as well as the browser's ability to auto-adjust the way the page fits into the viewport - all Martin is looking for is a way to disable the 'zoom-on-input-click' behavior. – matt lohkamp Mar 29 '11 at 19:49
  • I agree with matt. This doesn't really answer the question, what if I want to have zoom enabled in general, just not on input focus...? – Coomie Aug 29 '11 at 04:47
  • 3
    Right now someone at Posterous did just that, while having the font at 12px so it's unreadable and I cannot find a way around it. – Emil Ivanov Dec 01 '11 at 07:05
  • Only the first line. And move name="viewport" to the line's beginning. – Jack NUMBER Feb 18 '13 at 17:10
  • 74
    Every visually impaired person, including myself, hates this more than anything. I have to take screen grabs of pages that do this and then zoom in on them in the picture viewer. – Jack Marchetti Nov 21 '13 at 02:10
  • 6
    What does the second meta tag do? Isn't the `width=device-width` already in the first meta tag? – Luke Jan 15 '14 at 03:13
  • 2
    this doesn't seem to work on iOS 7. See lukad03 answer – davivid May 08 '14 at 19:09
  • 1
    For iOS7 too, replace user-scalable=0 with user-scalable=no. –  May 29 '14 at 06:35
  • It can be better to change the text-size. Have a look at this website: http://www.jonassebastianohlsson.com/blog/2013/11/25/how-to-stop-zoom-in-on-input-focus-on-mobile-devices/ – jeprubio Mar 16 '15 at 15:41
  • 1
    For anyone searching this issue now, this worked for me. It does not affect my browser version at all and disables zoom completely in my mobile version. As well as the annoying zoom when you focus on an input. (ios 9+) – Dalton May 08 '16 at 00:26
  • 1
    @JackMarchetti, surely there must be an browser extension to override this. I work a lot with maps where zooming within map vs. within page is really tough to solve neatly in any other way. – Henrik Jun 26 '20 at 09:49
  • 1
    @Henrik - what I ended up doing was creating a bookmark in mobile safari that removes this line of code using javascript. Setting user scale to zero has been less of a problem in recent years. – Jack Marchetti Jun 26 '20 at 23:02
  • 1
    (to everyone who thinks disabling zoom is a bad idea) For games, I really don't expect people to need to zoom into the 40px text of the buttons. – Ronald C Jun 20 '21 at 07:09
  • iOS has a zoom feature on the OS level. There's no need to have it in the webview for things like apps which are trying to have a similar experience to native apps. – Stephen Jan 11 '23 at 05:22
195

For those of you late to the party, kgutteridge's answer doesn't work for me and Benny Neugebauer's answer includes target-densitydpi (a feature that is being deprecated).

This however does work for me:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
falsarella
  • 12,217
  • 9
  • 69
  • 115
Luke Keller
  • 2,488
  • 3
  • 20
  • 23
74

There are a number of approaches here- and though the position is that typically users should not be restricted when it comes to zooming for accessibility purposes, there may be incidences where is it required:

Render the page at the width of the device, dont scale:

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

Prevent scaling- and prevent the user from being able to zoom:

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

Removing all zooming, all scaling

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
SW4
  • 69,876
  • 20
  • 132
  • 137
  • 2
    "typically users should not be restricted" but when developing web apps for mobile you often have to deal with the "zom-in on input focus". I find it helpful to disable zooming in that case. – Le 'nton Sep 18 '15 at 22:45
  • @Le'nton disabling zooming on the entire page is not a good way to deal with zoom in on input focus. On iOS at least, input focus zoom can be disabled by increasing the font size of the input. – pfg Feb 07 '20 at 22:55
  • `when developing web apps for mobile you often have to deal with the "zom-in on input focus"` This is not something to deal with but the app should be designed to work together with this feature. This kind of features were specifically designed to improve user experience (UX) and are based on best practices to help users. – Bartek Jul 20 '21 at 13:21
  • 3
    "typically users should not be restricted" I have never seen a mobile app that lets you zoom. Why should mobile web apps be different? Provided you are using rem units appropriately, the user can simply increase the font size on their device. – user3044394 May 09 '22 at 13:48
  • @user3044394 because most of the people that say it shouldn't be a thing because they themselves have never had a niche case where this would make sense. It's an option for a reason. They're just being ignorant. – ErraticFox May 08 '23 at 21:05
63

Seems like just adding meta tags to index.html doesn't prevent page from zooming. Adding below style will do the magic.

:root {
  touch-action: pan-x pan-y;
  height: 100% 
}

EDIT: Demo: https://no-mobile-zoom.stackblitz.io

Api
  • 1,582
  • 1
  • 14
  • 16
52

Mobile browsers (most of them) require font-size in inputs to be 16px.

And since there is still no solution for initial issue, here's a pure CSS solution.

input[type="text"],
input[type="number"],
input[type="email"],
input[type="tel"],
input[type="password"] {
  font-size: 16px;
}

solves the issue. So you don't need to disable zoom and loose accessibility features of you site.

If your base font-size is not 16px or not 16px on mobiles, you can use media queries.

@media screen and (max-width: 767px) {
  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="tel"],
  input[type="password"] {
    font-size: 16px;
  }
}
Sumit
  • 2,242
  • 1
  • 24
  • 37
Azamat Rasulov
  • 737
  • 1
  • 6
  • 12
  • 6
    Why does this solution have so few votes? It's by far the best way to avoid form-click zooming. – KlausCPH Apr 27 '18 at 19:36
  • 2
    Yes; this solved my problem as well! I didn't want to disable zooming entirely and this was just what I needed. – brendan Jul 17 '18 at 19:45
  • 2
    You can also set `font-size: 1rem` for the whole page, so the font scales nicely, simultaneously avoiding "focus-on-zoom" problem. – itachi May 27 '20 at 15:22
43

You can use:

<head>
  <meta name="viewport" content="target-densitydpi=device-dpi, initial-scale=1.0, user-scalable=no" />
  ...
</head>

But please note that with Android 4.4 the property target-densitydpi is no longer supported. So for Android 4.4 and later the following is suggested as best practice:

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
Benny Code
  • 51,456
  • 28
  • 233
  • 198
17

please try adding this meta-tag and style

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


<style>
body{
        touch-action: manipulation;
    }
</style>
Sarath Ak
  • 7,903
  • 2
  • 47
  • 48
12

Possible Solution for Web Apps: While zooming can not be disabled in iOS Safari anymore, it will be disabled when opening the site from a home screen shortcut.

Add these meta tags to declare your App as "Web App capable":

    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" >
    <meta name="apple-mobile-web-app-capable" content="yes" >

However only use this feature if your app is self sustaining, as the forward/backward buttons and URL bar as well as the sharing options are disabled. (You can still swipe left and right though) This approach however enables quite the app like ux. The fullscreen browser only starts when the site is loaded from the homescreen. I also only got it to work after I included an apple-touch-icon-180x180.png in my root folder.

As a bonus, you probably also want to include a variant of this as well:

<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
hossein sedighian
  • 1,711
  • 1
  • 13
  • 16
tlt
  • 358
  • 4
  • 10
9
<script type="text/javascript">
document.addEventListener('touchmove', function (event) {
  if (event.scale !== 1) { event.preventDefault(); }
}, { passive: false });
</script>

Please Add the Script to Disable pinch, tap, focus Zoom

Piotr Labunski
  • 1,638
  • 4
  • 19
  • 26
Prem
  • 153
  • 1
  • 7
4
<header>
 <meta name="viewport" content="user-scalable=no">
</header>

A pure "user-scalable=no" is sufficient and excluding other width and scale parameters should be fine for your case. In my case, I have just simply added the snippet on my . Tested on iOS 16.

JCTL
  • 88
  • 8
  • This makes any zooming and scrollbars not scalable. Everything is in birdview when running scrollbars on desktop – Bitfinicon Dec 06 '22 at 17:34
3

You can accomplish the task by simply adding the following 'meta' element into your 'head':

<meta name="viewport" content="user-scalable=no">

Adding all the attributes like 'width','initial-scale', 'maximum-width', 'maximum-scale' might not work. Therefore, just add the above element.

0

The solution using a meta-tag did not work for me (tested on Chrome win10 and safari IOS 14.3), and I also believe that the concerns regarding accessibility, as mentioned by Jack and others, should be honored.

My solution is to disable zooming only on elements that are damaged by the default zoom.

I did this by registering event listeners for zoom-gestures and using event.preventDefault() to suppress the browsers default zoom-behavior.

This needs to be done with several events (touch gestures, mouse wheel and keys). The following snippet is an example for the mouse wheel and pinch gestures on touchpads:

noteSheetCanvas.addEventListener("wheel", e => {
        // suppress browsers default zoom-behavior:
        e.preventDefault();

        // execution of my own custom zooming-behavior:
        if (e.deltaY > 0) {
            this._zoom(1);
        } else {
            this._zoom(-1);
        }
    });

How to detect touch gestures is described here: https://stackoverflow.com/a/11183333/1134856

I used this to keep the standard zooming behavior for most parts of my application and to define custom zooming-behavior on a canvas-element.

treeno
  • 2,510
  • 1
  • 20
  • 36
0

Using this post and a few others I managed to sort this out so that is compatible with Android and iPhone/iPad/iPod using the following code. This is for PHP, you can use the same concept for any other language with string searches.

<?php //Device specific headers
$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");

if($iPhone || $iPod || $iPad){
    echo '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />';
} else {
    echo '<meta name="viewport" content="width=device-width, initial-scale=1.0" />';
}

?>

zac
  • 2,223
  • 2
  • 15
  • 12
0

This may help someone: on iOS, my problem was fixed by swapping <button>s for <div>s, along with the other things mentioned.

Ronald C
  • 171
  • 6
-3
document.addEventListener('dblclick', (event) => {
    event.preventDefault()
}, { passive: false });
Tasos K.
  • 7,979
  • 7
  • 39
  • 63
Руслан
  • 675
  • 7
  • 8
  • 1
    Please elaborate your answer a bit more. We claim to be a reference book, i.e. the solutions should be explained. – max Nov 01 '20 at 10:27
  • 2
    Zoom in web page become from double click on the same point of the page . If you prevent default double click you will prevent zoom. This method works on ios(android must be too, havent tested yet). – Руслан Nov 03 '20 at 17:00