css属性规范

7/14/2021 CSS

# css 属性书写顺序

  1. 布局定位属性: display / position / float / clear / visibility / overflow
  2. 自身属性: width / height / margin / padding / border / background
  3. 文本属性: color / font / text-decoration / text-align / vertical-align / white-space / break-word
  4. 其他属性(css3): content / cursor / border-radius / box-shadow / text-shadow

# 属性值引号

css 属性需要用到引号时,统一使用单引号

/* 推荐 */
body {
    font-family: 'Hiragino Sans GB';
}
1
2
3
4

# 选择器

  • 尽量少用通用选择器 *
  • 不使用 ID 选择器
  • 不使用无具体语义定义的标签选择器

# 代码选择器

样式选择器,属性名,属性值关键字全部使用小写字母书写。

/* 推荐 */
.box {
    display: block;
}

/* 不推荐 */
.box {
    display: BLOCK;
}
1
2
3
4
5
6
7
8
9

# 其他注意的

  • 每个属性声明末尾加分号
  • 左括号与类名之间一个空格,冒号与属性值之间一个空格
  • 为单个 css 选择器或新申明开启新行
  • 属性值十六进制数值能用简写的尽量用简写
  • 不要为 0 设置单位

# 重置样式

移动端

* {
    -webkit-tap-highlight-color: transparent;
    outline: 0;
    margin: 0;
    padding: 0;
    vertical-align: baseline;
}
body,
h1,
h2,
h3,
h4,
h5,
h6,
hr,
p,
blockquote,
dl,
dt,
dd,
ul,
ol,
li,
pre,
form,
fieldset,
legend,
button,
input,
textarea,
th,
td {
    margin: 0;
    padding: 0;
    vertical-align: baseline;
}
img {
    border: 0 none;
    vertical-align: top;
}
i,
em {
    font-style: normal;
}
ol,
ul {
    list-style: none;
}
input,
select,
button,
h1,
h2,
h3,
h4,
h5,
h6 {
    font-size: 100%;
    font-family: inherit;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
a {
    text-decoration: none;
    color: #666;
}
body {
    margin: 0 auto;
    min-width: 320px;
    max-width: 640px;
    height: 100%;
    font-size: 14px;
    font-family: -apple-system, Helvetica, sans-serif;
    line-height: 1.5;
    color: #666;
    -webkit-text-size-adjust: 100% !important;
    text-size-adjust: 100% !important;
}
input[type='text'],
textarea {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

pc 端

html,
body,
div,
h1,
h2,
h3,
h4,
h5,
h6,
p,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
input,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
textarea,
article,
aside,
audio,
canvas,
figure,
footer,
header,
mark,
menu,
nav,
section,
time,
video {
    margin: 0;
    padding: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
    font-size: 100%;
    font-weight: normal;
}
article,
aside,
dialog,
figure,
footer,
header,
hgroup,
nav,
section,
blockquote {
    display: block;
}
ul,
ol {
    list-style: none;
}
img {
    border: 0 none;
    vertical-align: top;
}
blockquote,
q {
    quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
strong,
em,
i {
    font-style: normal;
    font-weight: normal;
}
ins {
    text-decoration: underline;
}
del {
    text-decoration: line-through;
}
mark {
    background: none;
}
input::-ms-clear {
    display: none !important;
}
body {
    font: 12px/1.5 \5fae\8f6f\96c5\9ed1, \5b8b\4f53, 'Hiragino Sans GB', STHeiti, 'WenQuanYi Micro Hei', 'Droid Sans Fallback',
        SimSun, sans-serif;
    background: #fff;
}
a {
    text-decoration: none;
    color: #333;
}
a:hover {
    text-decoration: underline;
}
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
上次更新: 7/14/2021, 5:37:59 PM