CSS 规范

语法

  • 从外部文件加载CSS,尽可能减少文件数。加载标签放在HEAD 部分。
  • 用 LINK 标签加载,永远不要用@import。
  • 加载样式表
    <link rel="stylesheet" type="text/css" href="myStylesheet.css" />
    
  • 不要用内联式样式,这样会很难追踪样式规则
      <p style="font-size: 12px; color: #FFFFFF">This is poor form, I say</p>
    
  • 使用 reset.css,normalize.css等重置游览器默认样式。
  • 定义样式的时候,对样式在页面只出现一次的元素用id,其他的用class。
  • 理解 层叠和选择器的优先度。
  • 避免使用性能开销大的CSS选择器。例如:* 通配符选择器,div#myid,table.results。后两种应该直接用#myid和.results代替。
  • 用两个空格来代替制表符(tab) -- 保证能在所有环境下获得一致的展现。
  • 为选择器分组时,将单独的选择器单独放在一行。
  • 为了获得更准确的错误报告,每条声明都应该独占一行。 所有声明语句都应当以';'结尾。最后一条声明语句后';'可省略。
  • 对于以逗号分隔的属性值,每个逗号后面都应该插入一个空格(例如,box-shadow)。
  • 省略小于 1 的小数前面的 0 (例如,.5 代替 0.5;-.5px 代替 -0.5px)。
  • 使用简写小写形式的十六进制值,例如,写 #fff 不要写 #FFFFFF。
  • 为选择器中的属性添加双引号,例如,input[type="text"]
  • 避免为 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;
}

使用CSS reset

  • reset文件用于重设各个浏览器的默认样式方案,解决其引起的耦合问题。
  • 也可使用 .clearfix 清除浮动

属性顺序

相关的属性声明应当归为一组,并按照下面的顺序排列:

  • Positioning
  • Box model
  • Typographic
  • Visual

    由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位。盒模型排在第二位,因为它决定了组件的尺寸和位置。

    其他属性只是影响组件的内部(inside)或者是不影响前两组属性,因此排在后面。
.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

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

媒体查询的位置

将媒体查询放在尽可能相关规则的附近。不要将他们打包放在一个单一样式文件中或者放在文档底部。如果你把他们分开了,将来只会被大家遗忘。下面给出一个典型的实例。

.element { ... }
.element-avatar { ... }
.element-selected { ... }

@media (min-width: 480px) {
  .element { ...}
  .element-avatar { ... }
  .element-selected { ... }
}

简写属性声明

在需要显示地设置所有值的情况下,应当尽量限制使用简写形式的属性声明。常见的滥用简写属性声明的情况如下: padding margin font background border 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 { … }
}

注释

代码是由人编写并维护的。请确保你的代码能够自描述、注释良好并且易于他人理解。好的代码注释能够传达上下文关系和代码目的。不要简单地重申组件或 class 名称。 对于较长的注释,务必书写完整的句子;对于一般性注解,可以书写简洁的短语。

class 命名

class 名称中只能出现小写字符和破折号。破折号应当用于相关 class 的命名(类似于命名空间)(例如,.btn 和 .btn-danger)。避免过度任意的简写。比如.s 不能表达任何意思。

class 名称应当尽可能短,并且意义明确。 使用有意义的名称。使用有组织的或目的明确的名称。 基于最近的父 class 或基本(base) class 作为新 class 的前缀。 使用 .js-* class 来标识行为(与样式相对),并且不要将这些 class 包含到 CSS 文件中。

/* Bad example */
.t { ... }
.red { ... }
.header { ... }

/* Good example */
.tweet { ... }
.important { ... }
.tweet-header { ... }

选择器

  • 通用元素使用 class ,这样利于渲染性能的优化。
  • 经常出现的组件,避免使用属性选择器(如,[class^="..."])。浏览器的性能会受到这些因素的影响。
  • 选择器要尽可能短,建议不要超过 3 个。
  • 只有在必要的时候才将 class 限制在最近的父元素内。
/* Bad example */
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }

/* Good example */
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }

代码组织

以组件为单位组织代码段。 制定一致的注释规范。 使用一致的空白符将代码分隔成块,这样利于扫描较大的文档。 如果使用了多个 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 { ... }

响应式解决方案

弹性图片

img {
  max-width: 100%;
  height: auto;
  width: auto\9; /* ie8 */
}

自适应嵌入媒体

.video embed, .video object, .video iframe {
  width: 100%;
  height: auto;
}

禁用iPhone字体自适应功能:

html {
  -webkit-text-size-adjust: none;
}
© QIANSR all right reserved,powered by Gitbook该文件修订时间: 2015-11-25 19:09:55