CSS样式表是一个序列通用字符集,传输和存储过程中,这些字符必须由支持 US-ASCII(例如 UTF-8, ISO 8859-x, SHIFT JIS 等)字符编码方式编译
When a style sheet is embedded in another document, such as in the STYLE element or "style" attribute of HTML, the style sheet shares the character encoding of the whole document.
当样式出现在其它文档,如 HTML 的 STYLE 标签或标签属性 "style",样式的编码由文档的决定。
When a style sheet resides in a separate file, user agents must observe the following priorities when determining a style sheet's character encoding (from highest priority to lowest):
An HTTP "charset" parameter in a "Content-Type" field (or similar parameters in other protocols)BOM and/or [@charset ]()or other metadata from the linking mechanism (if any)charset of referring style sheet or document (if any)Assume UTF-8
文档外链样式表的编码可以由以下各项按照由高到低的优先级顺序决定:
Authors using an [@charset ]() rule must place the rule at the very beginning of the style sheet, preceded by no characters. (If a byte order mark is appropriate for the encoding used, it may precede the [@charset ]() rule.)
[@charset ]() must be written literally, i.e., the 10 characters '[@charset ]() "' (lowercase, no backslash escapes), followed by the encoding name, followed by '";'.
推荐:
@charset "UTF-8";.jdc{}
不推荐:
/** * @desc File Info * @author Author Name * @date 2015-10-10 */ /* @charset规则不在文件首行首个字符开始 */@charset "UTF-8";.jdc{}
@CHARSET "UTF-8";/* @charset规则没有用小写 */.jdc{}
/* 无@charset规则 */.jdc{}
更多关于样式编码:CSS style sheet representation
样式书写一般有两种:一种是紧凑格式 (Compact)
.jdc{ display: block;width: 50px;}
一种是展开格式(Expanded)
.jdc{ display: block; width: 50px;}
团队约定
统一使用展开格式书写样式
样式选择器,属性名,属性值关键字全部使用小写字母书写,属性字符串允许使用大小写。
/* 推荐 */.jdc{ display:block;} /* 不推荐 */.JDC{ DISPLAY:BLOCK;}
/* 推荐 */.jdc {}.jdc li {}.jdc li p{}/* 不推荐 */*{}#jdc {}.jdc div{}
统一使用四个空格进行代码缩进,使得各编辑器表现一致(各编辑器有相关配置)
.jdc { width: 100%; height: 100%;}
每个属性声明末尾都要加分号;
.jdc { width: 100%; height: 100%;}
左括号与类名之间一个空格,冒号与属性值之间一个空格
推荐:
.jdc { width: 100%; }
不推荐:
.jdc{ width:100%;}
逗号分隔的取值,逗号之后一个空格
推荐:
.jdc { box-shadow: 1px 1px 1px #333, 2px 2px 2px #ccc;}
不推荐:
.jdc { box-shadow: 1px 1px 1px #333,2px 2px 2px #ccc;}
为单个css选择器或新申明开启新行
推荐:
.jdc, .jdc_logo, .jdc_hd { color: #ff0;}.nav{ color: #fff;}
不推荐:
.jdc,jdc_logo,.jdc_hd { color: #ff0;}.nav{ color: #fff;}
颜色值 rgb() rgba() hsl() hsla() rect() 中不需有空格,且取值不要带有不必要的 0
推荐:
.jdc { color: rgba(255,255,255,.5);}
不推荐:
.jdc { color: rgba( 255, 255, 255, 0.5 );}
属性值十六进制数值能用简写的尽量用简写
推荐:
.jdc { color: #fff;}
不推荐:
.jdc { color: #ffffff;}
不要为 0 指明单位
推荐:
.jdc { margin: 0 10px;}
不推荐:
.jdc { margin: 0px 10px;}
css属性值需要用到引号时,统一使用单引号
/* 推荐 */.jdc { font-family: 'Hiragino Sans GB';}/* 不推荐 */.jdc { font-family: "Hiragino Sans GB";}
建议遵循以下顺序:
.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;}
mozilla官方属性顺序推荐
CSS3 浏览器私有前缀在前,标准前缀在后
.jdc { -webkit-border-radius: 10px; -moz-border-radius: 10px; -o-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px;}
更多关于浏览器私有前辍写法:#Vendor-specific extensions
Google Code Guide