unit-vh-vw-overflow-auto-ref-iframe.html 1.2 KB

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