编码规范
CSS样式表是一个序列通用字符集,传输和存储过程中,这些字符必须由支持 US-ASCII(例如 UTF-8, ISO 8859-x, SHIFT JIS 等)字符编码方式编译
实用高于完美
尽量遵循HTML标准和语义,但是不应该以浪费实用性作为代价;
任何时候都要用尽量小的复杂度和尽量少的标签来解决问题。
代码格式化
统一使用展开格式书写样式
.thn{display: block;width: 50px;}
代码大小写
样式选择器,属性名,属性值关键字全部使用小写字母书写,属性字符串允许使用大小写。
/* 推荐 */.thn {display: block;}/* 多个单词中间用减号分割 */.thn-header {display: block;}/* 不推荐 */.thn {DISPLAY:BLOCK;}
选择器
- 尽量少用通用选择器
* - 不使用 ID 选择器
- 不使用无具体语义定义的标签选择器
/* 推荐 */.thn {}.thn li {}.thn li p{}/* 不推荐 */*{}#thn {}.thn div{}
分号
每个属性声明末尾都要加分号
.thn {width: 100%;height: 100%;}
代码易读性
- 左括号与类名之间一个空格,冒号与属性值之间一个空格
/* 推荐 */.thn {width: 100%;}/* 不推荐 */.thn{width:100%;}
- 逗号分隔的取值,逗号之后一个空格
/* 推荐 */.thn {box-shadow: 1px 1px 1px #333, 2px 2px 2px #ccc;}/* 不推荐 */.thn {box-shadow: 1px 1px 1px #333,2px 2px 2px #ccc;}
- 为单个css选择器或新申明开启新行
/* 推荐 */.thn,.thn_logo,.thn_hd {color: #ff0;}.nav {color: #fff;}/* 不推荐 */.thn,.thn_logo,.thn_hd {color: #ff0;}.nav {color: #fff;}
- 颜色值
rgb()rgba()hsl()hsla()rect()中加空格,且取值不要带有不必要的 0
/* 推荐 */.thn {color: rgba(255, 255, 255, .5);}/* 不推荐 */.thn {color: rgba(255,255,255,.5);}
- 属性值十六进制数值能用简写的尽量用简写
/* 推荐 */.thn {color: #fff;}/* 不推荐 */.thn {color: #ffffff;}
- 不要为
0指明单位
/* 推荐 */.thn {margin: 0 10px}/* 不推荐 */.thn {margin: 0px 10px}
- 不允许有空规则
/* 不推荐 */.thn {/* width: 100px; */}
属性值引号
css属性值需要用到引号时,统一使用双引号
/* 推荐 */.thn {font-family: "Hiragino Sans GB";}/* 不推荐 */.thn {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 …
.thn {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 浏览器私有前缀在前,标准前缀在后
.thn {-webkit-border-radius: 10px;-moz-border-radius: 10px;-o-border-radius: 10px;-ms-border-radius: 10px;border-radius: 10px;}
更多关于浏览器私有前辍写法:#Vendor-specific extensions
注释规范
- 注释以字符
/*开始,以字符*/结束 - 注释不能嵌套
/*Comment Text*/
单行注释
注释内容第一个字符和最后一个字符都是一个空格字符,单独占一行,行与行之间相隔一行
- 推荐
/* Comment Text */.thn {}/* Comment Text */.thn {}
- 不推荐
/*Comment Text*/.thn {display: block;}.thn {display: block;/*Comment Text*/}
文件信息注释
在样式文件编码声明 @charset 语句下面注明页面名称、作者、创建日期等信息
@charset "UTF-8";/*** @desc File Info* @author Author Name* @date 2015-10-10*/
更多关于CSS注释:#Comments
