1# Media Query
2
3
4Media queries are widely used on mobile devices. You can use them to modify the application style based on the device type or specific features and device parameters (such as the screen resolution). Specifically, media queries allow you to:
5
61. Design a layout style based on the device and app attributes.
7
82. Update the page layout to adapt to dynamic screen changes (for example, screen splitting or switching between landscape and portrait modes).
9
10
11> **NOTE**
12>
13> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
14>
15> The **media** attribute uses the actual size, physical pixel, and screen resolution of the device by default. Do not confuse them with the configuration based on basic screen width 720 px.
16
17
18## CSS Syntax Rules
19
20Use **\@media** to import query statements. The rule is as follows:
21
22
23```css
24@media [media-type] [and|not|only] [(media-feature)] {
25  CSS-Code;
26}
27```
28
29Example:
30
31\@media screen and (round-screen: true) { … } // The condition is met when the device screen is round.
32
33\@media (max-height: 800) { … } // Range query. CSS level 3 is used.
34
35\@media (height <= 800) { ... } // Range query. CSS level 4 is used, and the statement is equivalent to that of CSS level 3.
36
37\@media screen and (device-type: tv) or (resolution < 2) { … } // Multi-condition query that contains the media type and multiple media features.
38
39
40## Referencing Resources on a Page
41
42Use **@import** to import a media query. The rule is as follows:
43
44
45```
46@import url [media-type] [and|not|only] [(media-feature)];
47```
48
49Example:
50
51
52```
53@import '../common/style.css' screen and (min-width: 600) and (max-width: 1200);
54```
55
56
57## Media Type
58
59| Type| Description|
60| -------- | -------- |
61| screen | Media query based on screen-related parameters.|
62
63
64## Media Logical Operation
65
66Media logical operators (and, or, not, and only) are used to implement complex media query. They can also be combined using comma (,). The following table describes the operators.
67
68  **Table 1** Media logical operators
69
70| Type| Description|
71| -------- | -------- |
72| and | The **and** operator is used to combine multiple media features into one media query, in a logical AND operation. The query is valid only when all media features are true. It can also combine media types and media functions.<br>For example, **screen and (device-type: wearable) and (max-height: 600)** indicates that the query is valid when the device type is wearable and the maximum height of the application is 600 pixel units.|
73| not | The **not** operator is used to perform a logical negation for a media query. **true** is returned if the query condition is not met. Otherwise, **false** is returned. In a media query list, logical negation is performed only for the media query using the **not** operator.<br>For example, **not screen and (min-height: 50) and (max-height: 600)** indicates that the query is valid when the height of the application is less than 50 pixel units or greater than 600 pixel units.<br>You must specify the media type when using the **not** operator.|
74| only | The **only** operator applies the selected style only when the entire expression is matched. It can be used to prevent ambiguity on browsers of earlier versions. The statements that contain both media types and media features produce ambiguity when they are received by some browsers of earlier versions. For example:<br>screen and (min-height: 50)<br>The browsers of earlier versions would mislead this sentence into screen, causing the fact that the specified style is applied when only the media type is matched. In this case, the **only** operator can be used to avoid this problem.<br>You must specify the media type when using the **only** operator.|
75| ,(comma) | The **or** operator is used to combine multiple media features into one media query, in a logical OR operation. The query is valid if a media feature is true. The effect of a comma operator is equivalent to that of the **or** operator.<br>For example, **screen and (min-height: 1000),  (round-screen: true)** indicates that the query is valid when the minimum height of the application is 1000 pixel units or the device screen is round.|
76| or | The **or** operator is used to combine multiple media features into one media query, in a logical OR operation. The query is valid if a media feature is true.<br>For example, **screen and (max-height: 1000) or  (round-screen: true)** indicates that the query is valid when the maximum height of the application is 1000 pixel units or the device screen is round.|
77
78At MediaQuery Level 4, range query is imported so that you can use the operators including &lt;=, &gt;=, &lt;, and &gt; besides the max- and min-operators.
79
80  **Table 2** Logical operators for range query
81
82| Type| Description|
83| -------- | -------- |
84| &lt;= | Less than or equal to, for example, **screen and (height &lt;= 50)**.|
85| &gt;= | Greater than or equal to, for example, **screen and (height &gt;= 600)**.|
86| &lt; | Less than, for example, **screen and (height &lt; 50)**.|
87| &gt; | Greater than, for example, **screen and (height &gt; 600)**.|
88
89
90## Media Features
91
92| Type| Description|
93| -------- | -------- |
94| height | Height of the display area on the application page.|
95| min-height | Minimum height of the display area on the application page.|
96| max-height | Maximum height of the display area on the application page.|
97| width | Width of the display area on the application page.|
98| min-width | Minimum width of the display area on the application page.|
99| max-width | Maximum width of the display area on the application page.|
100| resolution | Resolution of the device. The unit can be dpi, dppx, or dpcm. Where:<br>- **dpi** indicates the number of physical pixels per inch. 1 dpi ≈ 0.39 dpcm.<br>- **dpcm** indicates the number of physical pixels per centimeter. 1 dpcm ≈ 2.54 dpi.<br>- **dppx** indicates the number of physical pixels in each pixel. (This unit is calculated based on this formula: 96 px = 1 inch, which is different from the calculation method of the px unit on the page.) 1 dppx = 96 dpi.|
101| min-resolution | Minimum device resolution.|
102| max-resolution | Maximum device resolution.|
103| orientation | Screen orientation.<br>Options are as follows:<br>- orientation: portrait<br>- orientation: landscape|
104| aspect-ratio | Ratio of the width to the height of the display area on the application page.<br>Example: **aspect-ratio:1/2**|
105| min-aspect-ratio | Minimum ratio of the width to the height of the display area on the application page.|
106| max-aspect-ratio | Maximum ratio of the width to the height of the display area on the application page.|
107| device-height | Height of the device.|
108| min-device-height | Minimum height of the device.|
109| max-device-height | Maximum height of the device.|
110| device-width | Width of the device.|
111| min-device-width | Minimum width of the device.|
112| max-device-width | Maximum width of the device.|
113| round-screen | Screen type. The value **true** means that the screen is round, and **false** means the opposite.|
114| dark-mode<sup>6+</sup> | Whether the device is in dark mode. The value **true** means that the device is in dark mode, and **false** means the opposite.|
115
116
117## Sample Code
118
119
120```html
121<!-- xxx.hml -->
122<div>
123  <div class="container">
124    <text class="title">Hello World</text>
125  </div>
126</div>
127```
128
129
130```css
131/* xxx.css */
132.container {
133  width: 300px;
134  height: 600px;
135  background-color: #008000;
136}
137
138@media (device-type: wearable) {
139    .container-inner {
140        justify-content: center;
141        align-items: center;
142        padding: 40px 26px;
143    }
144
145    .title {
146        text-align: center;
147    }
148
149    .detail_text {
150        max-lines: 2;
151        text-align: center;
152    }
153}
154
155@media (device-type: tv) {
156    .title {
157        font-size: 16px;
158    }
159
160    .detail_text {
161        font-size: 12px;
162    }
163}
164
165@media (device-type: car) {
166    .title {
167        font-size: 12px;
168        color: #FFFFFF;
169        font-family: @id_text_font_family_medium;
170    }
171
172    .detail_text {
173        font-size: 12px;
174        margin-top: 2px;
175        font-family: @id_text_font_family_regular;
176        color: #FFFFFF;
177    }
178}
179```
180