博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

web开发中怎么样使css书写规范?

Posted on 2009-10-28 13:27  海阔天空,一路是蓝  阅读(2680)  评论(10编辑  收藏  举报

web开发中页面设计时如何使css书写规范是很重要的。正确规范的书写css样式表可以使提高网页的速度以及,各种浏览器的兼容性,以便于开发程序的人员更好的嵌入程序。对于现在的web2.0,到底怎么书写css样式表,才算是最好的,最规范的!几乎没有什么明确的规定。在整个开发的项目中,只要写出一套最适合于团队开发的最好了。当然了很多的css样式书写范是大同小异的。需要不断的去归纳总结。


根据经验总结出以下几点规范,请大家参考一下:

一.所有的CSS的尽量采用外部调用:这是我认为最重要的一点,因为这影响加载页面时的速度.

<LINK href="style/style.css" rel="stylesheet" type="text/css">

书写时重定义的最先,伪类其次,自定义最后(其中a:link a:visited a:hover a:actived 要按照顺序写)便于自己和他人阅读。

为了保证不同浏览器上字号保持一致,字号建议用点数pt和像素px来定义,pt一般使用中文宋体的9pt和11pt,px一般使用中文宋体12pt 和14.7px 这是经过优化的字号,黑体字或者宋体字加粗时,一般选用11pt和14.7px 的字号比较合适。
 

样式名均以字母开头,后缀可包含字母、数字、下划线和中划线,样式名尽量采用通俗易懂的英文单词组成,如.img .body-bg #left_nav(后附一些通用的命名标准);对于公用的自定义样式可适当加入模块标识或comm标识,以免与独立模块样式产生冲突!

.html内置标签及CSS属性名称均采用大写字母书写,而自定义样式名称及CSS属性值则均采用小写字母写书;如:P{...} BODY{...}  .p1{COLOR: red}  .li_bg{MARGIN: 0px;} 等;

.能采用缩写的样式属性尽量采用缩写形式,如marginpaddingborder;
(原则:上--- --> MARGIN: 1px 2px 3px 4px  -(右左)- -->  MARGIN: 1px 2px 3px
(上下)-(左右)-->  MARGIN: 2px 4px   上右下左  --> MARGIN: 4px;  
字体颜色类:  #000000  -->  #000         #00FFDD -->  #0FD; 

附:缩写参考
There are a lot of CSS shorthand properties.

    * font
      font: font-style font-variant font-weight font-size/line-height font-family;
    * margin
      margin: margin-top margin-right margin-bottom margin-left;
    * padding
      padding: padding-top padding-right padding-bottom padding-left;
    * border
      border: border-width border-style border-color;
          * border-top
            border-top: border-top-width border-top-style border-top-color;
          * border-right
            border-right: border-right-width border-right-style border-right-color;
          * border-bottom
            border-bottom: border-bottom-width border-bottom-style border-bottom-color;
          * border-left
            border-left: border-left-width border-left-style border-left-color;
          * border-width
            border-width: border-top-width border-right-width border-bottom-width border-left-width
    * background
      background: background-color background-image background-repeat background-attachment background-position;
    * list-style
      list-style: list-style-type list-style-position list-style-image;
    * outline
      outline: outline-color outline-style outline-width;

在书写样式的过程中,同一划分模块的样式尽量写在一起,并适当加入注释,以方便阅读和查找;如<-- Footer --><-- End Footer -->或者/* Footer */  /* End Footer */

.样式抽象,我们可以把一些常用的样式抽象出来,做为一类样式;如COLOR: red  FLOAT:left  FONT-WEIGHT: bold 这些属性我们可能要经常用到,我们是否需要为每个需要应用这个样式的元素都单独写上一个样式呢?答案是明确的:不需要; 我们可以定义:  .red{ COLOR:red }  .fleft{FLOAT:left} 这样是不是要方便多了^_^


七. 效率规则:(css属性书写顺序)

1.
显示样式
    display/position/float/clear
2.
自身样式
    width/height/margin/padding/border/background 
3.
内容样式
    line-height/text-align/font
系列(font-size/font-weight/color/text-decoration/vitical-align

 

八.css样式的命名:

对于css样式表的命名书写规范,请参考http://www.po-soft(listly).com/blog/listly/220.html (地址中去掉"(listly)")

 关于更多的css书写规范,不止这些,还需要我们共同的来总结和学习,如果您有更好的见意,请留言!