REM for mobile-side adaptation

Source: Internet
Author: User

Mobile-side adaptation
    • Web page running on mobile (H5 page)
    • Cross-platform
    • Based on WebView ()
    • Based on WebKit
Common suitable formula method
    • PC-side with Display:inline-block, let div box horizontal row
    • Mobile Web: Using fixed height, width percentage,flex Flexible layout , media query medium queries ;

    • Media Enquiry
      Combine CSS with media type (screen, print) and media parameters (Max-width)

         @media screen and (max-width: 320px){        /* css在屏幕跨度<=320px时这段样式生效 */        .inner{            float: left;        }    }    @media screen and (min-width: 321px){        .inner{//        }    }

The above implements a simple horizontal and vertical response

    • REM principle and introduction
      REM is a font unit, similar to PX,
      difference: Depending on the size of the HTML root element, it can also be used as a unit of height and width.
      Adaptation Principle : Dynamically modify the font-size adaptation of the HTML, REM is the size of the font-size of the HTML root element set by different screen sizes to achieve adaptation.

      /* +_media实现*/@media screen and (max-width: 360px) and (min-width: 321px){    html{        font-size: 20px;    }}@media screen and (max-width: 320px){    html{        font-size: 24px;    }}

The Font-size is dynamically set via JS:

//获取视窗宽度    let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;        console.log(htmlWidth);    let htmlDom = document.getElementsByTagName(‘html‘)[0];        htmlDom.style.fontSize = htmlWidth/10 + ‘px‘;
REM Advanced
    • REM Reference value calculation

       1rem = 16px
    • REM Numerical calculation and construction

       170/16 = 170px
    • REM combined with SCSS (Node-sass installation)
      You can also install Sass directly (Installation tutorial http://www.cnblogs.com/yehui-mmd/p/8035170.html)
    • REM Fit Combat
      Adaptation by listening to the browser viewport

      let htmlDom = document.getElementsByTagName(‘html‘)[0];window.addEventListener(‘resize‘, (e)=>{    //获取视窗宽度    let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;    htmlDom.style.fontSize = htmlWidth/10 + ‘px‘;})

Define REM adaptation functions and use (SASS syntax)

  @function px2rem($px) {      $rem:37.5px;//iphone6      @return ($px / $rem) + rem;  }  .header{      width:100%;      height: px2rem(40px);      background-color: red;      padding-left: px2rem(23px);   }

REM for mobile-side adaptation

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.