rem原理

  1. <style>
  2. html{
  3. font-size: 10px;
  4. }
  5. div{
  6. width:5rem;
  7. height: 5rem;
  8. background-color: red;
  9. }
  10. </style>
  11. <div></div>
  12. <script>
  13. /*
  14. iphone6 375px 10rem 1rem=37.5px
  15. iphone6-plus 414px 10rem 1rem=41.4px
  16. iphone5 320px 10rem 1rem =32px;
  17. rem 相对于根元素(html)的font-size而言的
  18. 1rem = 10px;
  19. */
  20. </script>

视配方案

  1. <style>
  2. div{
  3. width:5rem;
  4. height: 1rem;
  5. background-color: red;
  6. }
  7. </style>
  8. <div></div>
  9. <script>
  10. /*
  11. iphone6 375px 10rem 1rem=37.5px
  12. iphone6-plus 414px 10rem 1rem=41.4px
  13. iphone5 320px 10rem 1rem =32px;
  14. rem 相对于根元素(html)的font-size而言的
  15. 1rem = 10px;
  16. */
  17. var html = document.getElementsByTagName("html")[0];
  18. var ww = window.innerWidth;
  19. var rem = ww/10;
  20. html.style.fontSize = rem+"px";
  21. window.onresize = function(){
  22. var ww = window.innerWidth;
  23. html.style.fontSize = ww/10+"px";
  24. }
  25. </script>

单位

你知道的单位有那些
        px  --绝对单位
        rem  --相对单位
        em  --相对于父元素的font-size而言的
        %
        vm   100vw;