REM is a unit of font-size size relative to the root element
Note inphone5 screen width is 320px font-size16px 1rem=16px
- <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>
- <body>
- <div id= "app" ></div>
- <div Class= "Test" >
- <p Class= "Hello" >hello wangyawei</p>
- </div>
- </body>
So we write the formula in JS 3 lines of code can be done
- <script>
- Let htmlwidth=document. documentelement.clientwidth| | document. Body.clientwidth;
- Console.log (Htmlwidth);
- Let htmldom=document. getElementsByTagName ("html") [0];
- //With Imphone5 screen as a fit 320/16=20
- htmldom.style.fontsize=htmlwidth/20+ "px";
- </script>
You can see the perfect fit, and the font-size becomes 18.75px
REM-Match on mobile