CSS
文件使用无 BOM
的 UTF-8
编码。4
个空格作为一个缩进层级,不容许使用 2
个空格 或 tab
字符。选择器
与 {
之间必须包含空格。.selector {
}
属性名
与以后的 :
之间不容许包含空格, :
与 属性值
之间必须包含空格。margin: 0;
列表型属性值
书写在单行时,,
后必须跟一个空格。font-family: Arial, sans-serif;
120
个字符,除非单行不可分割。空格
处或 ,
后换行,建议按逻辑分组。/* 不一样属性值按逻辑分组 */ background: transparent url(aVeryVeryVeryLongUrlIsPlacedHere) no-repeat 0 0; /* 可重复屡次的属性,每次重复一行 */ background-image: url(aVeryVeryVeryLongUrlIsPlacedHere) url(anotherVeryVeryVeryLongUrlIsPlacedHere); /* 相似函数的属性值能够根据函数调用的缩进进行 */ background-image: -webkit-gradient( linear, left bottom, left top, color-stop(0.04, rgb(88,94,124)), color-stop(0.52, rgb(115,123,162)) );
/* good */ .post, .page, .comment { line-height: 1.5; } /* bad */ .post, .page, .comment { line-height: 1.5; }
>
、+
、~
选择器的两边各保留一个空格。/* good */ main > nav { padding: 10px; } label + input { margin-left: 5px; } input:checked ~ button { background-color: #69C; } /* bad */ main>nav { padding: 10px; } label+input { margin-left: 5px; } input:checked~button { background-color: #69C; }
不容许使用单引号,不容许不使用引号。css
1.6 属性git
/* good */ .selector { margin: 0; padding: 0; } /* bad */ .selector { margin: 0; padding: 0; }
2.通用github
2.1选择器web
id
、class
选择器添加类型选择器进行限定。在性能和维护性上,都有必定的影响。windows
/* good */ #error, .danger-message { font-color: #c00; } /* bad */ dialog#error, p.danger-message { font-color: #c00; }
3
级,位置靠后的限定条件应尽量精确。
/* good */ .post { font: 12px/1.5 arial, sans-serif; } /* bad */ .post { font-family: arial, sans-serif; font-size: 12px; line-height: 1.5; }
border
/ margin
/ padding
等缩写时,应注意隐含值对实际数值的影响,确实须要设置多个方向的值时才使用缩写。
position
/ top
/ right
/ bottom
/ left
/ float
/ display
/ overflow
等border
/ margin
/ padding
/ width
/ height
等font
/ line-height
/ text-align
/ word-wrap
等background
/ color
/ transition
/ list-style
等另外,若是包含 content
属性,应放在最前面。浏览器
clear
或触发 BFC
的方式进行 clearfix
。尽可能不使用增长空标签的方式。
触发 BFC 的方式不少,常见的有:编辑器
另需注意,对已经触发 BFC 的元素不须要再进行 clearfix。ide
!important
声明。!important
定义样式。
2.6 z-index函数
z-index
进行分层,对文档流外绝对定位元素的视觉层级关系进行管理。 同层的多个元素,如多个由用户输入触发的 Dialog,在该层级内使用相同的 z-index
或递增 z-index
。布局
建议每层包含100个 z-index
来容纳足够的元素,若是每层元素较多,能够调整这个数值。
z-index
指定为 999999
。!important
,将 z-index
指定为 2147483647
。第三方环境对于开发者来讲彻底不可控。在第三方环境下的元素,为了保证元素不被其页面其余样式定义覆盖,须要采用此作法。
文本类型的内容可能在选择器、属性值等内容中。
0
。/* good */ panel { opacity: .8; } /* bad */ panel { opacity: 0.8; }
url()
函数中的路径不加引号。body { background: url(bg.png); }
url()
函数中的绝对路径可省去协议名。body { background: url(//baidu.com/img/bg.png) no-repeat 0 0; }
0
时须省略单位。 (也只有长度单位可省)#rrggbb
。不容许使用 rgb()
。使用 rgba()
时每一个逗号后必须保留一个空格。
/* good */ .success { box-shadow: 0 0 2px rgba(0, 128, 0, .3); border-color: #008000; } /* bad */ .success { box-shadow: 0 0 2px rgba(0,128,0,.3); border-color: rgb(0, 128, 0); }
/* good */ .success { background-color: #aca; } /* bad */ .success { background-color: #aaccaa; }
/* good */ .success { color: #90ee90; } /* bad */ .success { color: lightgreen; }
2D 位置初始值为 0% 0%
,但在只有一个方向的值时,另外一个方向的值会被解析为 center。为避免理解上的困扰,应同时给出两个方向的值
/* good */ body { background-position: center top; /* 50% 0% */ } /* bad */ body { background-position: top; /* 50% 0% */ }
font-family
属性中的字体族名称应使用字体的英文 Family Name
,其中若有空格,须放置在引号中。font-family
按「西文字体在前、中文字体在后」、「效果佳 (质量高/更能知足需求) 的字体在前、效果通常的字体在后」的顺序编写,最后必须指定一个通用字体族( serif
/ sans-serif
)。/* Display according to platform */ .article { font-family: Arial, sans-serif; } /* Specific for most platforms */ h1 { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", "WenQuanYi Micro Hei", "Microsoft YaHei", sans-serif; }
font-family
不区分大小写,但在同一个项目中,一样的 Family Name
大小写必须统一。
12px
。
normal
外的 font-style
。其余平台也应慎用。
font-weight
属性必须使用数值方式描述。CSS 的字重分 100 – 900 共九档,但目前受字体自己质量和浏览器的限制,实际上支持 400
和 700
两档,分别等价于关键词 normal
和 bold
。
在 <700
时通常匹配字体的 Regular 字重,>=700
时匹配 Bold 字重。
/* good */ h1 { font-weight: 700; } /* bad */ h1 { font-weight: bold; }
line-height
在定义文本段落时,应使用数值。将 line-height
设置为数值,浏览器会基于当前元素设置的 font-size
进行再次计算。在不一样字号的文本段落组合中,能达到较为温馨的行间间隔效果,避免在每一个设置了 font-size
都须要设置 line-height
。
当 line-height
用于控制垂直居中时,仍是应该设置成与容器高度一致。
transition
时应指定 transition-property
。
Media Query
不得单独编排,必须与相关的规则一块儿定义。/* Good */ /* header styles */ @media (...) { /* header styles */ } /* main styles */ @media (...) { /* main styles */ } /* footer styles */ @media (...) { /* footer styles */ } /* Bad */ /* header styles */ /* main styles */ /* footer styles */ @media (...) { /* header styles */ /* main styles */ /* footer styles */ }
标准属性放在最后,按冒号对齐方便阅读,也便于在编辑器内进行多行编辑。