Screen fitting is a very easy to ignore problem, but it is essential for the product to keep improving . For Web developers, it's also a classic question that needs to be understood clearly.
Starting with the public number (Mr. Chew)
(i)
What is the mobile-side fit?
We're talking about web pages that fit into a variety of screen sizes to make the page look the same as the designer's design. Plainly, the same set of code at different resolutions of the mobile phone running, the spacing between page elements, leaving white, and the size of the picture will change , in proportion with the design of the draft.
Give me a chestnut.
Figure 1. 260*400 's screen
Figure 2. 380*400 's screen
The top of the page in different size of the display on the screen, I see no problem, the general engineer will think is OK, so front-end engineers can easily ignore screen adaptation. But for some of the better products, this is not up to the requirements. For example, the following issues are:
- The size of the screen in Figure 1 is small, so the avatar should be smaller, the left side of the topic should be less blank.
- The picture should remain square, and the margins between the two graphs should change with the screen
Understand, so padding,margin, pictures and so on the size of basic should be done to fit
(ii)
What can be done to fit this?
The key is to find a unit of length so that the same value is scaled proportionally to the size of the screen on different sizes.
length unit I only know px ??
I'm going to push the details in the next installment about viewport and CSS pixels px, and this article focuses only on how to fit, so let's start with the conclusion:
- Web page layout in viewport, viewport is divided into small squares, a css pixel for a block
- In the case where the width of the viewport is equal to the width of the device, the physical size of the screen is as large as 1 css pixels on a screen of different sizes by an algorithm.
Since 1 css pixels are as large as the physical size on different screens, the PX must not be used as a fitting method.
The length unit rem is calculated relative to the font-size of the HTML tag. For example, the HTML tag is set font-size:36px, and the DIV is set Width:1.2rem. So the width of this div is 1.2rem=36px*1.2=43.2px .
As the above example, if the page is loaded, using JS to dynamically set the font-size of the HTML tag according to the size of the screen, with the value of the HTML tag font-size change, div 1.2rem conversion to PX value also changes, This enables the DIV to change with the size of the screen, and the CSS code is always width:1.2rem. And so on all elements of the page.
Understand, the mystery lies in the combination of the fixed size of PX and the relative size of REM!
(iii)
Mr. Chew, the principle I understand, you have to implement the specific plan ?? ?
The industry is more popular practice (refer to Ali's flexible), the following points:
- Set viewport to device width (not necessarily here, but this is the way to go)
- Divide the viewport into 10rem, and calculate 1rem in the current browser pixel value, give it to the HTML tag font-size (divided into 10rem just for the convenience of calculation)
- When you write CSS code, you encounter the place to adapt, such as width,margin,padding, and so on, do not use PX, changed to REM
JS and HTML code are as follows:
The CSS code makes a change like this:
The results are as follows: Margins and avatar images change with the screen
Four
The amount of code is very small, is to understand digestion. There is a problem, designers to the design of the size units are used px, but just said the 3rd step, write CSS when using REM, how to convert this? It's going to be troublesome
Existing designers to provide a width of 400px design, one of the width of a picture is designed to 20px, then, CSS is img{width:0.05rem;}, how to get this result?
- The width of the design is the same as the width of the phone, assuming that there is a viewport 400px mobile phone
- Divide it into 10rem, each rem is 40px;
- Then the picture width 20px is naturally 0.5rem;
Wait, it doesn't mean that every time I write to the size of the place, I have to first on the draft paper to convert the designer px to REM? Don't worry, sublime text 3 has a plugin to help you with this conversion, you only need to input 20px, it will automatically help you convert to 0.5rem, to see the second understand
Reference
- Use flexible to achieve the end of H5 page adaptation (HTTPS://GITHUB.COM/AMFE/ARTICLE/ISSUES/17)
- Mobile-high-definition, multi-screen adaptation solution (http://www.html-js.com/article/Mobile-terminal-H5-mobile-terminal-HD-multi-screen-adaptation-scheme%203041)
- A Tale of Viewports (http://www.quirksmode.org/mobile/viewports.html)
Mobile adaptation This will be written as a small series, the next time the write browser is how to calculate the CSS pixel, it's different on the screen.
Chew Mr. start code Word to know the code word is not easy, adhere to code word more difficult. Reprint please declare the source
"Mobile adaptation" mobile web How to do screen adaptation (i)