How to set the rpx unit in a small program on different mobile devices

Source: Internet
Author: User
This article explains how to set rpx units for applets on different mobile devices. when developing applets, when writing a style sheet, you often find that the effect of using PX is not ideal. In daily development, we often use rem and em as the Pixel units for responsive layout. they are all relative units. Rem is relative to the root element of the document, and em is relative to the parent element. But in the official documents of the applet, rpx is used as the responsive layout unit! So what is RPX? How should I set it? Let's take a look at it today.

You can use the @ import statement to import an external style table. @ import is followed by the relative path of the external style table to be imported.

@import "common.wxss";  @import "temp/loadBottomTemp/loadBottomTemp.wxss";  

3. inline style
You can use style and class attributes to control the style of a widget.
(1) style: static styles are uniformly written to the class. The style receives dynamic styles and is parsed during runtime. try to avoid writing static styles into the style to avoid affecting the rendering speed.

// Dynamic style
 Style = "width: {imageWidth} rpx; height: {imageHeight} rpx"; // static style = "color: #1083E5; font-size: 48rpx; font-weight: bold ;"

(2) class: This class is used to specify a style rule. Its attribute value is a set of class selector names (style class names) in the style rule. style class names do not need to be included ., style class names are separated by spaces.

  class="container-row buydes-center-des-select"  

4. Selector
Currently, the following selectors are supported:

2. common settings of mobile HTML5 development META

  
  
  
   

The First meta tag indicates that the width of the document (viewport width) must be kept at with the width of the device. The maximum width ratio of the document is 1.0, and users are not allowed to click on the screen to zoom in;
Width-viewport width height-viewport height [device-width (physical pixel width of the device) | pixel_value] pixel_value is the specific pixel value.
Case:

 
 

Initial-scale-initial scaling ratio
Minimum-scale-minimum scale allowed by users
Maximum-scale-maximum scale allowed by users
User-scalable-can users manually scale the file? some of the information here is written as none, and others are written as 0.
The second meta tag is the safari private meta tag in the iphone device. it indicates that full-screen viewing is allowed;
The third meta tag is also a private tag of the iphone. it specifies the pattern of the status bar at the top of safari in the iphone;
The color of the status bar (the top bar of the screen) in the web app;
The default value is default (white), which can be set to black (black) and black-translucent (gray translucent ).
Note: If the value is "black-translucent", it will occupy the page px position and float above the page (it will overwrite the page 20px height-Retina screen of iphone4 and itouc4is 40px ).
The fourth meta tag indicates that the device ignores the need to recognize numbers on the page as phone numbers.

HTML5 META tag common settings references click open link click Open link

3. device physical pixel (device pixel), device logic pixel (device independent pixel), code CSS pixel, device pixel ratio
Device physical resolution (pixels): The Physical resolution is also called the device pixel. the physical resolution is the original resolution of the image displayed on the LED display.

Device independent pixels

Code CSS pixel: CSS pixel is the concept of Web programming. it is independent of the device's unit for measuring pixels logically. that is to say, the CSS pixel units we use when making webpages are abstract, instead of actually existing

Iphone 6 (device pixel ratio is 2 ):
Device physical pixel (device pixel): 750x1334
Device Logic pixel (device independent pixel): 375x667
Code CSS pixels: 375x667
Device-width (physical pixel width of the device): 375 (1 css px = 2 physical pixels of the device)

** Case: ** the physical pixel of the iphone 6 s is 750x1334, and the window is in JS. innerWidth is used to obtain the physical pixel of the device. what if the value obtained by innerWidth is 375 instead of 750?
Because window. the value of innerWidth is represented by CSS pixels, while the pixel ratio of the iphone 6 S device is 2, 1 css px = 2 physical pixels, so the window. the value obtained by innerWidth is 375px instead of 750px (750px = 375px * device pixel ratio)

On a 1-rate screen: 1 css px = 1 physical pixel of the device
On a 2-rate screen: 1 css px = 2 physical pixels
Device pixel ratio = device pixel/device independent pixel (physical pixel/logical pixel)
Actual image pixels = logical pixel/device pixel ratio

1x: 1pt = 1dp = 1px (mdpi, iPhone 3gs) 2X: 1pt = 1dp = 2px (xhdpi, iPhone 4S/5/6) 3x: 1pt = 1dp = 3px (xxhdpi, iPhone 6 plus)

Take the iPhone 5s as an example. the screen resolution is 640 × 1136, and the magnification is 2. The browser determines that the screen resolution is 320x568, which is still the size of the reference rate.
Therefore, when creating a page, you only need to follow the reference rate. Regardless of the screen and magnification, pages are designed and developed based on the logical pixel size.
Only when preparing a resource diagram, you need to prepare a diagram of 2 times the size and scale it to 1 time by code to ensure clarity.

The above describes how to set the rpx unit for applets on different mobile devices. For more information, see other related articles in the first PHP community!

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.