欢迎加入站点基地设计开发交流群:30720409
详细内容
当前位置 首页 >> CSS >> CSS技术 >> 正文
几个CSS书写技巧
发布日期:2010-07-09     点击:3476     字体:[ ]

1. CSS Reset/重置

也许你需要先了解什么是css重置。然后怎么样写css重置。

你可以copy Eric Meyer Reset, YUI Reset或其它css reset, 但你接下来应该根据你的项目改成你自己的reset.
不要使用* { margin: 0; padding: 0; } 。我个人很爱用,原作者提到使用这句并不适用一些元素比如单选按钮。

2. 按字母顺序来排列css

不按字母顺序排的

div#header h1 {
z-index: 101;
color: #000;
position: relative;
line-height: 24px;
margin-right: 48px;
border-bottom: 1px solid #dedede;
font-size: 18px;
}

按字母顺序排的

div#header h1 {
 border-bottom: 1px solid #dedede;
 color: #000;
 font-size: 18px;
 line-height: 24px;
 margin-right: 48px;
 position: relative;
 z-index: 101;
}
理由是这样可以更好的找到某个属性。站点基地觉得还好,差别也不是太大。不过也许会适合你。

3. 更好的组织css结构

使用css注释来分给css分组,这样结构明了,也有利于协同设计。

/*****Reset*****/
xxxxxxx{xxxxx}
xxxxx{xxxxx}
/*****layouts*****/
xxxxxxx{xxxxx}
xxxxx{xxxxx}

4. 保持一致性

不要无意义的去讨论到底一个选择器的所有属性写在一行,还是每个属性写一行比较好。你自己觉得ok就好。

iv#header { float: left; width: 100%; }
div#header div.column {
border-right: 1px solid #ccc;
float: rightright;
margin-right: 50px;
padding: 10px;
width: 300px;
}
div#header h1 { float: left; position: relative; width: 250px; }
比如我个人就喜欢写在一行,因为每排写一行会让整个文档感觉太长了,找起来不方便。如果你喜欢写一行,但是发给team的另一个,他却喜欢每排一行,那怎么办?其实很简单,把css拿去w3c验证,它会有一份结果,会自动转换成每排一行。

5. 先标记后css

先把一个页面的大体结构搭建好,这样再来写CSS会比较有条理。

<body>
     <div id="wrapper">
          <div id="header"><!--end #header-->
          <div id="container">
               <div id="content">
               </div><!--end #content-->
               <div id="sidebar">
               </div><!--end #sidebarr-->
          </div><!--end #container-->
          <div id="footer">
          </div>!<--end #footer-->
       </div><!--end #wrapper-->
</body>

然后就是尽量善用子选择器,而不是一要给哪个元素进行样式化,就给它添加个选择器。

用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号 可人软件设计