6

Is there an html / css / javascipt way to maintain a <div> at a constant size in the face of the user's zooming the page in and out? That is, using control-plus to increase text size and control-minus to reduce it.

EDIT: The kicker, I guess, is that I want the content of the <div> to stay the same size, too.

Thanks!

EDIT: My goal was (and is) to keep an AdSense <div> from expanding so much as to obscure a lot of the real content on the page. But come to find out (thank you @thirtydot) there's really no good way to do this. The answer, for me (thank you @Neal!): give the <div> overflow:scroll so as to sacrifice its content rather than the content I'm trying to show.

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
  • Do you want to keep it a fixed px width and height, or fixed size relative to the size of the browser window? – LeeR May 24 '11 at 21:51
  • post the code you are dealing with, kinda open ended. Do you mean a constant relative (to the viewport) size or constant as in using pixels? – colinross May 24 '11 at 21:51
  • How is the user resizing? Do you mean by zooming or by changing the browser window size? – robertc May 24 '11 at 21:59
  • @lee @colinross -- constant as in 1.255 microfurlongs wide. – Pete Wilson May 24 '11 at 22:03
  • @robertc -- I meant (and mean) zooming, not resizing the viewport. Sorry for the phumbled phraseology. – Pete Wilson May 24 '11 at 22:08
  • I knew what you meant from the question title alone, not sure why other people are having a hard time understanding this. – Wesley Murch May 24 '11 at 22:10
  • @Wesley Murch -- Thank you [sob!] Wesley! – Pete Wilson May 24 '11 at 22:11
  • 2
    Why would you ever want to confuse your users? Thats like blocking right clicking on your site or opening every link in an modal w/ an iframe on your site -_- – Oscar Godson May 24 '11 at 22:11
  • @PeteWilson: But now I see you said "resize" at first, which is not correct. – Wesley Murch May 24 '11 at 22:12
  • @WesleyMurch Do you mean the question title now, or the question title before it was edited? – robertc May 24 '11 at 22:13
  • @Oscar Godson -- I want to ***un*** -confuse the visitor: I finally edited in the goal of this question which would have calmed you if it had been there for you to see. – Pete Wilson May 25 '11 at 21:22
  • I know the goal and i understood the first time, but it's _always_ a bad idea to take browser functionality and change it. Like i said, right clicking, or set ctrl/cmd+p to open a new window or something. If the zoom (cmd/ctrl++, right?) and it's not zooming properly like changing the structure of the site you should really submit it to as a bug report because it _should_ zoom in and not change the structure. – Oscar Godson May 26 '11 at 03:22

6 Answers6

2

.box {
    background: red;
    width: 5vw;
    height: 10vh;
    position: absolute;
    top: 10vh;
    left: 5vw;
}
<div class="box"></div>
royaltsp
  • 41
  • 1
  • 5
  • 1
    If anyone wants a reason why it works, it is because when the user zooms, the viewport width goes down, and the content size goes up. This means that, in this case, it all stays the same, and they all live happily ever after the end. – corn on the cob Oct 22 '20 at 14:54
1

To make the div size invariant of zooming (But not contents inside it) do the following :
Inside your css for that div :

min-width: 100%;
max-width: 100%;

This will freeze the width, you can do the same for height too.

Hemanth Malla
  • 405
  • 1
  • 5
  • 9
1

I am not sure what you mean, just use css:

div#id {
    width: 100px; /*or some other #*/
    height: 100px; /*or some other #*/
}

html:

<div id="id">some content</div>
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 2
    I hope I'm wrong but browsers seem to resize all content, including pixel-dimensioned content. Not just pixel-dimensioned, but any-absolute-measure-dimensioned. Please make my hopes come true -- tell me I'm wrong! – Pete Wilson May 24 '11 at 21:54
  • @Pete, browsers only `resize` content when the content inside is bigger than what is given. you can add `overflow: hidden` to the css to hide anything that would be outside the boundary – Naftali May 24 '11 at 21:55
  • @Pete - You mean that when the user zooms the browser window, you want an element to remain the same absolute size? – james6848 May 24 '11 at 21:58
  • @james6848 -- yes, that's what I mean. Gosh dang it, I should have said 'zoom' instead of 'resize.' I'll edit that right now. – Pete Wilson May 24 '11 at 22:04
1

There is no good way (read: reliable) to do this. Sorry.

What you're asking for basically boils down to detecting the zoom level of the browser, and there's a great answer here (confirming just how difficult this is):

As stated in that answer, there is a "kinda" cross-browser crazy way involving the use of Flash, but there are downsides:

  • It uses Flash.
  • It's not reliable if the user loads your page already zoomed in.
  • It uses Flash. Yes, this is so bad that I said it twice. Think of all those iPhones/iPads.

Anyway, it's here:
http://blog.sebastian-martens.de/2009/12/how-to-detect-the-browser-zoom-level-change-browser-zoo/

Community
  • 1
  • 1
thirtydot
  • 224,678
  • 48
  • 389
  • 349
0

I read in another post a solution that I didn't test yet...

Maintain div size (relative to screen) despite browser zoom level

that's the used javascript:

//This floating div function will cause a div to float in the upper right corner of the screen at all times. However, it's not smooth, it will jump to the proper location once the scrolling on the iPhone is done. (On my Mac, it's pretty smooth in Safari.) 

function flaotingDiv(){

    //How much the screen has been zoomed.
    var zoomLevel = ((screen.width)/(window.innerWidth));
    //By what factor we must scale the div for it to look the same.
    var inverseZoom = ((window.innerWidth)/(screen.width));
    //The div whose size we want to remain constant.
    var h = document.getElementById("fontSizeDiv");

    //This ensures that the div stays at the top of the screen at all times. For some reason, the top value is affected by the zoom level of the Div. So we need to multiple the top value by the zoom level for it to adjust to the zoom. 
    h.style.top = (((window.pageYOffset) + 5) * zoomLevel).toString() + "px";

    //This ensures that the window stays on the right side of the screen at all times. Once again, we multiply by the zoom level so that the div's padding scales up.
    h.style.paddingLeft = ((((window.pageXOffset) + 5) * zoomLevel).toString()) + "px";

    //Finally, we shrink the div on a scale of inverseZoom.
    h.style.zoom = inverseZoom;

}

//We want the div to readjust every time there is a scroll event:
window.onscroll = flaotingDiv;
Community
  • 1
  • 1
Diego Mariano
  • 191
  • 2
  • 6
0

You should just be ablemto set a width and height in css using a px measurement

Eg

div
{
width:100px; height:200px;
}
isNaN1247
  • 17,793
  • 12
  • 71
  • 118