Disable zoom on a div, but allow zoom on the page (an alternate div)

57,032

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
57,032

Related videos on Youtube

GJDesigns
Author by

GJDesigns

Mostly considered a “Multimedia Specialist” for his variety of skills in computers; including Charleston Website Design, Graphic Design, Video Editing, Audio Editing, and 3D Design. Gregory was born and raised in Charleston, and graduated with a Bachelors Degree in Graphic Design and Media Arts from the University of South Carolina. Gregory built his first computer in 1996. Programming Languages: PHP, HTML, jQuery / javascript, CSS, MySQL, XML, Flash (ActionScript 3.0)

Updated on July 09, 2022

Comments

  • GJDesigns
    GJDesigns almost 2 years

    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' />
    

    • yckart
      yckart over 11 years
      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?
    • flexponsive
      flexponsive over 9 years
      Encountered the same problem and was able to (mostly) solve it: stackoverflow.com/questions/27983673/…
  • ido
    ido almost 10 years
    It is theoretically possible, check this guy's answer: stackoverflow.com/questions/15233076/…
  • the_farmer
    the_farmer about 9 years
    Is it a similar question with same solution? stackoverflow.com/questions/29674936/… >> Not worked for me
  • lowtechsun
    lowtechsun over 7 years
    Detect touch and hybrid touch/mouse devices with this and then change the header or fixed element's style value to absolute. Like this when zooming on mobile or touch input devices the fixed element(s) do not break the rest of the site but are natively zoomable per browser and OS and most of all user specifications (zoom-level, etc.).
  • lowtechsun
    lowtechsun over 7 years
    I would even go as far as saying, generally on user zoom set fixed to absolute for Desktop as well as Mobile and Hybrid Touch/Mouse devices. Future proof, let browser and OS handle the Zoom, user happy, accessibility granted, done. Remember, it is not your responsibility for the site to look responsive when it is zoomed! That is the user's decision and in doing so the user wants to zoom in on every part of the page, not just the content. So excluding certain divs from zooming is not good.
  • Basj
    Basj over 6 years
    I tried it, but it didn't work @AleksejKomarov. Could you provide a runnable example? (not in a SO snippet, because SO snippets are not runnable on Android phones - I already posted a meta-SO request about that)
  • Basj
    Basj over 6 years
    Sadly, it doesn't work for Chrome / Android 5: imgur.com/a/Yumvk
  • Basj
    Basj over 6 years
    Would you have a runnable example @DominicCerisano? Would be super useful, as the the accepted answer doesn't really work (see imgur.com/a/Yumvk).
  • Dominic Cerisano
    Dominic Cerisano over 6 years
    As I mentioned, this would be a lot of work since it would require separate implementations for each browser (Chrome, MOZ, etc) and platform (PC, mobile, etc).
  • clockw0rk
    clockw0rk almost 4 years
    kindof breaks the intend of zooming. if i (the user) want to zoom something, it is my decision to do so, you (the dev) preventing me from that kind of makes me want to leave the page asap
  • Dominic Cerisano
    Dominic Cerisano almost 4 years
    It implements the intended zooming as described by the OP.