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## Style Import 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciCSS files can be imported using the **\@import** statement. This facilitates module management and code reuse. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Style Declaration 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_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. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci1. Internal style: The **style** and **class** attributes can be used to specify the component style. Sample code: 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci ```html 19e41f4b71Sopenharmony_ci <!-- index.hml --> 20e41f4b71Sopenharmony_ci <div class="container"> 21e41f4b71Sopenharmony_ci <text style="color: red">Hello World</text> 22e41f4b71Sopenharmony_ci </div> 23e41f4b71Sopenharmony_ci ``` 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci ```css 27e41f4b71Sopenharmony_ci /* index.css */ 28e41f4b71Sopenharmony_ci .container { 29e41f4b71Sopenharmony_ci justify-content: center; 30e41f4b71Sopenharmony_ci } 31e41f4b71Sopenharmony_ci ``` 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_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**. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci ```css 36e41f4b71Sopenharmony_ci /* style.css */ 37e41f4b71Sopenharmony_ci .title { 38e41f4b71Sopenharmony_ci font-size: 50px; 39e41f4b71Sopenharmony_ci } 40e41f4b71Sopenharmony_ci ``` 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci ```css 44e41f4b71Sopenharmony_ci /* index.css */ 45e41f4b71Sopenharmony_ci @import '../../common/style.css'; 46e41f4b71Sopenharmony_ci .container { 47e41f4b71Sopenharmony_ci justify-content: center; 48e41f4b71Sopenharmony_ci } 49e41f4b71Sopenharmony_ci ``` 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci## Selectors 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ciA CSS selector is used to select elements for which styles need to be added to. The following table lists the supported selectors. 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci| Selector | Example | Description | 57e41f4b71Sopenharmony_ci| ------ | --------------------- | ------------------------------------- | 58e41f4b71Sopenharmony_ci| .class | .container | Selects all components whose **class** is **container**. | 59e41f4b71Sopenharmony_ci| \#id | \#titleId | Selects all components whose **id** is **titleId**. | 60e41f4b71Sopenharmony_ci| , | .title, .content | Selects all components whose **class** is **title** or **content**.| 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciExample: 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci```html 66e41f4b71Sopenharmony_ci<!-- Pagelayoutexample.hml --> 67e41f4b71Sopenharmony_ci<div id="containerId" class="container"> 68e41f4b71Sopenharmony_ci <text id="titleId" class="title">Title</text> 69e41f4b71Sopenharmony_ci <div class="content"> 70e41f4b71Sopenharmony_ci <text id="contentId">Content</text> 71e41f4b71Sopenharmony_ci </div> 72e41f4b71Sopenharmony_ci</div> 73e41f4b71Sopenharmony_ci``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci```css 77e41f4b71Sopenharmony_ci/* Page style xxx.css */ 78e41f4b71Sopenharmony_ci/* Set the style for the components whose class is title. */ 79e41f4b71Sopenharmony_ci.title { 80e41f4b71Sopenharmony_ci font-size: 30px; 81e41f4b71Sopenharmony_ci} 82e41f4b71Sopenharmony_ci/* Set the style for the components whose id is contentId. */ 83e41f4b71Sopenharmony_ci#contentId { 84e41f4b71Sopenharmony_ci font-size: 20px; 85e41f4b71Sopenharmony_ci} 86e41f4b71Sopenharmony_ci/* Set padding for all components of the title or content class to 5 px. */ 87e41f4b71Sopenharmony_ci.title, .content { 88e41f4b71Sopenharmony_ci padding: 5px; 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci## Pseudo-classes 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciA CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected elements. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci| Name | Available Components | Description | 101e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | ---------------------------------------- | 102e41f4b71Sopenharmony_ci| :active | <br>input[type="button"] | Selects the element activated by a user, for example, a pressed button. Only the **background-color** and **background-image** attributes can be set for the pseudo-class selector on lite wearables.| 103e41f4b71Sopenharmony_ci| :checked | input[type="checkbox", type="radio"]| Selects the element whose **checked** attribute is **true**. Only the **background-color** and **background-image** attributes can be set for the pseudo-class selector on lite wearables.| 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ciThe following is an example for you to use the **:active** pseudo-class to control the style when a user presses the button. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci```html 109e41f4b71Sopenharmony_ci<!-- index.hml --> 110e41f4b71Sopenharmony_ci<div class="container"> 111e41f4b71Sopenharmony_ci <input type="button" class="button" value="Button"></input> 112e41f4b71Sopenharmony_ci</div> 113e41f4b71Sopenharmony_ci``` 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci```css 117e41f4b71Sopenharmony_ci/* index.css */ 118e41f4b71Sopenharmony_ci.button:active { 119e41f4b71Sopenharmony_ci background-color: #888888;/* After the button is activated, the background color is changed to #888888. */ 120e41f4b71Sopenharmony_ci} 121e41f4b71Sopenharmony_ci``` 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci## Precompiled Styles 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ciPrecompilation is a program that uses specific syntax to generate CSS files. It provides variables and calculation, helping you define component styles more conveniently. Currently, Less, Sass, and Scss are supported. To use precompiled styles, change the suffix of the original **.css** file. For example, change **index.css** to **index.less**, **index.sass**, or **index.scss**. 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci- The following **index.less** file is changed from **index.css**. 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci ```css 131e41f4b71Sopenharmony_ci /* index.less */ 132e41f4b71Sopenharmony_ci /* Define a variable. */ 133e41f4b71Sopenharmony_ci @colorBackground: #000000; 134e41f4b71Sopenharmony_ci .container { 135e41f4b71Sopenharmony_ci background-color: @colorBackground; /* Use the variable defined in the .less file. */ 136e41f4b71Sopenharmony_ci } 137e41f4b71Sopenharmony_ci ``` 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci- Reference a precompiled style file. For example, if the **style.scss** file is located in the **common** directory, change the original **index.css** file to **index.scss** and import **style.scss**. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci ```css 142e41f4b71Sopenharmony_ci /* style.scss */ 143e41f4b71Sopenharmony_ci /* Define a variable. */ 144e41f4b71Sopenharmony_ci $colorBackground: #000000; 145e41f4b71Sopenharmony_ci ``` 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci Reference the precompiled style file in **index.scss**: 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci ```css 151e41f4b71Sopenharmony_ci /* index.scss */ 152e41f4b71Sopenharmony_ci /* Import style.scss. */ 153e41f4b71Sopenharmony_ci @import '../../common/style.scss'; 154e41f4b71Sopenharmony_ci .container { 155e41f4b71Sopenharmony_ci background-color: $colorBackground; /* Use the variable defined in style.scss. */ 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci ``` 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci > **NOTE** 161e41f4b71Sopenharmony_ci > 162e41f4b71Sopenharmony_ci > Place precompiled style files in the **common** directory. 163