REM-Match on mobile

Source: Internet
Author: User

REM is a unit of font-size size relative to the root element

Note inphone5 screen width is 320px font-size16px 1rem=16px

  1. <meta charset= "utf-8" >
  2. <meta Name= "viewport" content= "width=device-width,initial-scale=1.0" >
  3. <title>smiledemo</title>
  4. <style>
  5. . test{
  6. Width:20rem;
  7. Height:10rem;
  8. Background-color:bisque;
  9. Text-align:center;
  10. }
  11. . hello{
  12. color:red;
  13. Font-size:1rem;
  14. }
  15. </style>
  16. <body>
  17. <div id= "app" ></div>
  18. <div Class= "Test" >
  19. <p Class= "Hello" >hello wangyawei</p>
  20. </div>
  21. </body>

So we write the formula in JS 3 lines of code can be done

    1. <script>
    2. Let htmlwidth=document. documentelement.clientwidth| | document. Body.clientwidth;
    3. Console.log (Htmlwidth);
    4. Let htmldom=document. getElementsByTagName ("html") [0];
    5. //With Imphone5 screen as a fit 320/16=20
    6. htmldom.style.fontsize=htmlwidth/20+ "px";
    7. </script>

You can see the perfect fit, and the font-size becomes 18.75px

REM-Match on mobile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.