42
votes

Is there a way to disable zoom on a div, or any particular elements on a website? For example, if I wanted the page to be zoomable, but not the #Header div, is there a way to make one zoomable, and the other not zoomable?

Basically, when you zoom on a mobile device, it zooms the Header too, but I want the header to be a fixed size at all times (not zoomable).

I know that you can use this code to disable zooming overall:

<meta content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' name='viewport' />

5
If you zoom on the entire page or particular on your header? Or further if you touch just the header or the total page? And, btw: Why you want that? The visitor can't see the full page... a bit crappy, eh?yckart
Encountered the same problem and was able to (mostly) solve it: stackoverflow.com/questions/27983673/…flexponsive

5 Answers

7
votes

You can't do that without clever hacks.

However, you can (and should) use the following CSS to fix zoom issues on mobile devices:

header {
    position: fixed;
    ...
}

@media only screen and (max-width: 720px) {
    header {
        position: absolute;
    }
}

This code enables position: absolute when display width is less or equal 720px, and header becomes the part of the page, rather than being fixed on top.

1
votes

I don't think you can do that directly. One possible option would be to detect the zooming through js events and scale elements accordingly.

Another option would be to "break" the CTRL key to disable zooming on your website, but that's just a big no-no.

1
votes

In shorter, you certainly can do that.

You can trap window resize events and resize your floating div according to the dpi change calculated from the various new window and inner width and height attributes.

So, when you zoom in, you want to shrink the floating div so it retains the original dpi, and vice versa.

This would be an epic fiddle - revisit this answer soon, since I may have to do such a thing. Already noticing some cross-browser inconsistencies with dpi, so yeah, fun.

0
votes

Faced the same problem an ended up disabling panning/zooming

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

and selectively reenable it with this great lib

https://soulfresh.github.io/pan-z/?path=/docs/pan-z--pan-z

Works great without much configuration.

My html-structure is as follows

<body>
  <header>Sticky unzoomed header</header>
  <main id='main'>Zoomable content</main>
</body>

Then I enabled panzooming on the main element

const PanZ = require('@thesoulfresh/pan-z')
new PanZ().init(document.getElementById('main'))

-4
votes

If you are using angular there is a way to give one id to your header div and then write the following code in controller:

document.getElementById("viewport").setAttribute('content','user-scalable=yes, width=device-width, minimum-scale=1, maximum-scale=1');

I used in my project and it worked well..