这些推荐使用的CSS命名和书写规范,你应该知道

来源 | https://juejin.im/post/6844903849245802510
选择器的命名规范
1、模块化命名
与布局相关的样式以“g”为开头。如“g-content”和“g-header”; 与挂钩相关的样式以“j”为开头。如“j-open”和“j-request”; 与元件相关的样式以“m”为开头。如“m-dropMenu”和“m-slider”; 与状态相关的样式以“s”为开头。如“s-current”和“s-selected”; 与工具相关的样式以“u”为开头。如“u-clearfix”和“u-ellipsis”。
2、选择器皆为小写形式
.g-first-header{line-height: 16px;}
.g-FirstHeader{line-height: 16px;}
3、每个选择器独占一列
.g-first-header,.g-second-header-1 > .g-second-header-2{border: 2px solid #C3C3C3;}
.g-first-header, .g-second-header-1>.g-second-header-2{border: 2px solid #C3C3C3;}
4、避开HTML标记
.g-content .g-item{flex-basis: 20%;}
.g-content li{flex-basis: 20%;}
5、少用ID
.g-special-content{height: 100px;width: 300px;}
#special-content{height: 100px;width: 300px;}
属性的书写规范
1、按顺序排列属性
位置:bottom、float、display、left、position、right、top和z-index等; 大小:height、margin、padding和width等; 版式:color、font、letter-spacing、line-height和text-align等; 背景:background等; 其它:animation和transition等。
2、缩写属性
.test-selector-1{padding: 3px 5px;}
.test-selector-1{padding-top: 3px;padding-right: 5px;padding-bottom: 3px;padding-left: 5px;}
3、去除小数开头的0
.test-selector-2{font-size: .5em;}
.test-selector-2{font-size: 0.5em;}
4、缩写十六进位值
.test-selector-3{background-color: #0b0;}
.test-selector-3{background-color: #00bb00;}
5、合理使用引号
.test-selector-4{font-family: "Microsoft YaHei", "微软正黑体", "\5b8b\4f53";}
.test-selector-4{font-family: "Microsoft YaHei", 微软正黑体, \5b8b\4f53;}
.test-selector-5{background-image: url(“../Images/BacPic.png”);}
.test-selector-5{background-image: url(../Images/BackPic.png);}
6、避开 !important
.test-selector-6 .test-selector-7{font-size: 16px;}.test-selector-6 .test-selector-7 .test-selector-8{font-size: 14px;}
.test-selector-6 .test-selector-7{font-size: 16px;}.test-selector-8{font-size: 14px !important;}
7、规范注释
/* 这是第一段注释文字。 */// 这是第二段注释文字。
/*这里是一段注释文字。*///这是第二段注释文字。
/*** 这里是一段注释文字。* 这是第二段注释文字。*/
/***这里是一段注释文字。*这是第二段注释文字。*/
/*** @name: 文件名;* @description: 描述文字;* @author: 张三、李四;* @update: 2018年12月19日。*/
/*** @name:文件名;* @description:描述文字;* @author:张三、李四;* @update:2018年12月19日。*/
8、将标准属性置于底部
.test-selector-9{opacity: 0;-webkit-transition: opacity 3s;-moz-transition: opacity 3s;-ms-transition: opacity 3s;-o-transition: opacity 3s;transition: opacity 3s;}
.test-selector-9{opacity: 0;-webkit-transition: opacity 3s;transition: opacity 3s;-moz-transition: opacity 3s;-ms-transition: opacity 3s;-o-transition: opacity 3s;}
9、注意标点符号
.test-selector-10{opacity: .5;}
.test-selector-10{opacity:.5}
10、样式块间留一空行
.test-selector-11{opacity: 0.5;}.test-selector-12{font-size: 16px;}.test-selector-13{overflow: hidden;}
.test-selector-11{opacity: 0.5;}.test-selector-12{font-size: 16px;}.test-selector-13{overflow: hidden;}
11、将过长的内容折为若干列
.test-selector-14{linear-gradient(135deg, deeppink 25%, transparent 25%) - 50px 0,linear-gradient(225deg, deeppink 25%, transparent 25%) - 50px 0,linear-gradient(315deg, deeppink 25%, transparent 25%),linear-gradient(45deg, deeppink 25%, transparent 25%);}
.test-selector-14{linear-gradient(135deg, deeppink 25%, transparent 25%) - 50px 0, linear-gradient(225deg, deeppink 25%, transparent 25%) - 50px 0, linear-gradient(315deg, deeppink 25%, transparent 25%), linear-gradient(45deg, deeppink 25%, transparent 25%);}
12、避开CSS Hack
13、减少使用影响性能的属性

评论
