js-framework-syntax-css.md 8.4 KB
Newer Older
G
ge-yafang 已提交
1
# CSS
Z
zengyawen 已提交
2 3


G
ge-yafang 已提交
4
Cascading 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.
Z
zengyawen 已提交
5

G
ge-yafang 已提交
6
## Size Unit
Z
zengyawen 已提交
7

G
ge-yafang 已提交
8 9 10
1. Logical px set by <length>:
   1. 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.
   2. 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.
Z
zengyawen 已提交
11

G
ge-yafang 已提交
12
2. 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.
Z
zengyawen 已提交
13 14


G
ge-yafang 已提交
15
## Style Import
Z
zengyawen 已提交
16

G
ge-yafang 已提交
17
CSS files can be imported using the @import statement. This facilitates module management and code reuse.
Z
zengyawen 已提交
18 19


G
ge-yafang 已提交
20
## Style Declaration
Z
zengyawen 已提交
21

G
ge-yafang 已提交
22
The .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.
Z
zengyawen 已提交
23

G
ge-yafang 已提交
24 25 26 27 28 29 30 31
1. Internal style: The style and class attributes can be used to specify the component style. Example:
  
   ```
   <!-- index.hml -->
   <div class="container">
     <text style="color: red">Hello World</text>
   </div>
   ```
Z
zengyawen 已提交
32

G
ge-yafang 已提交
33 34 35 36 37 38 39
   
   ```
   /* index.css */
   .container {
     justify-content: center;
   }
   ```
Z
zengyawen 已提交
40

G
ge-yafang 已提交
41 42 43 44 45 46 47 48
2. 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.
  
   ```
   /* style.css */
   .title {
     font-size: 50px;
   }
   ```
Z
zengyawen 已提交
49

G
ge-yafang 已提交
50 51 52 53 54 55 56 57
   
   ```
   /* index.css */
   @import '../../common/style.css';
   .container {
     justify-content: center;
   }
   ```
Z
zengyawen 已提交
58

G
ge-yafang 已提交
59 60

## Selectors
Z
zengyawen 已提交
61 62 63

A CSS selector is used to select elements for which styles need to be added to. The following table lists the supported selectors.

G
ge-yafang 已提交
64 65 66 67 68 69 70
| Selector | Example | Description |
| -------- | -------- | -------- |
| .class | .container | Selects all components whose class is container. |
| \#id | \#titleId | Selects all components whose id is titleId. |
| tag | text | Selects all  &lt;text&gt; components. |
| , | .title, .content | Selects all components whose class is title or content. |
| \#id .class tag | \#containerId .content text | Selects all grandchild  &lt;text&gt; 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. |
Z
zengyawen 已提交
71 72 73

The following is an example:

G
ge-yafang 已提交
74

Z
zengyawen 已提交
75 76 77 78 79 80 81 82 83 84
```
<!-- Page layoutxxx.hml -->
<div id="containerId" class="container">
  <text id="titleId" class="title">Title</text>
  <div class="content">
    <text id="contentId">Content</text>
  </div>
</div>
```

G
ge-yafang 已提交
85

Z
zengyawen 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
```
/* Page style xxx.css */
/* Set the style for all <div> components. */
div {
  flex-direction: column;
}
/* Set the style for the component whose class is title. */
.title {
  font-size: 30px;
}
/* Set the style for the component whose id is contentId. */
#contentId {
  font-size: 20px;
}
/* Set padding for all components of the title or content class to 5 px. */
.title, .content {
  padding: 5px;
}
/* Set the style for all texts of components whose class is container.
 */
.container text {
  color: #007dff;
}
/* Set the style for direct descendant texts of components whose class is container.
*/
.container > text {
  color: #fa2a2d;
}
```

The above style works as follows:

G
ge-yafang 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
![en-us_image_0000001267607889](figures/en-us_image_0000001267607889.png)

In 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).)

## Selector Specificity

The 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.)

When multiple selectors point to the same element, their priorities are as follows (in descending order): inline style > id > class > tag.


## Pseudo-classes

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, :disabled can be used to select the element whose disabled attribute is true.

In 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.

  | Pseudo-class | Available Components | Description | 
| -------- | -------- | -------- |
| :disabled | Components that support the disabled attribute | Selects the element whose disabled attribute is changed to true (unavailable for animation attributes). | 
| :focus | Components that support the focusable attribute | Selects the element that takes focus (unavailable for animation attributes). | 
| :active | Components that support the click event<br/> | Selects the element activated by a user. For example, a pressed button or a selected tab-bar (unavailable for animation attributes).  |
| :waiting | button | Selects the element whose waiting attribute is true (unavailable for animation attributes). | 
| :checked | input[type="checkbox", type="radio"], and switch | Selects the element whose checked attribute is true (unavailable for animation attributes).  |
| :hover<sup>6+</sup> | Components that support the mouseover event | Selects the element that the cursor is on. | 

The following is an example for you to use the :active pseudo-class to control the style when a user presses the button.

Z
zengyawen 已提交
146 147 148 149 150 151 152 153

```
<!-- index.hml -->
<div class="container">
  <input type="button" class="button" value="Button"></input>
</div>
```

G
ge-yafang 已提交
154

Z
zengyawen 已提交
155 156 157 158 159 160 161
```
/* index.css */
.button:active {
  background-color: #888888;/* After the button is activated, the background color is changed to #888888. */
}
```

G
ge-yafang 已提交
162 163 164
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> Pseudo-classes are not supported for the <popup> component and its child components, including, <dialog>, <menu>, <option>, and <picker>.

Z
zengyawen 已提交
165

G
ge-yafang 已提交
166
## Precompiled Styles
Z
zengyawen 已提交
167

G
ge-yafang 已提交
168
Precompilation 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.
Z
zengyawen 已提交
169

G
ge-yafang 已提交
170 171 172 173 174 175 176 177 178 179
- The following index.less file is changed from index.css.
  
  ```
  /* index.less */
  /* Define a variable. */
  @colorBackground: #000000;
  .container {
    background-color: @colorBackground; /* Use the variable defined in the .less file. */
  }
  ```
Z
zengyawen 已提交
180

G
ge-yafang 已提交
181 182 183 184 185 186 187
- 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.
  
  ```
  /* style.scss */
  /* Define a variable. */
  $colorBackground: #000000;
  ```
Z
zengyawen 已提交
188

G
ge-yafang 已提交
189
  Reference the precompiled style file in index.scss:
Z
zengyawen 已提交
190

G
ge-yafang 已提交
191 192 193 194 195 196 197 198 199
  
  ```
  /* index.scss */
  /* Import style.scss. */
  @import '../../common/style.scss';
  .container {
    background-color: $colorBackground; /* Use the variable defined in style.scss. */
  }
  ```
Z
zengyawen 已提交
200

G
ge-yafang 已提交
201 202 203
  
  > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
  > Place precompiled style files in the common directory.
Z
zengyawen 已提交
204

G
ge-yafang 已提交
205
## CSS Style Inheritance<sup>6+</sup>
Z
zengyawen 已提交
206

G
ge-yafang 已提交
207
CSS 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:
Z
zengyawen 已提交
208

G
ge-yafang 已提交
209
- font-family
Z
zengyawen 已提交
210

G
ge-yafang 已提交
211
- font-weight
Z
zengyawen 已提交
212

G
ge-yafang 已提交
213
- font-size
Z
zengyawen 已提交
214

G
ge-yafang 已提交
215 216 217 218 219 220 221
- font-style

- text-align

- line-height

- letter-spacing
Z
zengyawen 已提交
222

G
ge-yafang 已提交
223
- color
Z
zengyawen 已提交
224

G
ge-yafang 已提交
225
- visibility