1# CSS 2 3 4Cascading Style Sheets (CSS) is a language used to describe the HML page structure. All HML components have default styles. You can customize styles for these components using CSS to design various pages. 5 6 7## Size Unit 8 91. Logical px set by **\<length>**: 10 1. The default logical width of a service widget is 150 px. The page will be scaled to fit the actual width of the screen. For example, on a screen with the actual width of 300 physical px, 100 px is rendered on 200 physical px, with the size doubled from 150 px to 300 px. 11 2. If you set **autoDesignWidth** to **true**, the logical pixels are scaled based on the screen density. For example, if the screen density is 3x, 100 px will be rendered on 300 physical px. This approach is recommended when your application needs to adapt to multiple devices. 12 132. Percentage set by **\<percentage>**: The component size is represented by its percentage of the parent component size. For example, if the width **\<percentage>** of a component is set to **50%**, the width of the component is half of its parent component's width. 14 15 16## Style Import 17 18CSS files can be imported using the **\@import** statement. This facilitates module management and code reuse. 19 20 21## Style Declaration 22 23The **.css** file with the same name as the **.hml** file in each page directory describes the styles of components on the HML page, determining how the components will be displayed. 24 251. Internal style: The **style** and **class** attributes can be used to specify the component style. Example: 26 27 ```html 28 <!-- index.hml --> 29 <div class="container"> 30 <text style="color: red">Hello World</text> 31 </div> 32 ``` 33 34 35 ```css 36 /* index.css */ 37 .container { 38 justify-content: center; 39 } 40 ``` 41 422. External style files: You need to import the files. For example, create a **style.css** file in the **common** directory and import the file at the beginning of **index.css**. 43 44 ```css 45 /* style.css */ 46 .title { 47 font-size: 50px; 48 } 49 ``` 50 51 52 ```css 53 /* index.css */ 54 @import '../../common/style.css'; 55 .container { 56 justify-content: center; 57 } 58 ``` 59 60 61## Selectors 62 63A CSS selector is used to select elements for which styles need to be added to. The following table lists the supported selectors. 64 65| Selector| Example| Description| 66| -------- | -------- | -------- | 67| .class | .container | Selects all components whose **class** is **container**.| 68| \#id | \#titleId | Selects all components whose **id** is **titleId**.| 69 70Example: 71 72 73```html 74<!-- Pagelayoutexample.hml --> 75<div id="containerId" class="container"> 76 <text id="titleId" class="title">Title</text> 77 <div class="content"> 78 <text id="contentId">Content</text> 79 </div> 80</div> 81``` 82 83 84```css 85/* Pagestyleexample.css */ 86/* Set the style for the components whose class is title. */ 87.title { 88 font-size: 30px; 89} 90/* Set the style for the components whose id is contentId. */ 91#contentId { 92 font-size: 20px; 93} 94``` 95 96 97## Selector Specificity 98 99The specificity rule of the selectors complies with the W3C rule, which is only available for inline styles, **id**, and **class**. (Inline styles are those declared in the **style** attribute.) 100 101When multiple selectors point to the same element, their priorities are as follows (in descending order): inline style > **id** > **class**. 102