Mobile-adapted REM notes

Source: Internet
Author: User

/*Mobile Adapter Width|height|font-size = The amount of visual manuscript value/100rem; @lbl*/@media screen and (Max-WIDTH:359PX) and (min-width:320px) {html,body{font-size:50px!important; }} @media screen and (Max-WIDTH:374PX) and (min-width:360px) {html,body{font-size:56.25px!important; }} @media screen and (Max-WIDTH:413PX) and (min-width:375px) {html,body{font-size:58.5px!important; }} @media screen and (Max-WIDTH:639PX) and (min-width:414px) {html,body{font-size:64.6px!important; }} @media screen and (min-width:640px) {html,body{font-size:100px!important; }}

REM, as its name implies, is the root element, which is a relative unit, and the difference between em is that REM is relative to the font-size,em of the root node HTML relative to the parent element font-size. Using REM is actually using media query to modify the font-size of HTML, then the size of the elements using REM will change accordingly, so we just need to write a copy of the CSS to be able to fit all the phone's screen. Although REM is not accurate under some browsers, it is only a handful, and if you need a very precise fit, you can use JS to calculate the font-size of the HTML.

Many developers hear that the first response to adaptation is how much the phone's screen width is, how much physical pixels are, and then considering adaptation. This is from the point of view of matching, take the visual manuscript to fit the mobile phone. Can't we just leave the equipment behind and start with the visual manuscript? Then let's take a look.

The designer's visual manuscripts are generally designed according to the 640px. When we get the visual manuscript, we first convert the width of the visual manuscript to REM, and we don't need to consider the resolution of the device, just by calculating the formula for the width of the visual manuscript = Font-size x rem. The width of the visual manuscript is given, we take 640px for example, Font-size and REM are two variables, we only need their product equals 640. We use a change-one approach, assuming rem=6.4, then the initial value font-size must be equal to 100px, we can adjust the width of the screen as needed to scale font-size. So if our 640px visual manuscript needs to be shown as 320px, we just need to reduce the font-size by half. What other 375px, 414px, all kinds of screen, just need to scale and so on. Like what:

320px:font-size = 320/640 * = 50px;360px:font-size = 360/640 * =25px, ..... 800px:font-size = 800/640 * = 125px;.

In fact, according to the proportion of the font-size, I have to take the decimal point down, so that the width of the element will not exceed the total width. Just if our visual manuscript width to 640px, but need to be more than 640 device under the display, then there will be blurred images and so on, so we generally use large visual manuscript to fit the small screen. For example, you need to match 640px large screen, you can let the designer according to the width of 960px to design, then convert to 9.6rem, the initial value font-size=100px, and then adjust the size of the font-size according to the width of the screen to fit.

If you want more accurate adaptation, you can use the JS dynamic calculation of the width of the window to change the size of the font-size, Ali hand scouring the use of flexible is also the way, the following is down to a flexible simplification

(function(Win) {varRemcalc = {}; varDocel =win.document.documentElement, Tid; functionRefreshrem () {//gets the width of the current window        varwidth =docel.getboundingclientrect (). width; //greater than 640px Press 640 to calculate        if(Width > 640) {width = 640 }        //the width of the window is fixed into 10 parts, which is 10rem.        //according to the visual manuscript 640 640/10=64px so 1rem = 64px        //640 Visual 80px*80px button conversion to rem 80/64 = 1.25rem        //the width and height of the button is fixed at 1.25rem * 1.25rem        //when the window width is scaled to 320px        //so 1rem = 32px        //The original 80px*80px button now changes to 1.25rem * 32px = 40px        //button changes to 40px * 40px        //other widths are similar        //         //The CMS approach is similar        //We just fixed the window width into 6.4 parts, i.e. 6.4rem        //so 1rem = 100px        //640 Visual 80px*80px button conversion to rem 80/100 = 0.8rem        // .... The rest is similar .        //         //         //Compare        //In fact, it's the problem of calculating REM, the number of visual manuscripts, except 64 or 100.        //Besides 100 is always better than 64 good mental arithmetic        //even with sass, write a @function px2rem instead of mental arithmetic .        //. 8rem total than input px2rem (80) Few characters        //         //         varrem = WIDTH/10;//CMS Just change this line to var rem = width/640 *DocEl.style.fontSize = rem + "px"; Remcalc.rem=REM; //error, Compatibility handling        varActualSize = parsefloat (window.getComputedStyle (document.documentelement) ["Font-size"]); if(ActualSize!== rem && actualsize > 0 && math.abs (actualsize-rem) > 1) {            varremscaled = rem * REM/ActualSize; DocEl.style.fontSize= remscaled + "px"        }    }    //function throttling to avoid frequent updates    functionDbcrefresh () {cleartimeout (TID); Tid= SetTimeout (Refreshrem, 100)    }    //window Update dynamic change font-sizeWin.addeventlistener ("Resize",function() {Dbcrefresh ()},false); //when the page is displayed, the window size will change after the window is switched?....Win.addeventlistener ("Pageshow",function(e) {if(e.persisted) {Dbcrefresh ()}},false);    Refreshrem (); Remcalc.refreshrem=Refreshrem; remcalc.rem2px=function(d) {varval = parsefloat (d) * This. rem; if(typeofD = = = "string" && D.match (/rem$/)) {val + = "px" }        returnVal}; Remcalc.px2rem=function(d) {varval = parsefloat (d)/ This. rem; if(typeofD = = = "string" && D.match (/px$/)) {val + = "REM" }        returnVal}; Win.remcalc=Remcalc}) (window);//Koala Mobile Full station using REM is not a lot of//mainly used 100% layouts such as width, height, padding//There are also direct with PX font size using px fixed unchanged//There is also the use of EM based on the font-size fixed line-height//full station element using Box-sizing:border-box//Multi-line ellipsis text-overflow:ellipsis;-webkit-line-clamp:2;//Non-Main page many are pictures affect SEO see very short of the front end//use REM best text to specify font size or inherit HTML font-size

This essay is excerpted from : http://www.jianshu.com/p/dfa33d3be23c

Mobile-adapted REM notes

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.