1e41f4b71Sopenharmony_ci# CSS 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_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. For details, see [Universal Styles](../reference/apis-arkui/arkui-js/js-components-common-styles.md). 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## Size Unit 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- Logical px set by **\<length>**: 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci - The default logical screen width is 720 px (for details, see the **"window"** section in **[config.json](js-framework-js-tag.md)**). Your page will be scaled to fit the actual width of the screen. For example, on a screen with the actual width of 1440 physical px, 100 px is displayed on 200 physical px, with all sizes doubled from 720 px to 1440 px. 10e41f4b71Sopenharmony_ci - If you set **autoDesignWidth** to **true** (for details, see the **"window"** section in **[config.json](js-framework-js-tag.md)**), the logical px 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. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci- 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. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Style Import 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciCSS files can be imported using the **@import** statement. This facilitates module management and code reuse. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci## Style Declaration 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_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. 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci1. Internal style: The **style** and **class** attributes can be used to specify the component style. Example: 25e41f4b71Sopenharmony_ci ```html 26e41f4b71Sopenharmony_ci <!-- index.hml --> 27e41f4b71Sopenharmony_ci <div class="container"> 28e41f4b71Sopenharmony_ci <text style="color: red">Hello World</text> 29e41f4b71Sopenharmony_ci </div> 30e41f4b71Sopenharmony_ci ``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci ```css 33e41f4b71Sopenharmony_ci /* index.css */ 34e41f4b71Sopenharmony_ci .container { 35e41f4b71Sopenharmony_ci justify-content: center; 36e41f4b71Sopenharmony_ci } 37e41f4b71Sopenharmony_ci ``` 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_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**. 40e41f4b71Sopenharmony_ci ```css 41e41f4b71Sopenharmony_ci /* style.css */ 42e41f4b71Sopenharmony_ci .title { 43e41f4b71Sopenharmony_ci font-size: 50px; 44e41f4b71Sopenharmony_ci } 45e41f4b71Sopenharmony_ci ``` 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci ```css 48e41f4b71Sopenharmony_ci /* index.css */ 49e41f4b71Sopenharmony_ci @import '../../common/style.css'; 50e41f4b71Sopenharmony_ci .container { 51e41f4b71Sopenharmony_ci justify-content: center; 52e41f4b71Sopenharmony_ci } 53e41f4b71Sopenharmony_ci ``` 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci## Selectors 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciA CSS selector is used to select elements for which styles need to be added to. The following table lists the supported selectors. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci| Selector | Example | Description | 61e41f4b71Sopenharmony_ci| ------------------------- | ------------------------------------- | ---------------------------------------- | 62e41f4b71Sopenharmony_ci| .class | .container | Selects all components whose **class** is **container**. | 63e41f4b71Sopenharmony_ci| \#id | \#titleId | Selects all components whose **id** is **titleId**. | 64e41f4b71Sopenharmony_ci| tag | text | Selects all **\<text>** components. | 65e41f4b71Sopenharmony_ci| , | .title, .content | Selects all components whose **class** is **title** or **content**. | 66e41f4b71Sopenharmony_ci| \#id .class tag | \#containerId .content text | Selects all grandchild **\<text>** components whose grandparent components are identified as **containerId** and whose parent components are of the **content** class. To select child components, use **>** to replace the space between **\#id** and **.class**, for example, **\#containerId>.content**.| 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ciExample: 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci```html 71e41f4b71Sopenharmony_ci<!-- Pagelayoutexample.hml --> 72e41f4b71Sopenharmony_ci<div id="containerId" class="container"> 73e41f4b71Sopenharmony_ci <text id="titleId" class="title">Title</text> 74e41f4b71Sopenharmony_ci <div class="content"> 75e41f4b71Sopenharmony_ci <text id="contentId">Content</text> 76e41f4b71Sopenharmony_ci </div> 77e41f4b71Sopenharmony_ci</div> 78e41f4b71Sopenharmony_ci``` 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci```css 81e41f4b71Sopenharmony_ci/* Pagestyleexample.css */ 82e41f4b71Sopenharmony_ci/* Set the style for all <div> components. */ 83e41f4b71Sopenharmony_cidiv { 84e41f4b71Sopenharmony_ci flex-direction: column; 85e41f4b71Sopenharmony_ci} 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/* Set padding for all components of the title or content class to 5 px. */ 95e41f4b71Sopenharmony_ci.title, .content { 96e41f4b71Sopenharmony_ci padding: 5px; 97e41f4b71Sopenharmony_ci} 98e41f4b71Sopenharmony_ci/* Set the style for all texts of components whose class is container. */ 99e41f4b71Sopenharmony_ci.container text { 100e41f4b71Sopenharmony_ci color: #007dff; 101e41f4b71Sopenharmony_ci} 102e41f4b71Sopenharmony_ci/* Set the style for direct descendant texts of components whose class is container. */ 103e41f4b71Sopenharmony_ci.container > text { 104e41f4b71Sopenharmony_ci color: #fa2a2d; 105e41f4b71Sopenharmony_ci} 106e41f4b71Sopenharmony_ci``` 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciThe above style works as follows: 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ciIn the preceding example, **.container text** sets title and content to blue, and **.container > text** sets title to red. The two styles have the same priority, but **.container > text** declares the style later and overwrites the former style. For details about the priority, see [Selector Specificity](#selector-specificity). 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci## Selector Specificity 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciThe specificity rule of the selectors complies with the W3C rule, which is only available for inline styles, **id**, **class**, **tag**, grandchild components, and child components. (Inline styles are those declared in the **style** attribute.) 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ciWhen multiple selectors point to the same element, their priorities are as follows (in descending order): inline style > **id** > **class** > **tag**. 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci## Pseudo-classes 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ciA CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected elements. For example, **:disabled** can be used to select the element whose **disabled** attribute is **true**. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciIn addition to a single pseudo-class, a combination of pseudo-classes is supported. For example, **:focus:checked** selects the element whose **focus** and **checked** attributes are both set to **true**. The following table lists the supported single pseudo-class in descending order of priority. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci| Pseudo-class | Available Components | Description | 128e41f4b71Sopenharmony_ci| --------- | ---------------------------------------- | ---------------------------------------- | 129e41f4b71Sopenharmony_ci| :disabled | Components that support the **disabled** attribute | Selects the element whose **disabled** attribute is changed to **true** (unavailable for animation attributes). | 130e41f4b71Sopenharmony_ci| :active | Components that support the click event | Selects the element activated by a user. For example, a pressed button or a selected **tab-bar** (unavailable for animation attributes).| 131e41f4b71Sopenharmony_ci| :waiting | button | Selects the element whose **waiting** attribute is **true** (unavailable for animation attributes). | 132e41f4b71Sopenharmony_ci| :checked | input[type="checkbox", type="radio"], switch | Selects the element whose **checked** attribute is **true** (unavailable for animation attributes). | 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ciThe following is an example for you to use the **:active** pseudo-class to control the style when a user presses the button. 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci```html 137e41f4b71Sopenharmony_ci<!-- index.hml --> 138e41f4b71Sopenharmony_ci<div class="container"> 139e41f4b71Sopenharmony_ci <input type="button" class="button" value="Button"></input> 140e41f4b71Sopenharmony_ci</div> 141e41f4b71Sopenharmony_ci``` 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci```css 144e41f4b71Sopenharmony_ci/* index.css */ 145e41f4b71Sopenharmony_ci.button:active { 146e41f4b71Sopenharmony_ci background-color: #888888;/* After the button is activated, the background color is changed to #888888. */ 147e41f4b71Sopenharmony_ci} 148e41f4b71Sopenharmony_ci``` 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci> **NOTE** 151e41f4b71Sopenharmony_ci> Pseudo-classes are not supported for the **\<popup>** component and its child components, including, **\<popup>**, **\<dialog>**, **\<menu>**, **\<option>**, and **\<picker>**. 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci## Precompiled Styles 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_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**. 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci- The following **index.less** file is changed from **index.css**. 159e41f4b71Sopenharmony_ci ```less 160e41f4b71Sopenharmony_ci /* index.less */ 161e41f4b71Sopenharmony_ci /* Define a variable. */ 162e41f4b71Sopenharmony_ci @colorBackground: #000000; 163e41f4b71Sopenharmony_ci .container { 164e41f4b71Sopenharmony_ci background-color: @colorBackground; /* Use the variable defined in the .less file. */ 165e41f4b71Sopenharmony_ci } 166e41f4b71Sopenharmony_ci ``` 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_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**. 169e41f4b71Sopenharmony_ci ```scss 170e41f4b71Sopenharmony_ci /* style.scss */ 171e41f4b71Sopenharmony_ci /* Define a variable. */ 172e41f4b71Sopenharmony_ci $colorBackground: #000000; 173e41f4b71Sopenharmony_ci ``` 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci Reference the precompiled style file in **index.scss**: 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci ```scss 178e41f4b71Sopenharmony_ci /* index.scss */ 179e41f4b71Sopenharmony_ci /* Import style.scss. */ 180e41f4b71Sopenharmony_ci @import '../../common/style.scss'; 181e41f4b71Sopenharmony_ci .container { 182e41f4b71Sopenharmony_ci background-color: $colorBackground; /* Use the variable defined in style.scss. */ 183e41f4b71Sopenharmony_ci } 184e41f4b71Sopenharmony_ci ``` 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci > **NOTE** 187e41f4b71Sopenharmony_ci > 188e41f4b71Sopenharmony_ci > Place precompiled style files in the **common** directory. 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci## CSS Style Inheritance<sup>6+</sup> 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ciCSS style inheritance enables a child node to inherit the style of its parent node. The inherited style has the lowest priority when multiple style selectors are involved. Currently, the following styles can be inherited: 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci- font-family 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci- font-weight 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci- font-size 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci- font-style 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci- text-align 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci- line-height 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci- letter-spacing 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci- color 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci- visibility 211