2_em&rem.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./rem.js"></script>
  8. <style>
  9. html{
  10. font-size: 10px;
  11. }
  12. .div1{
  13. font-size: 15px;
  14. }
  15. /* .box { */
  16. /* font-size: 30px; */
  17. /* em 他是一个相对单位 em 相对于font-size进行调整 */
  18. /* 它先去自己内部找font-size如有那么就乘以当前font-size的值 */
  19. /* 如果自己没有font-size那么就往上找 */
  20. /* width: 20em;
  21. height: 20em;
  22. border: 1px solid black; */
  23. /* } */
  24. .box{
  25. /* rem 也是相对单位 它是相对于html的font-size */
  26. width: 2rem;
  27. height: 2rem;
  28. border:1px solid black;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="div1">
  34. <div class="box">
  35. hello world
  36. </div>
  37. </div>
  38. <script>
  39. // window.onpageshow = function(){
  40. // var htmlWidth = document.documentElement.getBoundingClientRect();
  41. // setTimeout(function(){
  42. // document.documentElement.style.fontSize = htmlWidth/7.5+"px";
  43. // console.log(document.documentElement.style.fontSize)
  44. // },1000)
  45. // }
  46. // window.onresize = function(){
  47. // var htmlWidth = document.documentElement.getBoundingClientRect();
  48. // document.documentElement.style.fontSize = htmlWidth/7.5+"px";
  49. // }
  50. </script>
  51. </body>
  52. </html>