移動端rem匹配

來源:互聯網
上載者:User

標籤:nts   ons   png   tle   適配   alt   單位   技術   bsp   

Rem是相對於根項目font-size大小的單位

記inphone5螢幕寬度是 320px font-size16px 1rem=16px

  1. <html>
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <meta name="viewport" content="width=device-width,initial-scale=1.0">
  5.     <title>smiledemo</title>
  6.     <style>
  7.        .test{
  8.          width: 20rem;
  9.          height: 10rem;
  10.          background-color:bisque;
  11.          text-align: center;
  12.        }
  13.        .hello{
  14.          color: red;
  15.          font-size: 1rem;
  16.        }
  17.     </style>
  18.   </head>
  19.   <body>
  20.     <div id="app"></div>
  21.     <div class="test">
  22.        <p class="hello" >hello wangyawei</p>
  23.     </div>
  24.   </body>
  25. </html>

於是我們在js中寫適配方案3行代碼即可搞定

  1. <script>
  2.      let htmlwidth=document.documentElement.clientWidth||document.body.clientWidth;
  3.      console.log(htmlwidth);
  4.      let htmlDom=document.getElementsByTagName("html")[0];
  5.      //以imphone5的螢幕作為適配 320/16=20
  6.      htmlDom.style.fontSize=htmlwidth/20+"px";
  7.   </script>

可以看出完美適配,font-size變為了18.75px

移動端rem匹配

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.