CSS编写规范的相关建议
2015-07-29 19:13
633 查看
避免过度约束
作为一般规则,不添加不必要的约束。
后代选择符最烂
不仅性能低下而且代码很脆弱,html代码和css代码严重耦合,html代码结构发生变化时,CSS也得修改,这是多么糟糕,特别是在大公司里,写html和css的往往不是同一个人。
尽可能使用复合语法
避免不必要的重复
组织好的代码格式
代码的易读性和易维护性成正比。下面是我遵循的格式化方法。
作为一般规则,不添加不必要的约束。
// 糟糕 ul#someid {..} .menu#otherid{..} // 好的 #someid {..} #otherid {..}
后代选择符最烂
不仅性能低下而且代码很脆弱,html代码和css代码严重耦合,html代码结构发生变化时,CSS也得修改,这是多么糟糕,特别是在大公司里,写html和css的往往不是同一个人。
// 烂透了 html div tr td {..}
尽可能使用复合语法
// 糟糕 .someclass { padding-top: 20px; padding-bottom: 20px; padding-left: 10px; padding-right: 10px; background: #000; background-image: url(../imgs/carrot.png); background-position: bottombottom; background-repeat: repeat-x; } // 好的 .someclass { padding: 20px 10px 20px 10px; background: #000 url(../imgs/carrot.png) repeat-x bottombottom; }
避免不必要的重复
// 糟糕 .someclass { color: red; background: blue; font-size: 15px; } .otherclass { color: red; background: blue; font-size: 15px; } // 好的 .someclass, .otherclass { color: red; background: blue; font-size: 15px; }
组织好的代码格式
代码的易读性和易维护性成正比。下面是我遵循的格式化方法。
// 糟糕 .someclass-a, .someclass-b, .someclass-c, .someclass-d { ... } // 好的 .someclass-a, .someclass-b, .someclass-c, .someclass-d { ... } // 好的做法 .someclass { background-image: linear-gradient(#000, #ccc), linear-gradient(#ccc, #ddd); box-shadow: 2px 2px 2px #000, 1px 4px 1px 1px #ddd inset; }
相关文章推荐
- 使用CSS居中浮动元素的方法
- CSS实现垂直水平居中
- list-style样式不显示问题
- html及css学习笔记_2_html基本结构
- CSS3的Flexible Boxes详细使用教程
- 【CSS学习】CSS 创建
- 【CSS学习】CSS Id 和 Class
- CSS3 中的动画功能
- 使用CSS将图片转换成黑白(灰色、置灰)
- 【CSS学习】CSS语法
- CSS ul li 样式详解
- CSS模块化思想-----命名是个技术活
- css3绘制 macbook
- WPF 4 DataGrid 控件(自定义样式篇)
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
- CSS优化技巧
- css中的相对定位和绝对定位
- CSS学习成长记
- css模块化思想(一)--------命名是个技术活
- 高性能 CSS3 动画