优化代码 改善CSS文件可读性

作者:蓝调 来源:蓝调blog 时间:2008-06-13 13:50:00 

Some readers have asked to me what is the better way to organize a CSS  file to optimize code readability and simplify code management. Generally,  I adopt just some simple rules and,  it's my opinion, they are useful in order not to become crazy if you have to manage a CSS file for a page/site with a complex design.   In this post, I discuss for simplicity a typical two columns fixed layout like this:
  一些读者曾问我如何以更好的方式组织CSS文件来改善代码的可读性并简化代码的管理。一般情况下,我只采用几个简单的规则,但我认为很有用,因为当你不得不去管理一个有着复杂设计的页面/站点的CSS文件时,它可以避免让你抓狂。在这里,我将简单探讨如下所示的典型的两列固定宽度布局:

Step 1: redefine HTML elements

I think it's a good rule to redefine HTML elements (body, a, form, input...) in the first rows of your CSS files.

第一步:重定义HTML标签

我认为在CSS文件前面几行重新定义HTML标签(body, a, form, input...)是一个好规则。

/* ------------------------------- */
/* HTML Elements
/* ------------------------------- */
html {font-family:arial, verdana, sans serif; font-size:13px;}
a:link, a:visited{color:#0033CC;}
a:hover{color:#003366;}
h1, h2, h3, h4, h5, h6,
form, input, text-area{
border:0; padding:0; margin:0;
font-family:arial, verdana, sans serif;}
h1{font-size:24px; color:#000000;}
h2{font-size:18px; color:#666666;}
... 

Step 2: define page elements
  In order to improve code readability, I suggest to indent all elements but with some sagacities: if an element have just two-three attributes (for example #navbar), you can use a single line to declare all properties, otherwise it's better to declare every single property in a new line (for example see #navbar li a:link, #navbar li a:visited):

第二步:重定义页面标签

为了改善代码可读性,我建议缩进所有标签是比较有远见的:如果一个标签只有两三个属性(如#navbar),你可以在一行内声明所有属性,否则在一行内最好只声明一个属性(如下所示的#navbar li a:link, #navbar li a:visited) 

/* PAGE Elements
/* ------------------------------- */
#container{width:780px; margin:0 auto;}

#topbar{width:auto; display:block; height:80px;}
#navbar{width:auto; display:block; height:24px;}

#navbar ul, #navbar ul li{padding:0; margin:0; list-style:none; float:left;}
#navbar a{color:#FFFFFF; font-weight:bold;}
#navbar a:hover{background:#777777;}
#navbar li a:link,
#navbar li a:visited {

background:#444444;
text-decoration:none;
height:24px;
line-height:24px;
display:inline;
float:left;
width:auto;
padding:0px 10px;}

#main{width:auto; display:block;}

#column_left{width:560px; margin-right:20px; float:left;}
#column_right{width:200px; float:left;}

div.spacer{clear:both; height:10px; display:block;}
#footer{width:auto; display:block; height:24px;}

#footer a{color:#666666; text-decoration:underline;} 

Step 3: define custom class

In the final section of your CSS files, you can define all other custom classes with the same rules I specified above:

第三步:定义自定义类

在CSS文件的末尾,你可以用我上面提出的规则定义所有其他的自定义类

/* ------------------------------- */
/* OTHER Class
/* ------------------------------- */
.small{font-size:11px;}
.underline{text-decoration:underline;}
div.small-section{background:#CCCCCC;}

div.small-section a{color:#333333; font-weight:bold;}
... 

How I said, these are only my personal suggestions and not true rules, but I think you can find them useful in order to deploy a more readable CSS file.
  就如我所说,这些只是我的个人建议,并不是本质规律,但我想如果你要部署一个更具可读性的CSS文件,你还是会发现这些规则是很有用的。
  

标签:优化,文件,css,代码
0
投稿

猜你喜欢

  • 用Dreamweaver MX实现网站批量更新

    2009-09-13 18:39:00
  • Sql Server 和 Access 操作数据库结构Sql语句

    2008-02-11 18:59:00
  • oracle 下WITH CHECK OPTION用法

    2009-02-28 10:59:00
  • 再谈动态添加样式规则

    2009-09-02 13:10:00
  • IE6/7关于 Absolute Position 、relative 的一些意外

    2008-11-27 11:34:00
  • 安装MySQL的步骤和方法

    2009-07-30 08:38:00
  • asp如何直接调用后台存储过程?

    2010-06-28 18:27:00
  • 面向对象CSS FAQ[译]

    2009-10-27 15:59:00
  • JavaScript 判断日期格式是否正确的实现代码

    2011-07-05 12:37:44
  • 用 Schema 约束 XML 数据

    2010-08-24 18:21:00
  • 如何以及何时使用sIFR

    2008-03-07 12:38:00
  • SQL 2005 sa islock用户不能正常登录的现象

    2008-12-05 15:49:00
  • 利用sort()和Math.random()实现元素的随机排列

    2010-10-19 12:42:00
  • 完全卸载MYSQL

    2011-02-23 12:11:00
  • ASP JSON类文件的使用方法

    2011-04-30 16:39:00
  • asp如何在网上查找链接?

    2010-06-22 21:10:00
  • Http头 Range、Content-Range

    2010-06-25 19:19:00
  • asp网上购物车实例代码

    2007-10-03 13:43:00
  • ASP利用Google实现在线翻译功能

    2010-03-07 17:28:00
  • 什么是Dynamic HTML(Dhtml)

    2010-03-16 12:40:00
  • asp之家 网络编程 m.aspxhome.com