1e41f4b71Sopenharmony_ci# CSS 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciCascading 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. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Size Unit 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci1. Logical px set by **\<length>**: 10e41f4b71Sopenharmony_ci 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. 11e41f4b71Sopenharmony_ci 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. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci2. 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. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Style Import 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciCSS files can be imported using the **\@import** statement. This facilitates module management and code reuse. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## Style Declaration 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciThe **.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. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci1. Internal style: The **style** and **class** attributes can be used to specify the component style. Example: 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci ```html 28e41f4b71Sopenharmony_ci <!-- index.hml --> 29e41f4b71Sopenharmony_ci <div class="container"> 30e41f4b71Sopenharmony_ci <text style="color: red">Hello World</text> 31e41f4b71Sopenharmony_ci </div> 32e41f4b71Sopenharmony_ci ``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci ```css 36e41f4b71Sopenharmony_ci /* index.css */ 37e41f4b71Sopenharmony_ci .container { 38e41f4b71Sopenharmony_ci justify-content: center; 39e41f4b71Sopenharmony_ci } 40e41f4b71Sopenharmony_ci ``` 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci2. 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**. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci ```css 45e41f4b71Sopenharmony_ci /* style.css */ 46e41f4b71Sopenharmony_ci .title { 47e41f4b71Sopenharmony_ci font-size: 50px; 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci ``` 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci ```css 53e41f4b71Sopenharmony_ci /* index.css */ 54e41f4b71Sopenharmony_ci @import '../../common/style.css'; 55e41f4b71Sopenharmony_ci .container { 56e41f4b71Sopenharmony_ci justify-content: center; 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci ``` 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci## Selectors 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciA CSS selector is used to select elements for which styles need to be added to. The following table lists the supported selectors. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci| Selector | Example | Description | 66e41f4b71Sopenharmony_ci| ------ | ---------- | ------------------------- | 67e41f4b71Sopenharmony_ci| .class | .container | Selects all components whose **class** is **container**.| 68e41f4b71Sopenharmony_ci| \#id | \#titleId | Selects all components whose **id** is **titleId**. | 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ciExample: 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci```html 74e41f4b71Sopenharmony_ci<!-- Pagelayoutexample.hml --> 75e41f4b71Sopenharmony_ci<div id="containerId" class="container"> 76e41f4b71Sopenharmony_ci <text id="titleId" class="title">Title</text> 77e41f4b71Sopenharmony_ci <div class="content"> 78e41f4b71Sopenharmony_ci <text id="contentId">Content</text> 79e41f4b71Sopenharmony_ci </div> 80e41f4b71Sopenharmony_ci</div> 81e41f4b71Sopenharmony_ci``` 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci```css 85e41f4b71Sopenharmony_ci/* Pagestyleexample.css */ 86e41f4b71Sopenharmony_ci/* Set the style for the components whose class is title. */ 87e41f4b71Sopenharmony_ci.title { 88e41f4b71Sopenharmony_ci font-size: 30px; 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci/* Set the style for the components whose id is contentId. */ 91e41f4b71Sopenharmony_ci#contentId { 92e41f4b71Sopenharmony_ci font-size: 20px; 93e41f4b71Sopenharmony_ci} 94e41f4b71Sopenharmony_ci``` 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci## Selector Specificity 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ciThe 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.) 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciWhen multiple selectors point to the same element, their priorities are as follows (in descending order): inline style > **id** > **class**. 102