js-framework-syntax-css.md 4.9 KB
Newer Older
E
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# CSS


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.


## Style Import

CSS files can be imported using the **\@import** statement. This facilitates module management and code reuse.


## Style Declaration

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.

1. Internal style: The **style** and **class** attributes can be used to specify the component style. Sample code:
E
ester.zhou 已提交
17 18

   ```html
E
ester.zhou 已提交
19 20 21 22 23 24
   <!-- index.hml -->
   <div class="container">
     <text style="color: red">Hello World</text>
   </div>
   ```

E
ester.zhou 已提交
25 26

   ```css
E
ester.zhou 已提交
27 28 29 30 31 32 33
   /* index.css */
   .container {
     justify-content: center;
   }
   ```

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**.
E
ester.zhou 已提交
34 35

   ```css
E
ester.zhou 已提交
36 37 38 39 40 41
   /* style.css */
   .title {
     font-size: 50px;
   }
   ```

E
ester.zhou 已提交
42 43

   ```css
E
ester.zhou 已提交
44 45 46 47 48 49 50 51 52 53 54 55
   /* index.css */
   @import '../../common/style.css';
   .container {
     justify-content: center;
   }
   ```


## Selectors

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

E
ester.zhou 已提交
56 57 58 59 60
| Selector   | Example                   | Description                                 |
| ------ | --------------------- | ------------------------------------- |
| .class | .container            | Selects all components whose **class** is **container**.            |
| \#id   | \#titleId             | Selects all components whose **id** is **titleId**.                 |
| ,      | .title, .content | Selects all components whose **class** is **title** or **content**.|
E
ester.zhou 已提交
61 62 63 64

Example:


E
ester.zhou 已提交
65
```html
E
ester.zhou 已提交
66 67 68 69 70 71 72 73 74 75
<!-- Pagelayoutexample.hml -->
<div id="containerId" class="container">
  <text id="titleId" class="title">Title</text>
  <div class="content">
    <text id="contentId">Content</text>
  </div>
</div>
```


E
ester.zhou 已提交
76
```css
E
ester.zhou 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/* Page style xxx.css */
/* Set the style for the components whose class is title. */
.title {
  font-size: 30px;
}
/* Set the style for the components 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;
}

```


## Pseudo-classes

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected elements.

E
ester.zhou 已提交
98 99 100 101 102


| Name      | Available Components                               | Description                                      |
| -------- | ----------------------------------- | ---------------------------------------- |
| :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.|
E
ester.zhou 已提交
103 104 105 106 107
| :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.|

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


E
ester.zhou 已提交
108
```html
E
ester.zhou 已提交
109 110 111 112 113 114 115
<!-- index.hml -->
<div class="container">
  <input type="button" class="button" value="Button"></input>
</div>
```


E
ester.zhou 已提交
116
```css
E
ester.zhou 已提交
117 118 119 120 121 122 123 124 125 126 127 128
/* index.css */
.button:active {
  background-color: #888888;/* After the button is activated, the background color is changed to #888888. */
}
```


## Precompiled Styles

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

- The following **index.less** file is changed from **index.css**.
E
ester.zhou 已提交
129 130

  ```css
E
ester.zhou 已提交
131 132 133 134 135 136 137 138 139
  /* index.less */
  /* Define a variable. */
  @colorBackground: #000000;
  .container {
      background-color: @colorBackground; /* Use the variable defined in the .less file. */
  }
  ```

- 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**.
E
ester.zhou 已提交
140 141

  ```css
E
ester.zhou 已提交
142 143 144 145 146 147 148
  /* style.scss */
  /* Define a variable. */
  $colorBackground: #000000;
  ```

  Reference the precompiled style file in **index.scss**:

E
ester.zhou 已提交
149 150

  ```css
E
ester.zhou 已提交
151 152 153 154 155 156 157 158
  /* index.scss */
  /* Import style.scss. */
  @import '../../common/style.scss';
  .container {
    background-color: $colorBackground; /* Use the variable defined in style.scss. */
  }
  ```

E
ester.zhou 已提交
159

E
ester.zhou 已提交
160 161 162
  >  **NOTE**
  >
  >  Place precompiled style files in the **common** directory.