rem原理
<style> html{ font-size: 10px; } div{ width:5rem; height: 5rem; background-color: red; } </style> <div></div> <script> /* iphone6 375px 10rem 1rem=37.5px iphone6-plus 414px 10rem 1rem=41.4px iphone5 320px 10rem 1rem =32px; rem 相对于根元素(html)的font-size而言的 1rem = 10px; */ </script>
视配方案
<style> div{ width:5rem; height: 1rem; background-color: red; } </style> <div></div> <script> /* iphone6 375px 10rem 1rem=37.5px iphone6-plus 414px 10rem 1rem=41.4px iphone5 320px 10rem 1rem =32px; rem 相对于根元素(html)的font-size而言的 1rem = 10px; */ var html = document.getElementsByTagName("html")[0]; var ww = window.innerWidth; var rem = ww/10; html.style.fontSize = rem+"px"; window.onresize = function(){ var ww = window.innerWidth; html.style.fontSize = ww/10+"px"; } </script>
单位
你知道的单位有那些
px --绝对单位
rem --相对单位
em --相对于父元素的font-size而言的
%
vm 100vw;