unit-vh-vw-overflow-auto-ref-iframe.html (1242B)
1 <style> 2 body { overflow: hidden } 3 div { 4 width: 10px; 5 height: 10px; 6 background-color: #d64203; 7 } 8 </style> 9 10 <body> 11 <div id="a"></div> 12 <div id="b"></div> 13 <div id="c"></div> 14 <div id="d"></div> 15 16 <!-- 17 Deliberately make the scrollbars appear to ensure that with 'overflow: auto' 18 viewport units are calculated as if the scrollbars are _not_ there. 19 --> 20 <div style="width: 500px; height: 500px; background-color: black"> 21 </body> 22 23 <script> 24 // client{Width, Height} consist of the area _inside_ the scrollbars, so we need 25 // to calculate these units with 'overflow: hidden' set to ensure that there are 26 // no scrollbars. This reflects the fact that with 'overflow: auto' set, viewport 27 // units are sized without taking the scrollbars into account. 28 var vw = 0.01 * document.body.clientWidth; 29 var vh = 0.01 * document.body.clientHeight; 30 var vmin = Math.min(vw, vh); 31 var vmax = Math.max(vw, vh); 32 document.body.style.overflow = "auto"; 33 document.getElementById('a').style.width = (50 * vw) + "px"; 34 document.getElementById('b').style.height = (25 * vh) + "px"; 35 document.getElementById('c').style.width = (35 * vmin) + "px"; 36 document.getElementById('d').style.height = (25 * vmax) + "px"; 37 </script>