css - Maintain aspect ratio and font size based on browser height and width? -
the code below attached window.onresize = resize;
. basewidth
, baseheight
read on load basis calculations. main
variable defined setting main html node. font set on block element cause of em
based elements within resize in kind. when width or height of browser changed ratio recalculated. please see demo understand achieve js find pure css solution: http://codepen.io/anon/pen/nlauf
i have been exploring options in css3 such calc
. feel free suggest improvements 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!
i wrote code including font-size calculation vmin units :
css :
main { width: 80vmin; height: 60vmin; background-color: #000; position: absolute; top:0; bottom:0; left:0; right:0; margin:auto; } h1 { color: #fff; font-size: 30px; /* general fallback */ font-size: 5vm; /* ie9 fallback */ font-size: 5vmin; }
for browser support, can check caniuse
Comments
Post a Comment