Skip to content

Commit 12a673a

Browse files
author
calvin
committed
Feat: 增加样式规范
1 parent 4f4ca0c commit 12a673a

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# 样式规范参考
2+
3+
### 1. 基本原则
4+
5+
- 尽量避免使用`important`
6+
- 尽量避免在各种地方给元素定高定宽,尽量使用继承和计算
7+
- **尽量避免使用第三方框架的选择器去定义样式,因为第三方框架如果升级会破坏掉期望效果或者很有可能你会把原有样式覆盖掉,切记!**
8+
- 在less里面,选择器的嵌套尽量不要超过3层
9+
- 在各个组件的特定less里面,记得在外面套上`:host`,以避免污染全局
10+
- 界面上的各元素尽量保持对齐
11+
- 避免滥用`z-index`,使用整百来管理z-index:100, 200, 300, 400, 500, 600, 700,每个数字代表的就是对应的层级,如:100代表上浮一层,200上浮两层。理论上同一个试图,层级不应该过多,建议不超过4层。
12+
- 当元素需要浮动的时候,要给它的父容器清除浮动,请使用`.clearfix`工具类
13+
14+
### 2. 间距
15+
16+
由于使用了ng-zorro,我们可以统一采用ng-zorro的规范,最小值(xs)为8px,中间值(md)为16px,最大值(lg)为24px,所有相邻的视图元素之间必须要有合适的间距,不允许在UI界面上出现紧挨在一块的组件。
17+
18+
- [间距说明](https://ng-alain.com/theme/tools/zh#%E9%97%B4%E8%B7%9D)
19+
- 相应的一些工具类参考[ng-alain](https://ng-alain.com/theme/tools/zh),尽量使用工具类,避免重复定义。
20+
- [栅格系统](https://ng.ant.design/components/grid/zh)
21+
22+
在写样式的时候,需要注意ng-zorro和ng-alain的基本组件其实都已经提供了默认的间距,避免覆盖掉这些默认行为。
23+
24+
25+
26+
### 3. 通用
27+
28+
- [**强制**] 属性定义必须另起一行。
29+
30+
示例:
31+
32+
```less
33+
/* good */
34+
.selector {
35+
margin: 0;
36+
padding: 0;
37+
}
38+
39+
/* bad */
40+
.selector { margin: 0; padding: 0; }
41+
```
42+
43+
- [**强制**] 属性定义后必须以分号结尾。
44+
45+
示例:
46+
47+
```less
48+
/* good */
49+
.selector {
50+
margin: 0;
51+
}
52+
53+
/* bad */
54+
.selector {
55+
margin: 0
56+
}
57+
```
58+
59+
60+
61+
- [**强制**] 当一个 rule 包含多个 selector 时,每个选择器声明必须独占一行。
62+
63+
示例:
64+
65+
```less
66+
/* good */
67+
.post,
68+
.page,
69+
.comment {
70+
line-height: 1.5;
71+
}
72+
73+
/* bad */
74+
.post, .page, .comment {
75+
line-height: 1.5;
76+
}
77+
```
78+
79+
- [**强制**] `>``+``~` 选择器的两边各保留一个空格。
80+
81+
示例:
82+
83+
```less
84+
/* good */
85+
main > nav {
86+
padding: 10px;
87+
}
88+
89+
label + input {
90+
margin-left: 5px;
91+
}
92+
93+
input:checked ~ button {
94+
background-color: #69C;
95+
}
96+
97+
/* bad */
98+
main>nav {
99+
padding: 10px;
100+
}
101+
102+
label+input {
103+
margin-left: 5px;
104+
}
105+
106+
input:checked~button {
107+
background-color: #69C;
108+
}
109+
```
110+
111+
- [**强制**] 如无必要,不得为 `id``class` 选择器添加类型选择器进行限定。
112+
113+
解释:
114+
115+
在性能和维护性上,都有一定的影响。
116+
117+
示例:
118+
119+
```less
120+
/* good */
121+
#error,
122+
.danger-message {
123+
font-color: #c00;
124+
}
125+
126+
/* bad */
127+
dialog#error,
128+
p.danger-message {
129+
font-color: #c00;
130+
}
131+
```
132+
133+
- [**建议**] 在可以使用缩写的情况下,尽量使用属性缩写。
134+
135+
示例:
136+
137+
```less
138+
/* good */
139+
.post {
140+
font: 12px/1.5 arial, sans-serif;
141+
}
142+
143+
/* bad */
144+
.post {
145+
font-family: arial, sans-serif;
146+
font-size: 12px;
147+
line-height: 1.5;
148+
}
149+
```
150+
151+
- [**强制**] 当数值为 0 - 1 之间的小数时,省略整数部分的 `0`。
152+
153+
示例:
154+
155+
```less
156+
/* good */
157+
panel {
158+
opacity: .8
159+
}
160+
161+
/* bad */
162+
panel {
163+
opacity: 0.8
164+
}
165+
```
166+
167+
- [**强制**] `url()` 函数中的路径不加引号。
168+
169+
示例:
170+
171+
```less
172+
body {
173+
background: url(bg.png);
174+
}
175+
```
176+
177+
- [**建议**] `url()` 函数中的绝对路径可省去协议名。
178+
179+
示例:
180+
181+
```less
182+
body {
183+
background: url(//baidu.com/img/bg.png) no-repeat 0 0;
184+
}
185+
```
186+
187+
- [**强制**] 长度为 `0` 时须省略单位。 (也只有长度单位可省)
188+
189+
示例:
190+
191+
```less
192+
/* good */
193+
body {
194+
padding: 0 5px;
195+
}
196+
197+
/* bad */
198+
body {
199+
padding: 0px 5px;
200+
}
201+
```
202+
203+
- [**建议**] `line-height` 在定义文本段落时,应使用数值。
204+
205+
解释:
206+
207+
将 line-height 设置为数值,浏览器会基于当前元素设置的 font-size 进行再次计算。在不同字号的文本段落组合中,能达到较为舒适的行间间隔效果,避免在每个设置了 font-size 都需要设置 line-height。
208+
209+
当 line-height 用于控制垂直居中时,还是应该设置成与容器高度一致。
210+
211+
```less
212+
.container {
213+
line-height: 1.5;
214+
}
215+
```

0 commit comments

Comments
 (0)