The code below is attached to window.onresize = resize;
. The baseWidth
and baseHeight
are read on load as a basis for the calculations. The main
variable is defined just by setting it to the main html node. The font is set on a block element to cause all of the em
based elements within it to resize in kind. When the width or height of the browser is changed then the ratio is recalculated. Please see demo to understand what I achieve with JS but would like to find a pure CSS solution: http://codepen.io/anon/pen/nLauF
I have been exploring options in CSS3 such as calc
. Feel free to also suggest any improvements to the JS below also.
function resize() {
var height = 0,
width = 0;
if(window.innerWidth <= window.innerHeight) {
size = window.innerWidth / baseWidth;
height = baseHeight * size;
width = window.innerWidth;
} else {
size = window.innerHeight / baseHeight;
height = window.innerHeight;
width = baseWidth * size;
}
if(baseWidth * size > window.innerWidth) {
size = window.innerWidth / baseWidth;
height = baseHeight * size;
width = window.innerWidth;
}
main.style.height = height + "px";
main.style.width = width + "px";
main.style.fontSize = size * 16 + "px";
}
Thanks!