Which unit is better to use for mobile layout?

Source: Internet
Author: User

On the mobile side of the layout using which units, everyone has their own habit of using the unit, this is just my personal little humble opinion.

You are welcome to advise.

Many people feel annoyed when it comes to writing mobile pages. Because you want to write n sets of CSS styles. Then use the media query to do the matching.

Generally this time I have said that I am ignorant of the DA, do not understand why to write so many sets. May be because I have not enough experience, so I can not see how to write so many sets of what the meaning.

I think that on the mobile side can be adapted to different phone resolutions, if only to maintain overall scale, no design differences can not need to use the ' media query '.

Let's talk about the units I used to use:em,rem,%

1.em

EM is a relative value, and it is a value relative to the parent element, so when using EM, you need to know what the font-size of the parent element is.

The EM formula is as follows :

1÷ the Font-sizex of the parent element needs to convert the pixel value = em value

The first time I use EM, because it is not clear that it is relative to the value of the parent element, the page written out is really cute.

When using EM, I used JS to do some matching.

The default resolution for different phone browsers is not the same, so you need to calculate the ratio of the actual screen width of the browser to the width of the design, and then calculate the font-size of the body based on that scale.

Just write a set of CSS styles that are appropriate to display on different screen sizes.

SW is the width of the design chart, assuming that the design of the iphone 6 design of the high-definition design, the width of the plan is 750;

var w = $ (window);
function auto () {
< span>
var auto1 = (parseint ( W.width ())/SW);
var Auto3 = auto1 * 62.5 +"% ";
$ (" body "). CSS (" Font-size ", Auto3);
};

Note: When using EM, be sure to note the size of the font-size of its parent element!

The important thing to say three times:em is relative to the parent! EM is relative to the parent! EM is relative to the parent!!

2.rem

REM is a value relative to the root element

Let's look at a simple code example:

html {font-size: Span class= "number" > 62.5%; /*10÷16x100% = 62.5%*/}body {font-size: 1.4rem; /*1.4x10px = 14px */}h1 {font-size: 2.4rem; /*2.4x10px = 24px*/           

I have defined a basic font size of 62.5% (that is, 10px) in root element

After learning about the REM unit, we can make a mobile layout for the features of this unit.

① setting the size of the page rem

{font-size: calc (100vw/3.75)}  

100VW is the width of the device, divided by 3.75, to make the 1rem size equal to 100px at IPhone6.

② Conversion Units

In the layout, direct conversion, the ' px ' is replaced with ' REM '. Join the 10px on the design map, you can Ctrip 0.1rem. Thus, under iphone 6, the dimensions of all elements are the same as the size of the visual manuscript, while on other handheld devices, the value of the 100vw/3.75 changes proportionally as the width of the handheld device changes.

This makes it possible to maintain visual consistency for devices of any resolution.

③VW Unit compatibility

Low-version handheld devices may not support VW units and need to use JS processing.

Document.documentElement.style.fontSize = window.innerwidth/3.75+ ' px '

In practical use, window.innerwidth on individual Android phones is the screen resolution width, not the device width. Use this method for child shoes to pay attention to the test.

3. Percent%

Width/visual width of elements on visual artwork = Percentage of the width of the element displayed on the device.

This way, the handheld devices in different resolutions can be kept consistent with the visual version.

Which unit is better to use for mobile layout?

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.