js-service-widget-syntax-css.md 3.3 KB
Newer Older
E
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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.


## Size Unit

1. Logical px set by **\<length>**:
   1. The default logical width of a service widget is 150 px. The page will be scaled to fit the actual width of the screen. For example, on a screen with the actual width of 300 physical px, 100 px is rendered on 200 physical px, with the size doubled from 150 px to 300 px.
   2. If you set **autoDesignWidth** to **true**, the logical pixels 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.

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.


## 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. Example:
E
ester.zhou 已提交
26

E
ester.zhou 已提交
27 28 29 30 31 32 33
   ```html
   <!-- index.hml -->
   <div class="container">
     <text style="color: red">Hello World</text>
   </div>
   ```

E
ester.zhou 已提交
34

E
ester.zhou 已提交
35 36 37 38 39 40 41 42
   ```css
   /* 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 已提交
43

E
ester.zhou 已提交
44 45 46 47 48 49 50
   ```css
   /* style.css */
   .title {
     font-size: 50px;
   }
   ```

E
ester.zhou 已提交
51

E
ester.zhou 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
   ```css
   /* 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 已提交
65 66
| Selector   | Example        | Description                     |
| ------ | ---------- | ------------------------- |
E
ester.zhou 已提交
67
| .class | .container | Selects all components whose **class** is **container**.|
E
ester.zhou 已提交
68
| \#id   | \#titleId  | Selects all components whose **id** is **titleId**.     |
E
ester.zhou 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

Example:


```html
<!-- Pagelayoutexample.hml -->
<div id="containerId" class="container">
  <text id="titleId" class="title">Title</text>
  <div class="content">
    <text id="contentId">Content</text>
  </div>
</div>
```


```css
/* Pagestyleexample.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;
}
```


## Selector Specificity

The specificity rule of the selectors complies with the W3C rule, which is only available for inline styles, **id**, and **class**. (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**.