Mobile-side Layouts

Source: Internet
Author: User

Recently done a project for mobile, in the development encountered a lot of pits, learned a lot, found that there are many places worth writing, for the web to do the mobile peer reference reference.

One, the unit

Speaking of mobile, have to mention adaptation problems, large and small mobile devices not only make Android and iOS sad, because the device size and browser differences, now also let the front end began to headache, but, the method is always more than the problem, we are the revolutionary team, encounter problems will be on!

Because today's mobile devices in the market resolution of different sizes, it is obvious that we commonly used PX units at this time some of the less brilliant, for this, CSS3 out a new unit--rem,rem is a mobile artifact, it perfectly solves the resolution of the adaptation problem.

REM is relative to the root element

In this way, a wide 100px box appears, very simple, found no, some like we used to the EM, but REM is relative to the root element (

Although very simple, but mobile devices so much, how can we according to the phone screen resolution, to set the

There are 2 ways I know:

1) querying via CSS Media

, through the media query method, only need to take the usual screen width into account, to meet most of the application scenarios, but this approach is not rigorous, Virgo, how can you meet? Then use JS to set the FontSize bar!

2) through JS calculation

With the screen resolution of the JS device, you can calculate the size of the corresponding font, this method can be adapted to the size of all screens, which is a lot of perfect.

But sometimes it will be very troublesome, because REM will involve conversion problems, such as 70px width, the root directory font is 12px, the conversion formula is: 70/12 = 5.83333333~, each write a unit to use calculator to calculate, think of tears, but as a program ape, How can people do this kind of thing, the work can let less or sass and other pre-processor to complete.

second, use the viewport layout in the <meta> tag

With the REM unit is not enough, you have to add viewport, with this thing, MA Ma no longer have to worry about my mobile adapter problem, talk not much, direct

Red circle must be, with this <meta> tag, the page has a little meaning.

Three, flexible layout box layout

Previous PC-side layouts typically use float, margin, padding, and so on, which are not only cumbersome to calculate, but also on the mobile side

Error prone. Now mobile, there is a better choice to use the Flexbox layout method. Give me a chestnut: use Flex to tab.

HTML code: CSS Code:

To do an evenly-divided tab, it should be the most common function of flexbox, the implementation principle is very simple, only need to give "parent element", "child element"

respectively, "Display:flex", "Flex:1″ can be." The width of the child element does not change depending on the length of the content. Front-end classmate additions or deletions

tab number, only need to increase or decrease the DOM structure, no modification of the style.

This layout has always been a compatibility issue, so that the front-end students want to use, and do not dare to use, the reason is that it is not clear its various versions of

Write specifications and compatibility. Surprisingly, at present, in addition to opera mobile12, mobile browsers are supported by the old version of Flexbox syntax

, but does not include the Wrap property of Flex. So you can use it boldly without worrying too much.

Iv. Pits

Layout problem is solved, although now seems to be perfect, but there are still a lot of pits waiting for us!

1, the use of REM compatibility is no problem, but there are many problems to note

A) The effect is somewhat biased.

Why does this happen? It was later discovered that this was due to the different processing of decimal numbers by the browser. Different browsers calculate REM turn

For PX values, the processing of values after the decimal point is biased, and the source of the REM calculation bias is the difference between the number types of the browser kernel.

If the browser's kernel number type is float type, it is better to support numeric values with decimal points. When the browser kernel number type is int type,

If the decimal point is not supported, the number is rounded, so there is a bias. The larger the element, the more obvious the deviation! This pit is almost unavoidable,

Only allow him to better adapt to the most browsers. such as the Chroem kernel.

b) Sprite map positioning problem

From the above we can be informed that REM conversion into PX size is not strictly accurate, if the distance between sprite icon is too small, it may lead to the icon over the bounds,

So the gap between the figure and the figure needs to be a little larger.

C) REM units preferably not on PC side

Raise a chestnut directly:

Code:

Look at the code, you should guess, this should be two exactly the same box is right, but the effect is like this

PC Effects:

IPhone6 Effect:

From the effect that the PC-side browser to the REM unit support is not friendly, and finally its reasons, I also know, hope to have the classmate, more sharing,

Therefore, the front-end students try not to put this unit on the PC side to use, so as to avoid and design drawings.

1, the use of flexible box models need to pay attention to the problem.

A) Although the mobile side of the major browsers are supported by the old version of Flexbox syntax, but still have to have a compatibility notation;

b) In the development, I found that when using the elastic box model, if the text is involved in the need to pay attention to, in Li wrote "we" and "our", respectively, is two characters and three words. There will be different widths that lead to non-uniformity, as shown in the CSS above, I set the child element width to 5% (only set the LI is uniform width on the line, not necessarily 5%) can solve the problem.

Summarize:

Speaking of which, the Web mobile layout problem is basically finished, but, learning these is not enough, want to do a good job of web mobile development,

Still need to learn a lot of things (such as performance problems) to make the user Experience Super Good Web page, the revolution has not been successful, comrade still need to work ah.

Mobile-side Layouts

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.