1e41f4b71Sopenharmony_ci# Media Query 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci> **NOTE** 4e41f4b71Sopenharmony_ci> 5e41f4b71Sopenharmony_ci> - This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version. 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciMedia Query is widely used on mobile devices. You may need 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: 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci1. Design a layout style based on the device and application attributes. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci2. Update the page layout to adapt to dynamic screen changes (for example, screen splitting or switching between landscape and portrait modes). 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@media [media-type] [and|not|only] [(media-feature)] { 24e41f4b71Sopenharmony_ci CSS-Code; 25e41f4b71Sopenharmony_ci} 26e41f4b71Sopenharmony_ci``` 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci@media screen and (round-screen: true) { … }: The condition is met when the device screen is round. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci@media (max-height: 800) { … }: Range query. CSS level 3 is used. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci@media (height <= 800) { ... }: Range query. CSS level 4 is used, and the statement is equivalent to that of CSS level 3. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci@media screen and (device-type: tv) or (resolution < 2) { ... }: Multi-condition query that contains the media type and multiple media features. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci## Referencing Resources on a Page 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciUse **@import** to import a media query. The rule is as follows: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci``` 42e41f4b71Sopenharmony_ci@import url [media-type] [and|not|only] [(media-feature)]; 43e41f4b71Sopenharmony_ci``` 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ciExample: 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci``` 48e41f4b71Sopenharmony_ci@import '../common/style.css' screen and (min-width: 600) and (max-width: 1200); 49e41f4b71Sopenharmony_ci``` 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci## Media Type 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci| Type | Description | 55e41f4b71Sopenharmony_ci| ------ | -------------- | 56e41f4b71Sopenharmony_ci| screen | Media query based on screen-related parameters.| 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci## Media Logical Operation 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_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. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**Table 1** Media logical operators 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci| Type | Description | 66e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | 67e41f4b71Sopenharmony_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)** evaluates to **true** when the device type is wearable and the maximum height of the application is 600 pixel units.| 68e41f4b71Sopenharmony_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)** evaluates to **true** 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.| 69e41f4b71Sopenharmony_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.| 70e41f4b71Sopenharmony_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)** evaluates to **true** when the minimum height of the application is 1000 pixel units or the device screen is round.| 71e41f4b71Sopenharmony_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)** evaluates to **true** when the maximum height of the application is 1000 pixel units or the device screen is round.| 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ciAt MediaQuery Level 4, range query is imported so that you can use the operators including <=, >=, <, and > besides the max- and min-operators. 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**Table 2** Logical operators for range query 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci| Type | Description | 78e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- | 79e41f4b71Sopenharmony_ci| <= | Less than or equal to, for example, **screen and (height <= 50)**.| 80e41f4b71Sopenharmony_ci| >= | Greater than or equal to, for example, **screen and (height >= 600)**.| 81e41f4b71Sopenharmony_ci| < | Less than, for example, **screen and (height < 50)**.| 82e41f4b71Sopenharmony_ci| > | Greater than, for example, **screen and (height > 600)**.| 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci## Media Features 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci| Type | Description | 88e41f4b71Sopenharmony_ci| ---------------------- | ---------------------------------------- | 89e41f4b71Sopenharmony_ci| height | Height of the display area on the application page. | 90e41f4b71Sopenharmony_ci| min-height | Minimum height of the display area on the application page. | 91e41f4b71Sopenharmony_ci| max-height | Maximum height of the display area on the application page. | 92e41f4b71Sopenharmony_ci| width | Width of the display area on the application page. | 93e41f4b71Sopenharmony_ci| min-width | Minimum width of the display area on the application page. | 94e41f4b71Sopenharmony_ci| max-width | Maximum width of the display area on the application page. | 95e41f4b71Sopenharmony_ci| resolution | Resolution of the device. The unit can be dpi, dppx, or dpcm. <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 per 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. | 96e41f4b71Sopenharmony_ci| min-resolution | Minimum device resolution. | 97e41f4b71Sopenharmony_ci| max-resolution | Maximum device resolution. | 98e41f4b71Sopenharmony_ci| orientation | Screen orientation.<br>Options are as follows:<br>- orientation: portrait<br>- orientation: landscape| 99e41f4b71Sopenharmony_ci| aspect-ratio | Ratio of the width to the height of the display area on the application page.<br>Example: **aspect-ratio:1/2**| 100e41f4b71Sopenharmony_ci| min-aspect-ratio | Minimum ratio of the width to the height of the display area on the application page. | 101e41f4b71Sopenharmony_ci| max-aspect-ratio | Maximum ratio of the width to the height of the display area on the application page. | 102e41f4b71Sopenharmony_ci| device-height | Height of the device. | 103e41f4b71Sopenharmony_ci| min-device-height | Minimum height of the device. | 104e41f4b71Sopenharmony_ci| max-device-height | Maximum height of the device. | 105e41f4b71Sopenharmony_ci| device-width | Width of the device. | 106e41f4b71Sopenharmony_ci| min-device-width | Minimum width of the device. | 107e41f4b71Sopenharmony_ci| max-device-width | Maximum width of the device. | 108e41f4b71Sopenharmony_ci| round-screen | Screen type. The value **true** means that the screen is round, and **false** means the opposite. | 109e41f4b71Sopenharmony_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. | 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci## Sample Code for the Common Media Feature 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ciThe number and type of attributes must be the same among **.container** blocks. Otherwise, display errors will occur. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci```html 117e41f4b71Sopenharmony_ci<!-- xxx.hml --> 118e41f4b71Sopenharmony_ci<div> 119e41f4b71Sopenharmony_ci <div class="container"> 120e41f4b71Sopenharmony_ci <text class="title">Hello World</text> 121e41f4b71Sopenharmony_ci </div> 122e41f4b71Sopenharmony_ci</div> 123e41f4b71Sopenharmony_ci``` 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci```css 126e41f4b71Sopenharmony_ci/* xxx.css */ 127e41f4b71Sopenharmony_ci.container { 128e41f4b71Sopenharmony_ci width: 300px; 129e41f4b71Sopenharmony_ci height: 600px; 130e41f4b71Sopenharmony_ci background-color: #008000; 131e41f4b71Sopenharmony_ci} 132e41f4b71Sopenharmony_ci@media (device-type: tv) { 133e41f4b71Sopenharmony_ci .container { 134e41f4b71Sopenharmony_ci width: 500px; 135e41f4b71Sopenharmony_ci height: 500px; 136e41f4b71Sopenharmony_ci background-color: #fa8072; 137e41f4b71Sopenharmony_ci } 138e41f4b71Sopenharmony_ci} 139e41f4b71Sopenharmony_ci@media (device-type: wearable) { 140e41f4b71Sopenharmony_ci .container { 141e41f4b71Sopenharmony_ci width: 300px; 142e41f4b71Sopenharmony_ci height: 300px; 143e41f4b71Sopenharmony_ci background-color: #008b8b; 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci} 146e41f4b71Sopenharmony_ci``` 147