CSS语法规范!!!!!!!!!!!!!!!!!!!!!!!

来源:互联网 发布:vb和java哪个好 编辑:程序博客网 时间:2024/05/16 10:24
一.语法

  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;。

[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Bad CSS */  
  2. .selector, .selector-secondary, .selector[type=text] {  
  3.   padding:15px;  
  4.   margin:0px 0px 15px;  
  5.   background-color:rgba(0000.5);  
  6.   box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF  
  7. }  
  8.   
  9. /* Good CSS */  
  10. .selector,  
  11. .selector-secondary,  
  12. .selector[type="text"] {  
  13.   padding15px;  
  14.   margin-bottom15px;  
  15.   background-color: rgba(0,0,0,.5);  
  16.   box-shadow: 0 1px 2px #cccinset 0 1px 0 #fff;  
  17. }  
二.声明顺序

  1.Positioning

  2.Box model

  3.Typographic

  4.Visual

[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. .declaration-order {  
  2.   /* Positioning */  
  3.   positionabsolute;  
  4.   top: 0;  
  5.   right: 0;  
  6.   bottom: 0;  
  7.   left: 0;  
  8.   z-index100;  
  9.   
  10.   /* Box-model */  
  11.   displayblock;  
  12.   floatright;  
  13.   width100px;  
  14.   height100px;  
  15.   
  16.   /* Typography */  
  17.   fontnormal 13px "Helvetica Neue"sans-serif;  
  18.   line-height1.5;  
  19.   color#333;  
  20.   text-aligncenter;  
  21.   
  22.   /* Visual */  
  23.   background-color#f5f5f5;  
  24.   border1px solid #e5e5e5;  
  25.   border-radius: 3px;  
  26.   
  27.   /* Misc */  
  28.   opacity: 1;  
  29. }  
三.不要使用@import

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

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

  将媒体查询放在尽可能相关规则的附近。不要将他们打包放在一个单一样式文件中或者放在文档底部。如果你把他们分开了,将来只会被大家遗忘。下面给出一个典型的实例。
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. .element { ... }  
  2. .element-avatar { ... }  
  3. .element-selected { ... }  
  4.   
  5. @media (min-width480px) {  
  6.   .element { ...}  
  7.   .element-avatar { ... }  
  8.   .element-selected { ... }  
  9. }  
五.带前缀的属性

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

[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Prefixed properties */  
  2. .selector {  
  3.   -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);  
  4.           box-shadow: 0 1px 2px rgba(0,0,0,.15);  
  5. }  
六.单行规则声明

  对于只包含一条声明的样式,为了易读性和便于快速编辑,建议将语句放在同一行。对于带有多条声明的样式,还是应当将声明分为多行。
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Single declarations on one line */  
  2. .span1 { width60px; }  
  3. .span2 { width140px; }  
  4. .span3 { width220px; }  
  5.   
  6. /* Multiple declarations, one per line */  
  7. .sprite {  
  8.   display: inline-block;  
  9.   width16px;  
  10.   height15px;  
  11.   background-imageurl(../img/sprite.png);  
  12. }  
  13. .icon           { background-position0 0; }  
  14. .icon-home      { background-position0 -20px; }  
  15. .icon-account   { background-position0 -40px; }  

七.简写形式的属性声明

  在需要显示地设置所有值的情况下,应当尽量限制使用简写形式的属性声明。
    1.padding
    2.margin
    3.font
    4.background
    5.border
    6.border-radius
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Bad example */  
  2. .element {  
  3.   margin0 0 10px;  
  4.   backgroundred;  
  5.   backgroundurl("image.jpg");  
  6.   border-radius: 3px 3px 0 0;  
  7. }  
  8.   
  9. /* Good example */  
  10. .element {  
  11.   margin-bottom10px;  
  12.   background-colorred;  
  13.   background-imageurl("image.jpg");  
  14.   border-top-left-radius: 3px;  
  15.   border-top-right-radius: 3px;  
  16. }  
八.Less 和 Sass 中的嵌套

  避免非必要的嵌套。这是因为虽然你可以使用嵌套,但是并不意味着应该使用嵌套。只有在必须将样式限制在父元素内(也就是后代选择器),并且存在多个需要嵌套的元素时才使用嵌套。
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // Without nesting  
  2. .table > thead > tr > th { … }  
  3. .table > thead > tr > td { … }  
  4.   
  5. // With nesting  
  6. .table > thead > tr {  
  7.   > th { … }  
  8.   > td { … }  
  9. }  
九.注释

  对于较长的注释,务必书写完整的句子;对于一般性注解,可以书写简洁的短语。
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Bad example */  
  2. /* Modal header */  
  3. .modal-header {  
  4.   ...  
  5. }  
  6.   
  7. /* Good example */  
  8. /* Wrapping element for .modal-title and .modal-close */  
  9. .modal-header {  
  10.   ...  
  11. }  
十.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 文件中。
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Bad example */  
  2. .t { ... }  
  3. .red { ... }  
  4. .header { ... }  
  5.   
  6. /* Good example */  
  7. .tweet { ... }  
  8. .important { ... }  
  9. .tweet-header { ... }  

十一.选择器

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

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

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

  4.只有在必要的时候才将 class 限制在最近的父元素内(也就是后代选择器)(例如,不使用带前缀的 class 时 -- 前缀类似于命名空间)。
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Bad example */  
  2. span { ... }  
  3. .page-container #stream .stream-item .tweet .tweet-header .username { ... }  
  4. .avatar { ... }  
  5.   
  6. /* Good example */  
  7. .avatar { ... }  
  8. .tweet-header .username { ... }  
  9. .tweet .avatar { ... }  

十二.代码组织

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

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

  4.如果使用了多个 CSS 文件,将其按照组件而非页面的形式分拆,因为页面会被重组,而组件只会被移动。
[css] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * Component section heading 
  3.  */  
  4.   
  5. .element { ... }  
  6.   
  7.   
  8. /* 
  9.  * Component section heading 
  10.  * 
  11.  * Sometimes you need to include optional context for the entire component. Do that up here if it's important enough. 
  12.  */  
  13.   
  14. .element { ... }  
  15.   
  16. /* Contextual sub-component or modifer */  
  17. .element-heading { ... }  
0 0
原创粉丝点击