CSS语法规范

来源:互联网 发布:淘宝海外代购哪家靠谱 编辑:程序博客网 时间:2024/05/23 14:58
一.语法

  1.两个空格代替一个制表符(tab)。

  2.为选择器分组时,将单独的选择器放在一行。

  3.为增加代码可读性,每个声明的左花括号前加一个空格。

  4.声明块的右花括号单独成行。

  5.每条声明语句的:后应插入一个空格。

  6.为了获得更准确的错误报告,每条声明都应该独占一行。

  7.所有声明语句都应当以分号结尾。最后一条声明语句后面的分号是可选的,但是,如果省略这个分号,你的代码可能更易出错。

  8.对于以逗号分隔的属性值,每个逗号后面都应该插入一个空格(例如,box-shadow)。

  9.不要在 rgb()、rgba()、hsl()、hsla() 或 rect() 值的内部的逗号后面插入空格。这样利于从多个属性值(既加逗号也加空格)中区分多个颜色值(只加逗号,不加空格)。

  10.对于属性值或颜色参数,省略小于 1 的小数前面的 0 (例如,.5 代替 0.5;-.5px 代替 -0.5px)。

  11.十六进制值应该全部小写,例如,#fff。在扫描文档时,小写字符易于分辨,因为他们的形式更易于区分。

  12.尽量使用简写形式的十六进制值,例如,用 #fff 代替 #ffffff。

  13.为选择器中的属性添加双引号,例如,input[type="text"]。

  14.避免为 0 值指定单位,例如,用 margin: 0; 代替 margin: 0px;。

/* Bad CSS */.selector, .selector-secondary, .selector[type=text] {  padding:15px;  margin:0px 0px 15px;  background-color:rgba(0, 0, 0, 0.5);  box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF}/* Good CSS */.selector,.selector-secondary,.selector[type="text"] {  padding: 15px;  margin-bottom: 15px;  background-color: rgba(0,0,0,.5);  box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff;}
二.声明顺序

  1.Positioning

  2.Box model

  3.Typographic

  4.Visual

.declaration-order {  /* Positioning */  position: absolute;  top: 0;  right: 0;  bottom: 0;  left: 0;  z-index: 100;  /* Box-model */  display: block;  float: right;  width: 100px;  height: 100px;  /* Typography */  font: normal 13px "Helvetica Neue", sans-serif;  line-height: 1.5;  color: #333;  text-align: center;  /* Visual */  background-color: #f5f5f5;  border: 1px solid #e5e5e5;  border-radius: 3px;  /* Misc */  opacity: 1;}
三.不要使用@import

  与 <link> 标签相比,@import 指令要慢很多,不光增加了额外的请求次数,还会导致不可预料的问题。

四.媒体查询(Media query)的位置

  将媒体查询放在尽可能相关规则的附近。不要将他们打包放在一个单一样式文件中或者放在文档底部。如果你把他们分开了,将来只会被大家遗忘。下面给出一个典型的实例。
.element { ... }.element-avatar { ... }.element-selected { ... }@media (min-width: 480px) {  .element { ...}  .element-avatar { ... }  .element-selected { ... }}
五.带前缀的属性

  当使用特定厂商的带有前缀的属性时,通过缩进的方式,让每个属性的值在垂直方向对齐,这样便于多行编辑。

/* Prefixed properties */.selector {  -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);          box-shadow: 0 1px 2px rgba(0,0,0,.15);}
六.单行规则声明

  对于只包含一条声明的样式,为了易读性和便于快速编辑,建议将语句放在同一行。对于带有多条声明的样式,还是应当将声明分为多行。
/* Single declarations on one line */.span1 { width: 60px; }.span2 { width: 140px; }.span3 { width: 220px; }/* Multiple declarations, one per line */.sprite {  display: inline-block;  width: 16px;  height: 15px;  background-image: url(../img/sprite.png);}.icon           { background-position: 0 0; }.icon-home      { background-position: 0 -20px; }.icon-account   { background-position: 0 -40px; }

七.简写形式的属性声明

  在需要显示地设置所有值的情况下,应当尽量限制使用简写形式的属性声明。
    1.padding
    2.margin
    3.font
    4.background
    5.border
    6.border-radius
/* Bad example */.element {  margin: 0 0 10px;  background: red;  background: url("image.jpg");  border-radius: 3px 3px 0 0;}/* Good example */.element {  margin-bottom: 10px;  background-color: red;  background-image: url("image.jpg");  border-top-left-radius: 3px;  border-top-right-radius: 3px;}
八.Less 和 Sass 中的嵌套

  避免非必要的嵌套。这是因为虽然你可以使用嵌套,但是并不意味着应该使用嵌套。只有在必须将样式限制在父元素内(也就是后代选择器),并且存在多个需要嵌套的元素时才使用嵌套。
// Without nesting.table > thead > tr > th { … }.table > thead > tr > td { … }// With nesting.table > thead > tr {  > th { … }  > td { … }}
九.注释

  对于较长的注释,务必书写完整的句子;对于一般性注解,可以书写简洁的短语。
/* Bad example *//* Modal header */.modal-header {  ...}/* Good example *//* Wrapping element for .modal-title and .modal-close */.modal-header {  ...}
十.class命名

  1.class 名称中只能出现小写字符和破折号(dashe)(不是下划线,也不是驼峰命名法)。破折号应当用于相关 class 的命名(类似于命名空间)(例如,.btn 和 .btn-danger)。

  2.避免过度任意的简写。.btn 代表 button,但是 .s 不能表达任何意思。

  3.class 名称应当尽可能短,并且意义明确。

  4.使用有意义的名称。使用有组织的或目的明确的名称,不要使用表现形式(presentational)的名称。

  5.基于最近的父 class 或基本(base) class 作为新 class 的前缀。
  
  6.使用 .js-* class 来标识行为(与样式相对),并且不要将这些 class 包含到 CSS 文件中。
/* Bad example */.t { ... }.red { ... }.header { ... }/* Good example */.tweet { ... }.important { ... }.tweet-header { ... }

十一.选择器

  1.对于通用元素使用 class ,这样利于渲染性能的优化。

  2.对于经常出现的组件,避免使用属性选择器(例如,[class^="..."])。浏览器的性能会受到这些因素的影响。

  3.选择器要尽可能短,并且尽量限制组成选择器的元素个数,建议不要超过 3 。

  4.只有在必要的时候才将 class 限制在最近的父元素内(也就是后代选择器)(例如,不使用带前缀的 class 时 -- 前缀类似于命名空间)。
/* Bad example */span { ... }.page-container #stream .stream-item .tweet .tweet-header .username { ... }.avatar { ... }/* Good example */.avatar { ... }.tweet-header .username { ... }.tweet .avatar { ... }

十二.代码组织

  1.以组件为单位组织代码段。
  
  2.制定一致的注释规范。

  3.使用一致的空白符将代码分隔成块,这样利于扫描较大的文档。

  4.如果使用了多个 CSS 文件,将其按照组件而非页面的形式分拆,因为页面会被重组,而组件只会被移动。
/* * Component section heading */.element { ... }/* * Component section heading * * Sometimes you need to include optional context for the entire component. Do that up here if it's important enough. */.element { ... }/* Contextual sub-component or modifer */.element-heading { ... }






0 0
原创粉丝点击