Preserve browser window aspect ratio while shrinking [closed]

That’s not a good idea. Resizing the window is frowned upon, and in recent browser versions it’s rarely allowed. See these notes about the rules that Firefox imposes.

However, it would be possible, if you are allowed to resize the window, to call window.resizeTo with the right parameters computed using window.innerWidth, window.innerHeight, window.outerWidth, window.outerHeight.

Something like this should work:

window.onresize = function() {
    var targetInnerHeight = window.innerWidth * 800 / 1200;
    var heightDiff = window.outerHeight - window.innerHeight;
    var targetOuterHeight = targetInnerHeight + heightDiff;
    window.resizeTo(window.outerWidth, targetOuterHeight);
}

But again, you shouldn’t do this, in most cases the browser won’t let you do that for security reasons, and it’s not nice to manipulate the user’s browser and prevent the user from resizing the browser as he pleases.

Leave a Comment