Css
Css
Css
Css
命名
- id 采用驼峰式命名
- 类名使用小写字母,以中划线分隔
- scss 中的变量、函数、混合、placeholder 采用驼峰式命名
代码格式化
使用展开格式书写样式
1
2
3
4
.jdc {
display: block;
width: 50px;
}
代码大小写
样式选择器,属性名,属性值关键字全部使用小写字母书写,属性字符串允许使用大小写。
1
2
3
.jdc {
display:block;
}
选择器
- 尽量少用通用选择器 *
- 尽量不使用 ID 选择器
- 不使用无具体语义定义的标签选择器
1
2
3
.jdc {}
.jdc li {}
.jdc li p {}
代码缩进
使用四个空格进行代码缩进
1
2
3
4
.jdc {
width: 100%;
height: 100%;
}
分号
1
2
3
4
.jdc {
width: 100%;
height: 100%;
}
代码易读性
左括号与类名之间一个空格,冒号与属性值之间一个空格
1
2
3
.jdc {
width: 100%;
}
属性名 与之后的 : 之间不允许包含空格, : 与 属性值 之间必须包含空格
1
margin: 0;
逗号分隔的取值,逗号之后一个空格
1
2
3
.jdc {
box-shadow: 1px 1px 1px #333, 2px 2px 2px #ccc;
}
为单个css选择器或新申明开启新行
1
2
3
4
5
6
7
8
.jdc,
.jdc_logo,
.jdc_hd {
color: #ff0;
}
.nav{
color: #fff;
}
颜色值 rgb() rgba() hsl() hsla() rect() 中不需有空格,且取值不要带有不必要的 0
1
2
3
.jdc {
color: rgba(255,255,255,.5);
}
属性值十六进制数值能用简写的尽量用简写
1
2
3
.jdc {
color: #fff;
}
不要为 0 指明单位
1
2
3
.jdc {
margin: 0 10px;
}
属性值引号
css属性值需要用到引号时,使用单引号
1
2
3
.jdc {
font-family: 'Hiragino Sans GB';
}
属性书写顺序
建议遵循以下顺序:
- 布局定位属性:display / position / float / clear / visibility / overflow
- 自身属性:width / height / margin / padding / border / background
- 文本属性:color / font / text-decoration / text-align / vertical-align / white- space / break-word
- 其他属性(CSS3):content / cursor / border-radius / box-shadow / text-shadow / background:linear-gradient …
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.jdc {
display: block;
position: relative;
float: left;
width: 100px;
height: 100px;
margin: 0 10px;
padding: 20px 0;
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
color: #333;
background: rgba(0,0,0,.5);
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-o-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}
CSS3浏览器私有前缀写法
CSS3 浏览器私有前缀在前,标准前缀在后
1
2
3
4
5
6
7
.jdc {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-o-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
}
注释规范
单行注释
注释内容第一个字符和最后一个字符都是一个空格字符,单独占一行,行与行之间相隔一行
1
2
3
4
/* Comment Text */
.jdc {
display: block;
}
模块注释
注释内容第一个字符和最后一个字符都是一个空格字符,/* 与 模块信息描述占一行,多个横线分隔符-与*/占一行,行与行之间相隔两行
1
2
3
4
5
6
7
8
/* Module A
---------------------------------------------------------------- */
.mod_a {}
/* Module B
---------------------------------------------------------------- */
.mod_b {}
文件信息注释
在样式文件编码声明 @charset 语句下面注明页面名称、作者、创建日期等信息
1
2
3
4
5
6
@charset "UTF-8";
/**
* @desc File Info
* @author Author Name
* @date 2021-06-18
*/
重置样式
移动端
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
* {
outline: 0;
margin: 0;
padding: 0;
vertical-align: baseline;
-webkit-tap-highlight-color: transparent;
}
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;
}
PC端
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
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;
}
本文由作者按照 CC BY 4.0 进行授权