• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CSS语法参考<a name="ZH-CN_TOPIC_0000001163932188"></a>
2
3CSS是描述HML页面结构的样式语言。所有组件均存在系统默认样式,也可在页面CSS样式文件中对组件、页面自定义不同的样式。
4
5## 尺寸单位<a name="zh-cn_topic_0000001127125112_section81638230913"></a>
6
71.  逻辑像素px(文档中以<length\>表示):
8    1.  默认屏幕具有的逻辑宽度为720px(配置见[配置文件](js-framework-js-tag.md)中的window小节),实际显示时会将页面布局缩放至屏幕实际宽度,如100px在实际宽度为1440物理像素的屏幕上,实际渲染为200物理像素(从720px向1440物理像素,所有尺寸放大2倍)。
9    2.  额外配置autoDesignWidth为true时(配置见[配置文件](js-framework-js-tag.md)中的window小节),逻辑像素px将按照屏幕密度进行缩放,如100px在屏幕密度为3的设备上,实际渲染为300物理像素。应用需要适配多种设备时,建议采用此方法。
10
112.  百分比(文档中以<percentage\>表示):表示该组件占父组件尺寸的百分比,如组件的width设置为50%,代表其宽度为父组件的50%。
12
13## 样式导入<a name="zh-cn_topic_0000001127125112_section890312411592"></a>
14
15为了模块化管理和代码复用,CSS样式文件支持 @import 语句,导入css文件。
16
17## 声明样式<a name="zh-cn_topic_0000001127125112_section197695604215"></a>
18
19每个页面目录下存在一个与布局hml文件同名的css文件,用来描述该hml页面中组件的样式,决定组件应该如何显示。
20
211.  内部样式,支持使用style、class属性来控制组件的样式。例如:
22
23    ```
24    <!-- index.hml -->
25    <div class="container">
26      <text style="color: red">Hello World</text>
27    </div>
28    ```
29
30    ```
31    /* index.css */
32    .container {
33      justify-content: center;
34    }
35    ```
36
372.  文件导入,合并外部样式文件。例如,在common目录中定义样式文件style.css,并在index.css文件首行中进行导入:
38
39    ```
40    /* style.css */
41    .title {
42      font-size: 50px;
43    }
44    ```
45
46    ```
47    /* index.css */
48    @import '../../common/style.css';
49    .container {
50      justify-content: center;
51    }
52    ```
53
54
55## 选择器<a name="zh-cn_topic_0000001127125112_section381741144310"></a>
56
57css选择器用于选择需要添加样式的元素,支持的选择器如下表所示:
58
59<a name="zh-cn_topic_0000001127125112_table8917183413489"></a>
60<table><thead align="left"><tr id="zh-cn_topic_0000001127125112_row1291753412489"><th class="cellrowborder" valign="top" width="15.341534153415342%" id="mcps1.1.4.1.1"><p id="zh-cn_topic_0000001127125112_p291763474818"><a name="zh-cn_topic_0000001127125112_p291763474818"></a><a name="zh-cn_topic_0000001127125112_p291763474818"></a>选择器</p>
61</th>
62<th class="cellrowborder" valign="top" width="24.852485248524854%" id="mcps1.1.4.1.2"><p id="zh-cn_topic_0000001127125112_p1491783412488"><a name="zh-cn_topic_0000001127125112_p1491783412488"></a><a name="zh-cn_topic_0000001127125112_p1491783412488"></a>样例</p>
63</th>
64<th class="cellrowborder" valign="top" width="59.805980598059804%" id="mcps1.1.4.1.3"><p id="zh-cn_topic_0000001127125112_p10917173404818"><a name="zh-cn_topic_0000001127125112_p10917173404818"></a><a name="zh-cn_topic_0000001127125112_p10917173404818"></a>样例描述</p>
65</th>
66</tr>
67</thead>
68<tbody><tr id="zh-cn_topic_0000001127125112_row2091743484810"><td class="cellrowborder" valign="top" width="15.341534153415342%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p1891783411483"><a name="zh-cn_topic_0000001127125112_p1891783411483"></a><a name="zh-cn_topic_0000001127125112_p1891783411483"></a>.class</p>
69</td>
70<td class="cellrowborder" valign="top" width="24.852485248524854%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p169174348484"><a name="zh-cn_topic_0000001127125112_p169174348484"></a><a name="zh-cn_topic_0000001127125112_p169174348484"></a>.container</p>
71</td>
72<td class="cellrowborder" valign="top" width="59.805980598059804%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p991733474814"><a name="zh-cn_topic_0000001127125112_p991733474814"></a><a name="zh-cn_topic_0000001127125112_p991733474814"></a>用于选择class="container"的组件。</p>
73</td>
74</tr>
75<tr id="zh-cn_topic_0000001127125112_row189178343481"><td class="cellrowborder" valign="top" width="15.341534153415342%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p5917634124817"><a name="zh-cn_topic_0000001127125112_p5917634124817"></a><a name="zh-cn_topic_0000001127125112_p5917634124817"></a>#id</p>
76</td>
77<td class="cellrowborder" valign="top" width="24.852485248524854%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p89231120496"><a name="zh-cn_topic_0000001127125112_p89231120496"></a><a name="zh-cn_topic_0000001127125112_p89231120496"></a>#titleId</p>
78</td>
79<td class="cellrowborder" valign="top" width="59.805980598059804%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p132391416144917"><a name="zh-cn_topic_0000001127125112_p132391416144917"></a><a name="zh-cn_topic_0000001127125112_p132391416144917"></a>用于选择id="titleId"的组件。</p>
80</td>
81</tr>
82<tr id="zh-cn_topic_0000001127125112_row12917934154811"><td class="cellrowborder" valign="top" width="15.341534153415342%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p1041721913499"><a name="zh-cn_topic_0000001127125112_p1041721913499"></a><a name="zh-cn_topic_0000001127125112_p1041721913499"></a>tag</p>
83</td>
84<td class="cellrowborder" valign="top" width="24.852485248524854%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p4917203424816"><a name="zh-cn_topic_0000001127125112_p4917203424816"></a><a name="zh-cn_topic_0000001127125112_p4917203424816"></a>text</p>
85</td>
86<td class="cellrowborder" valign="top" width="59.805980598059804%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p174847365491"><a name="zh-cn_topic_0000001127125112_p174847365491"></a><a name="zh-cn_topic_0000001127125112_p174847365491"></a>用于选择text组件。</p>
87</td>
88</tr>
89<tr id="zh-cn_topic_0000001127125112_row99179340488"><td class="cellrowborder" valign="top" width="15.341534153415342%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p19918123424819"><a name="zh-cn_topic_0000001127125112_p19918123424819"></a><a name="zh-cn_topic_0000001127125112_p19918123424819"></a>,</p>
90</td>
91<td class="cellrowborder" valign="top" width="24.852485248524854%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p7918734154817"><a name="zh-cn_topic_0000001127125112_p7918734154817"></a><a name="zh-cn_topic_0000001127125112_p7918734154817"></a>.title, .content</p>
92</td>
93<td class="cellrowborder" valign="top" width="59.805980598059804%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p9240140154916"><a name="zh-cn_topic_0000001127125112_p9240140154916"></a><a name="zh-cn_topic_0000001127125112_p9240140154916"></a>用于选择class="title"和class="content"的组件。</p>
94</td>
95</tr>
96<tr id="zh-cn_topic_0000001127125112_row1091833418485"><td class="cellrowborder" valign="top" width="15.341534153415342%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p27744448492"><a name="zh-cn_topic_0000001127125112_p27744448492"></a><a name="zh-cn_topic_0000001127125112_p27744448492"></a>#id .class tag</p>
97</td>
98<td class="cellrowborder" valign="top" width="24.852485248524854%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p0402948154911"><a name="zh-cn_topic_0000001127125112_p0402948154911"></a><a name="zh-cn_topic_0000001127125112_p0402948154911"></a>#containerId .content text</p>
99</td>
100<td class="cellrowborder" valign="top" width="59.805980598059804%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p19830323135016"><a name="zh-cn_topic_0000001127125112_p19830323135016"></a><a name="zh-cn_topic_0000001127125112_p19830323135016"></a>非严格父子关系的后代选择器,选择具有id="containerId"作为祖先元素,class="content"作为次级祖先元素的所有text组件。如需使用严格的父子关系,可以使用“&gt;”代替空格,如:#containerId&gt;.content。</p>
101</td>
102</tr>
103</tbody>
104</table>
105
106示例:
107
108```
109<!-- 页面布局xxx.hml -->
110<div id="containerId" class="container">
111  <text id="titleId" class="title">标题</text>
112  <div class="content">
113    <text id="contentId">内容</text>
114  </div>
115</div>
116```
117
118```
119/* 页面样式xxx.css */
120/* 对所有div组件设置样式 */
121div {
122  flex-direction: column;
123}
124/* 对class="title"的组件设置样式 */
125.title {
126  font-size: 30px;
127}
128/* 对id="contentId"的组件设置样式 */
129#contentId {
130  font-size: 20px;
131}
132/* 对所有class="title"以及class="content"的组件都设置padding为5px */
133.title, .content {
134  padding: 5px;
135}
136/* 对class="container"的组件下的所有text设置样式 */
137.container text {
138  color: #007dff;
139}
140/* 对class="container"的组件下的直接后代text设置样式 */
141.container > text {
142  color: #fa2a2d;
143}
144```
145
146以上样式运行效果如下:
147
148![](figures/sample_css.png)
149
150其中“.container text”将“标题”和“内容”设置为蓝色,而“.container \> text”直接后代选择器将“标题”设置为红色。2者优先级相同,但直接后代选择器声明顺序靠后,将前者样式覆盖(优先级计算见[选择器优先级](#zh-cn_topic_0000001127125112_section1460102134415))。
151
152## 选择器优先级<a name="zh-cn_topic_0000001127125112_section1460102134415"></a>
153
154选择器的优先级计算规则与w3c规则保持一致(只支持:内联样式,id,class,tag,后代和直接后代),其中内联样式为在元素style属性中声明的样式。
155
156当多条选择器声明匹配到同一元素时,各类选择器优先级由高到低顺序为:内联样式 \> id \> class \> tag。
157
158## 伪类<a name="zh-cn_topic_0000001127125112_section633010213458"></a>
159
160css伪类是选择器中的关键字,用于指定要选择元素的特殊状态。例如,:disabled状态可以用来设置元素的disabled属性变为true时的样式。
161
162除了单个伪类之外,还支持伪类的组合,例如,:focus:checked状态可以用来设置元素的focus属性和checked属性同时为true时的样式。支持的单个伪类如下表所示,按照优先级降序排列:
163
164<a name="zh-cn_topic_0000001127125112_table584491515455"></a>
165<table><thead align="left"><tr id="zh-cn_topic_0000001127125112_row3844171516452"><th class="cellrowborder" valign="top" width="11.441144114411442%" id="mcps1.1.4.1.1"><p id="zh-cn_topic_0000001127125112_p3844151594510"><a name="zh-cn_topic_0000001127125112_p3844151594510"></a><a name="zh-cn_topic_0000001127125112_p3844151594510"></a>名称</p>
166</th>
167<th class="cellrowborder" valign="top" width="26.142614261426147%" id="mcps1.1.4.1.2"><p id="zh-cn_topic_0000001127125112_p11844131512458"><a name="zh-cn_topic_0000001127125112_p11844131512458"></a><a name="zh-cn_topic_0000001127125112_p11844131512458"></a>支持组件</p>
168</th>
169<th class="cellrowborder" valign="top" width="62.41624162416242%" id="mcps1.1.4.1.3"><p id="zh-cn_topic_0000001127125112_p78441615184511"><a name="zh-cn_topic_0000001127125112_p78441615184511"></a><a name="zh-cn_topic_0000001127125112_p78441615184511"></a>描述</p>
170</th>
171</tr>
172</thead>
173<tbody><tr id="zh-cn_topic_0000001127125112_row104936528343"><td class="cellrowborder" valign="top" width="11.441144114411442%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p2235059193413"><a name="zh-cn_topic_0000001127125112_p2235059193413"></a><a name="zh-cn_topic_0000001127125112_p2235059193413"></a>:disabled</p>
174</td>
175<td class="cellrowborder" valign="top" width="26.142614261426147%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p1323545943417"><a name="zh-cn_topic_0000001127125112_p1323545943417"></a><a name="zh-cn_topic_0000001127125112_p1323545943417"></a>支持disabled属性的组件</p>
176</td>
177<td class="cellrowborder" valign="top" width="62.41624162416242%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p10235155912344"><a name="zh-cn_topic_0000001127125112_p10235155912344"></a><a name="zh-cn_topic_0000001127125112_p10235155912344"></a>表示disabled属性变为true时的元素(不支持动画样式的设置)。</p>
178</td>
179</tr>
180<tr id="zh-cn_topic_0000001127125112_row0844121554510"><td class="cellrowborder" valign="top" width="11.441144114411442%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p138441015144512"><a name="zh-cn_topic_0000001127125112_p138441015144512"></a><a name="zh-cn_topic_0000001127125112_p138441015144512"></a>:active</p>
181</td>
182<td class="cellrowborder" valign="top" width="26.142614261426147%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p1184441511457"><a name="zh-cn_topic_0000001127125112_p1184441511457"></a><a name="zh-cn_topic_0000001127125112_p1184441511457"></a><span id="zh-cn_topic_0000001127125112_ph32264934610"><a name="zh-cn_topic_0000001127125112_ph32264934610"></a><a name="zh-cn_topic_0000001127125112_ph32264934610"></a>支持click事件的组件</span></p>
183</td>
184<td class="cellrowborder" valign="top" width="62.41624162416242%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p1384471516454"><a name="zh-cn_topic_0000001127125112_p1384471516454"></a><a name="zh-cn_topic_0000001127125112_p1384471516454"></a>表示被用户激活的元素,如:被用户按下的按钮<span id="zh-cn_topic_0000001127125112_ph1265519710112"><a name="zh-cn_topic_0000001127125112_ph1265519710112"></a><a name="zh-cn_topic_0000001127125112_ph1265519710112"></a>、被激活的tab-bar页签(不支持动画样式的设置)</span>。</p>
185</td>
186</tr>
187<tr id="zh-cn_topic_0000001127125112_row12925340193519"><td class="cellrowborder" valign="top" width="11.441144114411442%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p134731642153511"><a name="zh-cn_topic_0000001127125112_p134731642153511"></a><a name="zh-cn_topic_0000001127125112_p134731642153511"></a>:waiting</p>
188</td>
189<td class="cellrowborder" valign="top" width="26.142614261426147%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p114732420358"><a name="zh-cn_topic_0000001127125112_p114732420358"></a><a name="zh-cn_topic_0000001127125112_p114732420358"></a>button</p>
190</td>
191<td class="cellrowborder" valign="top" width="62.41624162416242%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p2473742143511"><a name="zh-cn_topic_0000001127125112_p2473742143511"></a><a name="zh-cn_topic_0000001127125112_p2473742143511"></a>表示waiting属性为true的元素<span id="zh-cn_topic_0000001127125112_ph3323185819110"><a name="zh-cn_topic_0000001127125112_ph3323185819110"></a><a name="zh-cn_topic_0000001127125112_ph3323185819110"></a>(不支持动画样式的设置)</span>。</p>
192</td>
193</tr>
194<tr id="zh-cn_topic_0000001127125112_row1884431514452"><td class="cellrowborder" valign="top" width="11.441144114411442%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001127125112_p7844161512452"><a name="zh-cn_topic_0000001127125112_p7844161512452"></a><a name="zh-cn_topic_0000001127125112_p7844161512452"></a>:checked</p>
195</td>
196<td class="cellrowborder" valign="top" width="26.142614261426147%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001127125112_p198451215174516"><a name="zh-cn_topic_0000001127125112_p198451215174516"></a><a name="zh-cn_topic_0000001127125112_p198451215174516"></a>input[type="checkbox"、type="radio"]<span id="zh-cn_topic_0000001127125112_ph55837371112"><a name="zh-cn_topic_0000001127125112_ph55837371112"></a><a name="zh-cn_topic_0000001127125112_ph55837371112"></a>、 switch</span></p>
197</td>
198<td class="cellrowborder" valign="top" width="62.41624162416242%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001127125112_p9845415164512"><a name="zh-cn_topic_0000001127125112_p9845415164512"></a><a name="zh-cn_topic_0000001127125112_p9845415164512"></a>表示checked属性为true的元素<span id="zh-cn_topic_0000001127125112_ph1476627729"><a name="zh-cn_topic_0000001127125112_ph1476627729"></a><a name="zh-cn_topic_0000001127125112_ph1476627729"></a>(不支持动画样式的设置)</span>。</p>
199</td>
200</tr>
201</tbody>
202</table>
203
204伪类示例如下,设置按钮的:active伪类可以控制被用户按下时的样式:
205
206```
207<!-- index.hml -->
208<div class="container">
209  <input type="button" class="button" value="Button"></input>
210</div>
211```
212
213```
214/* index.css */
215.button:active {
216  background-color: #888888;/*按钮被激活时,背景颜色变为#888888 */
217}
218```
219
220>![](../public_sys-resources/icon-note.gif) **说明:**
221>针对弹窗类组件及其子元素不支持伪类效果,包括popup、dialog、menu、option、picker
222
223## 样式预编译<a name="zh-cn_topic_0000001127125112_section1690162216454"></a>
224
225预编译提供了利用特有语法生成css的程序,可以提供变量、运算等功能,令开发者更便捷地定义组件样式,目前支持less、sass和scss的预编译。使用样式预编译时,需要将原css文件后缀改为less、sass或scss,如index.css改为index.lessindex.sassindex.scss226
227-   当前文件使用样式预编译,例如将原index.css改为index.less228
229    ```
230    /* index.less */
231    /* 定义变量 */
232    @colorBackground: #000000;
233    .container {
234      background-color: @colorBackground; /* 使用当前less文件中定义的变量 */
235    }
236    ```
237
238
239-   引用预编译文件,例如common中存在style.scss文件,将原index.css改为index.scss,并引入style.scss240
241    ```
242    /* style.scss */
243    /* 定义变量 */
244    $colorBackground: #000000;
245    ```
246
247index.scss中引用:
248
249    ```
250    /* index.scss */
251    /* 引入外部scss文件 */
252    @import '../../common/style.scss';
253    .container {
254      background-color: $colorBackground; /* 使用style.scss中定义的变量 */
255    }
256    ```
257
258    >![](../public_sys-resources/icon-note.gif) **说明:**
259    >引用的预编译文件建议放在common目录进行管理。
260
261
262## CSS样式继承<sup>6+</sup><a name="zh-cn_topic_0000001127125112_section11758912154910"></a>
263
264css样式继承提供了子节点继承父节点样式的能力,继承下来的样式在多选择器样式匹配的场景下,优先级排最低,当前支持以下样式的继承:
265
266-   font-family
267-   font-weight
268-   font-size
269-   font-style
270-   text-align
271-   line-height
272-   letter-spacing
273-   color
274-   visibility
275
276