GithubHelp home page GithubHelp logo

cssguideline's Introduction

CSS Code Guideline

  1. CSS浏览器支持标准

WinXP Win7 OS X
IE9 B B
IE8 A A
IE7 B B
IE6 B B
Chrome16 A A A
Chrome14 A A A
Firefox10 A A A
Firefox9 A A A
Safari B B B
Opera C C C
  • A级-交互和视觉完全符全设计的要求
  • B级-视觉上允许有所差异,但不破坏页面效果
  • C级-可忽略设计上的细节,但不防碍使用
  1. 重用已有的样式库

  • 2-1. 主站全局CSS文件 - global.css
  • 2-2. 当前使用的CSS库(持续更新)
    • css/bootstrap.css
    • css/bootstrap-theme.css
  1. CSS文件的目录管理

所有的CSS放在如下目录中 css/

  1. CSS的模块化组织

当前静态文件管理系统已支持SCSS和导入语法。开发中,应尽量按功能分解大文件,实现模块化的文件组织。

  • 使用和完善现有CSS库(见2-4)。
  • 单个CSS文件避免过大(建议少于300行)。
  • CSS文件嵌套不要超过一层
  1. 不要轻易改动全站级CSS和通用CSS库。改动后,要经过全面测试

  1. 单条CSS规则的书写格式要求

  • 6-1. 单行形式(例如)。 (注:在很长的文件中,单行形式有利于检索选择器)
    .selector { property:value;property:value; }

简短规则声明(1或2个)也适用单行形式。

    .selector { property:value; }
  • 6-2. 格式化书写形式。适用于不是很长的模块文件或CSS3语法。冒号后加空格。
    .selector { 
        property: value;
        property: value; 
    }

CSS3兼容书写形式和对齐方式:

    .selector { 
      -webkit-box-shadow: 0 0 5px rgba(200, 200,200, 0.8);
         -moz-box-shadow: 0 0 5px rgba(200, 200,200, 0.8);
              box-shadow: 0 0 5px rgba(200, 200,200, 0.8);
    }

CSS3中逗号分隔的长属性值:

    .selector {
        box-shadow:
            1px 1px 1px #000,
            2px 2px 1px 1px #ccc inset;
        background-image:
            linear-gradient(#fff, #ccc),
            linear-gradient(#f3c, #4ec);
    }
  • 6-3. 多个(>2)selector每个占一行:
    .selector1,
    .selector2,
    .selector3 { ... }
  • 6-4. 规则声明的顺序:定位、盒模型(width/height/padding/border/margin)、行高、字体/字号/颜色、背景、CSS3效果等

  • 6-5. 兼容多个浏览器时,将标准规则声明写在后面,如:

    -webkit-border-radius: 4px;
       -moz-border-radius: 4px;
            border-radius: 4px;
  1. 注释书写形式

  • 7-1. 注释内容单行控制在40个中文或80个英文字符宽。注释的格式:
     /* 
      * mod: doulist 
      * 描述内容
      */

SCSS中支持单行注释

    // mod: doulist
  • 7-2. 规则分类放在一起。通用规则在具体业务规则的前面。如:
    /* layout */
    ...
    /* mod */
    ...
    /* nav */
    ...
    /* mod: events album */
    ...
  1. ID和Class命名。命名不要用缩写(除一些公认的缩写,见8-4),单词间用"-"做为连接符

  • 8-1. ID是用来标识具体模块,命名必须具体且唯一,由前缀和名字组成。不要滥用ID。如: #db-video-list、#group-member-list等。
  • 8-2. Class是用来标识某一类型的对象,命名简洁表意清楚。如:.list。
  • 8-3. 命名示例:

坏:

    #rec
    .gray-link
    .broadsmr
    .pl

好:

    #db-nav-main
    .infobox
    .item
  • 8-4. 推荐使用的class名:
表示状态 .on, .active, .selected, .hili
表示位置 .first, .last, .main, .side
表示结构 .hd, .bd, .ft, .col, .section
通用元素 .tb, .frm, .nav, .list, .item, .tag, .pic, .info
  1. 避免滥用CSS Hack

推荐使用下面的:

区别属性:

IE6 _property:value
IE6/7 *property:value
IE6/7/8/9 property:value\9
区别规则:
IE6 * html selector { ... }
IE7 *:first-child+html selector { ... }
非IE6 html>body selector { ... }
firefox only @-moz-document url-prefix() { ... }
saf3+/chrome1+ @media all and (-webkit-min-device-pixel-ratio:0) { ... }
opera only @media all and (-webkit-min-device-pixel-ratio:10000),not all and (-webkit-min-device-pixel-ratio:0) { ... }
iPhone/mobile webkit @media screen and (max-device-width: 480px) { ... }
注意:SCSS会自动转换一些不标准CSS写法,会破坏CSS Hack。
  1. 使用after或overflow的方式清浮动,不要在html里增加多余的标签

  1. CSS必须放在head里

  1. 避免使用低效的选择器

如:

    body > * {...}
    ul > li > a {...}
    #footer > h3 {...}
    ul#top_blue_nav {...}
    #searbar span.submit a { ... }
    .target #target-node { ... }
  1. 避免使用filter

  1. 避免直接定义标签的样式。

  1. 避免在标签上直接写样式。

  1. 避免在CSS中使用expression

  1. 避免在CSS中使用@import

  1. 尽量不要在CSS中使用!important

参考 douban css guide line

cssguideline's People

Contributors

yuchav avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.