• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CSS
2
3Cascading 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/arkui-js/js-components-common-styles.md).
4
5## Size Unit
6
7- Logical px set by **\<length>**:
8
9   - 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.
10   - 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.
11
12- 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.
13
14
15## Style Import
16
17CSS files can be imported using the **@import** statement. This facilitates module management and code reuse.
18
19
20## Style Declaration
21
22The **.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.
23
241. Internal style: The **style** and **class** attributes can be used to specify the component style. Example:
25   ```html
26   <!-- index.hml -->
27   <div class="container">
28     <text style="color: red">Hello World</text>
29   </div>
30   ```
31
32   ```css
33   /* index.css */
34   .container {
35     justify-content: center;
36   }
37   ```
38
392. 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**.
40   ```css
41   /* style.css */
42   .title {
43     font-size: 50px;
44   }
45   ```
46
47   ```css
48   /* index.css */
49   @import '../../common/style.css';
50   .container {
51     justify-content: center;
52   }
53   ```
54
55
56## Selectors
57
58A CSS selector is used to select elements for which styles need to be added to. The following table lists the supported selectors.
59
60| Selector                      | Example                                   | Description                                    |
61| ------------------------- | ------------------------------------- | ---------------------------------------- |
62| .class                    | .container                            | Selects all components whose **class** is **container**.               |
63| \#id                      | \#titleId                             | Selects all components whose **id** is **titleId**.                    |
64| tag                       | text                                  | Selects all **\<text>** components.                             |
65| ,                         | .title, .content                 | Selects all components whose **class** is **title** or **content**.   |
66| \#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**.|
67
68Example:
69
70```html
71<!-- Pagelayoutexample.hml -->
72<div id="containerId" class="container">
73  <text id="titleId" class="title">Title</text>
74  <div class="content">
75    <text id="contentId">Content</text>
76  </div>
77</div>
78```
79
80```css
81/* Pagestyleexample.css */
82/* Set the style for all <div> components. */
83div {
84  flex-direction: column;
85}
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/* Set padding for all components of the title or content class to 5 px. */
95.title, .content {
96  padding: 5px;
97}
98/* Set the style for all texts of components whose class is container. */
99.container text {
100  color: #007dff;
101}
102/* Set the style for direct descendant texts of components whose class is container. */
103.container > text {
104  color: #fa2a2d;
105}
106```
107
108The above style works as follows:
109
110![en-us_image_0000001267607889](figures/en-us_image_0000001267607889.png)
111
112In 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).
113
114## Selector Specificity
115
116The 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.)
117
118When multiple selectors point to the same element, their priorities are as follows (in descending order): inline style > **id** > **class** > **tag**.
119
120
121## Pseudo-classes
122
123A 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**.
124
125In 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.
126
127| Pseudo-class       | Available Components                                    | Description                                      |
128| --------- | ---------------------------------------- | ---------------------------------------- |
129| :disabled | Components that support the **disabled** attribute                         | Selects the element whose **disabled** attribute is changed to **true** (unavailable for animation attributes).     |
130| :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).|
131| :waiting  | button                                   | Selects the element whose **waiting** attribute is **true** (unavailable for animation attributes).        |
132| :checked  | input[type="checkbox", type="radio"], switch | Selects the element whose **checked** attribute is **true** (unavailable for animation attributes).        |
133
134The following is an example for you to use the **:active** pseudo-class to control the style when a user presses the button.
135
136```html
137<!-- index.hml -->
138<div class="container">
139  <input type="button" class="button" value="Button"></input>
140</div>
141```
142
143```css
144/* index.css */
145.button:active {
146    background-color: #888888;/* After the button is activated, the background color is changed to #888888. */
147}
148```
149
150> **NOTE**
151> Pseudo-classes are not supported for the **\<popup>** component and its child components, including, **\<popup>**, **\<dialog>**, **\<menu>**, **\<option>**, and **\<picker>**.
152
153
154## Precompiled Styles
155
156Precompilation 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**.
157
158- The following **index.less** file is changed from **index.css**.
159  ```less
160  /* index.less */
161  /* Define a variable. */
162  @colorBackground: #000000;
163  .container {
164      background-color: @colorBackground; /* Use the variable defined in the .less file. */
165  }
166  ```
167
168- 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**.
169  ```scss
170  /* style.scss */
171  /* Define a variable. */
172  $colorBackground: #000000;
173  ```
174
175  Reference the precompiled style file in **index.scss**:
176
177  ```scss
178  /* index.scss */
179  /* Import style.scss. */
180  @import '../../common/style.scss';
181  .container {
182      background-color: $colorBackground; /* Use the variable defined in style.scss. */
183  }
184  ```
185
186  > **NOTE**
187  >
188  > Place precompiled style files in the **common** directory.
189
190## CSS Style Inheritance<sup>6+</sup>
191
192CSS 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:
193
194- font-family
195
196- font-weight
197
198- font-size
199
200- font-style
201
202- text-align
203
204- line-height
205
206- letter-spacing
207
208- color
209
210- visibility
211