What effect do you want to achieve?
In different sizes of mobile devices, the page should be able to display well, will not appear dislocation deformation (adaptive), or maintain a uniform effect of the ratio of scaling.
Design Draft size
The first question is how much design size to choose, if according to 320px Design manuscript cut, under the retina screen, the picture will be distorted. As an example of Ipone4 (320*480) on the Retina screen, the actual number of pixels is 640*960.
Put a picture to help understand
As shown, the physical pixels corresponding to the 1 CSS pixels in the normal screen (left) and the Retina screen (right) are inconsistent.
Under normal screen, 1 css pixels correspond to 1 physical pixels (1:1)
Under the Retina screen, 1 css pixels correspond to 4 physical pixels (1:4)
In theory, a pixel on a picture corresponds to a physical pixel, and the picture is perfectly displayed.
There is no problem on the normal screen, but there is not enough pixel dots on the retina screen to blur the picture.
Put a picture to help understand
For example, for the retina screen of dpr=2, 1 bit pixels correspond to 4 physical pixel, because the single bit image can not be further segmented, so only the nearest color, resulting in blurred images (note the above several color values).
So, for picture distortion, the solution is to use twice times the size of the picture
But what happens if you use a twice-fold picture on a normal screen?
As shown on the left, 1 physical pixels correspond to 4 image pixels, resulting in chromatic aberration.
Comprehensive: Processing pictures in HD screen 1 time times the size of the image distortion and normal screen twice times the picture chromatic aberration, should prepare two sets of pictures, through the media query or to introduce the corresponding image URL
Under Retina, border:1px problem
Since the 1 CSS pixels on the retina screen correspond to 2 physical pixels, that is, its minimum display unit is 0.5 css pixels, if we set the following CSS
border:1px solid #000;
The effect on the normal screen and the retina screen is like this
That is, we cannot write a border style that occupies only one physical pixel because setting 0.5px is considered 0px in some systems.
The solution on the Internet is
.scale { position : relative ;} .scale:after { content : ; position : absolute ; bottom : 0px ; left : 0px ; right : 0px ; Border-bottom : 1px solid #ddd ; -webkit-transform : scaley (. 5) ; -webkit-transform-origin : 0 0 ;}
Use : after to offset the other 0.5 pixels, this hack is not enough to handle round corners and so on.
Another option is to add the following meta tags to the dpr=2 screen.
<meta name= "viewport" content= "width=640,initial-scale=0.5,maximum-scale=0.5, minimum-scale=0.5,user-scalable= No ">
Set the initial zoom ratio of viewport to 0.5 so that the entire 1px border of the page becomes 0.5px, for all scenarios
But scaling the entire page also poses some problems:
1, font size will also be scaled, in the normal screen and HD screen can not display the same
2, the page layout will also be scaled
Fit layout
There are many kinds of layout options on the Web, and here only the consensus is that the better solution, REM layout
Principle: According to different mobile phone size and DPR dynamic change the font-size size of root node
Direct Sticker Code
varDocel =document.documentelement, Metael= Document.queryselector (' meta[name= "viewport"] '), Styleel= document.createelement (' style ')), DPR= Win.devicepixelratio | | 1, REM= Docel.clientwidth * DPR/10, Scale= 1/DPR;//dynamic refers to the scale of the viewportMetael.setattribute (' content ', ' width=device-width, initial-scale= ' + scale + ', maximum-scale= ' + scale + ', minimum-scal E= ' + scale + ', User-scalable=no ');//set the DATA-DPR property for use as a CSS hackDocel.setattribute (' DATA-DPR ', DPR);//Dynamic Write StyleDocEl.firstElementChild.appendChild (styleel); styleel.innerhtml= ' html{font-size: ' + rem + ' px!important;} ';
Calculate REM Description
1. Multiply DPR to circumvent the effect of scaling on layout and font size
2. Divided by 16, I do not know, it is said that in order to convert a smaller reference value can also be set to 10, or any value you want to set
The font-size of HTML by calculation is:
IPHONE3GS:320PX/10 = 32px
IPHONE4/5:320px * 2/10 = 64px
iphone6:375px * 2/10 = 75px
How can I restore the true width of a visual manuscript in CSS encoding?
For designs with a width of 640px, if a div's width height is 300*320px how to convert to REM
Package a mixin with a sass
For example Torem (height) $ppr: 640PX/10 * 1REM; Pixel per rem
@mixin Torem ($property, $values ...) { $max: Length ($values); $remValues: "; @for $i from 1 through $max { $value: nth ($values, $i)/$PPR; $remValues: #{$remValues + $value}; @if $i < $max{ $remValues: #{$remValues + ""}; } } #{$property}: $remValues;}
So for a div with a wide height of 300*320px, you can write this
@include torem (width, 300px); @include torem (height, 320px);
The resulting CSS code is
Width:4.6875rem;height:5rem;
Font size Issues
Different DPR will cause different scaling, the font will also be scaled, because the font can not use REM, so you need to apply different font sizes to different DPR screens, or to encapsulate a mixin, I do not know how to encapsulate, and so I know how to fill ^_^
Reference website: Mobile HD, multi-screen adaptation, mobile page with REM layout
Getting started with mobile--adaptation