1e41f4b71Sopenharmony_ci# Media Query
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciMedia 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:
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci1. Design a layout style based on the device and app attributes.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci2. Update the page layout to adapt to dynamic screen changes (for example, screen splitting or switching between landscape and portrait modes).
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci> **NOTE**
12e41f4b71Sopenharmony_ci>
13e41f4b71Sopenharmony_ci> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
14e41f4b71Sopenharmony_ci>
15e41f4b71Sopenharmony_ci> 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.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## CSS Syntax Rules
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciUse **\@media** to import query statements. The rule is as follows:
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci```css
24e41f4b71Sopenharmony_ci@media [media-type] [and|not|only] [(media-feature)] {
25e41f4b71Sopenharmony_ci  CSS-Code;
26e41f4b71Sopenharmony_ci}
27e41f4b71Sopenharmony_ci```
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciExample:
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci\@media screen and (round-screen: true) { … } // The condition is met when the device screen is round.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci\@media (max-height: 800) { … } // Range query. CSS level 3 is used.
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci\@media (height <= 800) { ... } // Range query. CSS level 4 is used, and the statement is equivalent to that of CSS level 3.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci\@media screen and (device-type: tv) or (resolution < 2) { … } // Multi-condition query that contains the media type and multiple media features.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci## Referencing Resources on a Page
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciUse **@import** to import a media query. The rule is as follows:
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci```
46e41f4b71Sopenharmony_ci@import url [media-type] [and|not|only] [(media-feature)];
47e41f4b71Sopenharmony_ci```
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciExample:
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci```
53e41f4b71Sopenharmony_ci@import '../common/style.css' screen and (min-width: 600) and (max-width: 1200);
54e41f4b71Sopenharmony_ci```
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci## Media Type
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci| Type| Description|
60e41f4b71Sopenharmony_ci| -------- | -------- |
61e41f4b71Sopenharmony_ci| screen | Media query based on screen-related parameters.|
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci## Media Logical Operation
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciMedia 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.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci  **Table 1** Media logical operators
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci| Type| Description|
71e41f4b71Sopenharmony_ci| -------- | -------- |
72e41f4b71Sopenharmony_ci| 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.|
73e41f4b71Sopenharmony_ci| 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.|
74e41f4b71Sopenharmony_ci| 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.|
75e41f4b71Sopenharmony_ci| ,(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.|
76e41f4b71Sopenharmony_ci| 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.|
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ciAt 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.
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci  **Table 2** Logical operators for range query
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci| Type| Description|
83e41f4b71Sopenharmony_ci| -------- | -------- |
84e41f4b71Sopenharmony_ci| &lt;= | Less than or equal to, for example, **screen and (height &lt;= 50)**.|
85e41f4b71Sopenharmony_ci| &gt;= | Greater than or equal to, for example, **screen and (height &gt;= 600)**.|
86e41f4b71Sopenharmony_ci| &lt; | Less than, for example, **screen and (height &lt; 50)**.|
87e41f4b71Sopenharmony_ci| &gt; | Greater than, for example, **screen and (height &gt; 600)**.|
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci## Media Features
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci| Type| Description|
93e41f4b71Sopenharmony_ci| -------- | -------- |
94e41f4b71Sopenharmony_ci| height | Height of the display area on the application page.|
95e41f4b71Sopenharmony_ci| min-height | Minimum height of the display area on the application page.|
96e41f4b71Sopenharmony_ci| max-height | Maximum height of the display area on the application page.|
97e41f4b71Sopenharmony_ci| width | Width of the display area on the application page.|
98e41f4b71Sopenharmony_ci| min-width | Minimum width of the display area on the application page.|
99e41f4b71Sopenharmony_ci| max-width | Maximum width of the display area on the application page.|
100e41f4b71Sopenharmony_ci| 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.|
101e41f4b71Sopenharmony_ci| min-resolution | Minimum device resolution.|
102e41f4b71Sopenharmony_ci| max-resolution | Maximum device resolution.|
103e41f4b71Sopenharmony_ci| orientation | Screen orientation.<br>Options are as follows:<br>- orientation: portrait<br>- orientation: landscape|
104e41f4b71Sopenharmony_ci| aspect-ratio | Ratio of the width to the height of the display area on the application page.<br>Example: **aspect-ratio:1/2**|
105e41f4b71Sopenharmony_ci| min-aspect-ratio | Minimum ratio of the width to the height of the display area on the application page.|
106e41f4b71Sopenharmony_ci| max-aspect-ratio | Maximum ratio of the width to the height of the display area on the application page.|
107e41f4b71Sopenharmony_ci| device-height | Height of the device.|
108e41f4b71Sopenharmony_ci| min-device-height | Minimum height of the device.|
109e41f4b71Sopenharmony_ci| max-device-height | Maximum height of the device.|
110e41f4b71Sopenharmony_ci| device-width | Width of the device.|
111e41f4b71Sopenharmony_ci| min-device-width | Minimum width of the device.|
112e41f4b71Sopenharmony_ci| max-device-width | Maximum width of the device.|
113e41f4b71Sopenharmony_ci| round-screen | Screen type. The value **true** means that the screen is round, and **false** means the opposite.|
114e41f4b71Sopenharmony_ci| 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.|
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci## Sample Code
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci```html
121e41f4b71Sopenharmony_ci<!-- xxx.hml -->
122e41f4b71Sopenharmony_ci<div>
123e41f4b71Sopenharmony_ci  <div class="container">
124e41f4b71Sopenharmony_ci    <text class="title">Hello World</text>
125e41f4b71Sopenharmony_ci  </div>
126e41f4b71Sopenharmony_ci</div>
127e41f4b71Sopenharmony_ci```
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci```css
131e41f4b71Sopenharmony_ci/* xxx.css */
132e41f4b71Sopenharmony_ci.container {
133e41f4b71Sopenharmony_ci  width: 300px;
134e41f4b71Sopenharmony_ci  height: 600px;
135e41f4b71Sopenharmony_ci  background-color: #008000;
136e41f4b71Sopenharmony_ci}
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci@media (device-type: wearable) {
139e41f4b71Sopenharmony_ci    .container-inner {
140e41f4b71Sopenharmony_ci        justify-content: center;
141e41f4b71Sopenharmony_ci        align-items: center;
142e41f4b71Sopenharmony_ci        padding: 40px 26px;
143e41f4b71Sopenharmony_ci    }
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci    .title {
146e41f4b71Sopenharmony_ci        text-align: center;
147e41f4b71Sopenharmony_ci    }
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci    .detail_text {
150e41f4b71Sopenharmony_ci        max-lines: 2;
151e41f4b71Sopenharmony_ci        text-align: center;
152e41f4b71Sopenharmony_ci    }
153e41f4b71Sopenharmony_ci}
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci@media (device-type: tv) {
156e41f4b71Sopenharmony_ci    .title {
157e41f4b71Sopenharmony_ci        font-size: 16px;
158e41f4b71Sopenharmony_ci    }
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci    .detail_text {
161e41f4b71Sopenharmony_ci        font-size: 12px;
162e41f4b71Sopenharmony_ci    }
163e41f4b71Sopenharmony_ci}
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci@media (device-type: car) {
166e41f4b71Sopenharmony_ci    .title {
167e41f4b71Sopenharmony_ci        font-size: 12px;
168e41f4b71Sopenharmony_ci        color: #FFFFFF;
169e41f4b71Sopenharmony_ci        font-family: @id_text_font_family_medium;
170e41f4b71Sopenharmony_ci    }
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci    .detail_text {
173e41f4b71Sopenharmony_ci        font-size: 12px;
174e41f4b71Sopenharmony_ci        margin-top: 2px;
175e41f4b71Sopenharmony_ci        font-family: @id_text_font_family_regular;
176e41f4b71Sopenharmony_ci        color: #FFFFFF;
177e41f4b71Sopenharmony_ci    }
178e41f4b71Sopenharmony_ci}
179e41f4b71Sopenharmony_ci```
180