移動端rem匹配
阿新 • • 發佈:2018-04-22
nts script ldo center sca mile font text app
Rem是相對於根元素font-size大小的單位
記inphone5屏幕寬度是 320px font-size16px 1rem=16px
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1.0">
- <title>smiledemo</title>
- <style>
- .test{
- width: 20rem;
- height: 10rem;
- background-color:bisque;
- text-align: center;
- }
- .hello{
- color: red;
- font-size: 1rem;
- }
- </style>
- </head>
- <body>
- <div id="app"></div>
- <div class="test">
- <p class="hello
- </div>
- </body>
- </html>
於是我們在js中寫適配方案3行代碼即可搞定
- <script>
- let htmlwidth=document.documentElement.clientWidth||document.body.clientWidth;
- console.log(htmlwidth);
- let htmlDom=document.getElementsByTagName("html")[0];
- //以imphone5的屏幕作為適配 320/16=20
- htmlDom.style.fontSize=htmlwidth/20+"px";
- </script>
可以看出完美適配,font-size變為了18.75px
移動端rem匹配