Use REM to develop your mobile website

Source: Internet
Author: User

What is REM?

  1) The unit of measurement element size in CSS3, similar to PX, PT, EM.

  2) A method of calculating the relative root element font-size. 1rem =

  3) Use the scene: the width of each element, text size, line spacing, fillers and so on all can use the length of the unit.

4) Use example:

HTML{font-size:37.5px;}/*The base font-size of the root element is set to 37.5px, then 1rem=37.5px*/#box1{width:10rem;Height:2rem;font-size:0.5rem;}/*box1 width 10rem is calculated as width 375px, height is 75px, text size is 18.25px;*/#box1. Title{width:2rem;}/*the width of title is still relative to the root element, and the result is 75px*/

 Adaptation of REM on the mobile side

As can be seen from the above example, using REM to layout the site, we only need to modify the root element of the font-size size, the entire page elements can be calculated and adapted to scale as expected .

For example, in the above example, we change the HTML font-size to 32px, then the box1 style becomes 320px wide, high 64px, and the text size becomes 16px;

  That is, in the development process, we simply use REM to lay out a device based on one width. Then use the JS or CSS media to change the base font-size of the HTML based on the width. Can be adapted to a variety of mobile devices.

  

The steps are as follows:

    1) Suppose we get a 750px width design. and ready to debug on the IPhone6, iphone6 screen width of 375px, their proportion is 2:1;

Initial process:

Set <meta content= "Width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" name= "viewport"/ >

Set html{font-size:37.5px}  

          Why is it set to 37.5px? You can also set the value to another. But for ease of conversion and memory, we default to each screen setting his base font-size= (screen width/10px);

The development process: sets the unit of each element to use REM (any place that originally uses px,em,pt), the quantity (actual px/2/37.5 of the elements on the design diagram) REM;

...

Well, our page has been developed and completed.

    2) At this time, our page, under the IPhone6, has been duly completed. So how to compatible with other mobile devices of different widths?

Idea: Change the CSS property of the root element of each device of different widths so that its font-size value is the device width/10px.

Scenario 1: Insert a section of JS at the head of the page. //Why is the head? --Prevent page Flicker

(function() {    varfunction() {        var html =  document.documentelement;         = Html.getboundingclientrect (). WIDTH/10 + ' px ';    };    Window.addeventlistener (false);    Newrem ();}) ();

Scenario 2: Adding media queries to the CSS file

@media (min-width:320px){html{font-size:32px;}} @media (min-width:360px){html{font-size:36px;}} @media (min-width:375px){html{font-size:37.5px;}} @media (min-width:384px){html{font-size:38.5px;}} @media (min-width:414px){html{font-size:41.4px;}} @media (min-width:448px){html{font-size:44.8px;}} @media (min-width:480px){html{font-size:48px;}} @media (min-width:512px){html{font-size:51.2px;}} @media (min-width:544px){html{font-size:54.4px;}} @media (min-width:576px){html{font-size:57.6px;}} @media (min-width:608px){html{font-size:60.8px;}} @media (min-width:640px){html{font-size:64px;}} @media (min-width:750px){html{font-size:75px;}} @media (min-width:800px){html{font-size:80px;}} @media (min-width:1024px){html{font-size:102.4px;} }

What kind of plan is better?

CSS fits most of the mainstream screens and is lightweight and does not need to be calculated.

JS based on the specific screen width of the precise calculation, can be adapted to all the equipment,

You still need to choose according to your own usage scenarios.

What is the compatibility of REM?

Reference:

Can see, a piece of floating green, the results gratifying.

Some of the benefits of REM adaptation include:

1)   the relative change of the viewport,rem is not deformed, not blurred.

  2) relative to Em,rem is not relative to the parent, so it does not cause confusion.

  3) relative to PX, no need to say more.

  4) relative to the various media query adaptive, REM mode can be changed one-click, save countless code, and maintenance convenience.

Some of the non-applicable scenes of REM

  1) want to use on ipad or PC or Super large screen device. (Good freshman words && pictures and videos are blurred).

  2) can't think of it.

In summary, REM can be in the mobile development process to bring powerful convenience, he may also have some shortcomings, but the flaw does not cover jade, far from its many advantages of the light coverage, will be the future trend.

Use REM to develop your mobile website

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.