编写更好的CSS

十度 HTML 2016年02月16日 收藏

编写好的CSS代码能提升页面的渲染速度。本质上,一条规则都没有引擎解析的最快。MDN上将CSS选择符归拆分成四个主要类别,如下所示,性能依次降低。

  1. ID 规则
  2. Class 规则
  3. 标签规则
  4. 通用规则

对效率普遍认识是从Steve Souders在2009年出版的《高性能网站建设进阶指南》开始的,虽然Souders的书中罗列的非常详细,你可以在这里查看完整列表引用。你也可以在谷歌的高效的CSS选择器的最佳实践中查看更多的细节。

本文我想分享一些我在编写高性能CSS中用到的简单的例子和指导方针。受MDN的编写高效的CSS指南的启发,并遵循类似的格式。

避免过度约束

作为一般规则,不添加不必要的约束。

  1. // 糟糕
  2. ul#someid {..}
  3. .menu#otherid{..}
  4. // 好的
  5. #someid {..}
  6. #otherid {..}

后代选择符最烂

不仅性能低下而且代码很脆弱,html代码和css代码严重耦合,html代码结构发生变化时,CSS也得修改,这是多么糟糕,特别是在大公司里,写html和css的往往不是同一个人。

  1. // 烂透了
  2. html div tr td {..}

避免链式(交集)选择符

这和过度约束的情况类似,更明智的做法是简单的创建一个新的CSS类选择符。

  1. // 糟糕
  2. .menu.left.icon {..}
  3. // 好的
  4. .menu-left-icon {..}

坚持KISS原则

想象我们有如下的DOM:

  1. <ul id="navigator">
  2. <li><a href="#" class="twitter">Twitter</a></li>
  3. <li><a href="#" class="facebook">Facebook</a></li>
  4. <li><a href="#" class="dribble">Dribbble</a></li>
  5. </ul>

下面是对应的规则……

  1. // 糟糕
  2. #navigator li a {..}
  3. // 好的
  4. #navigator {..}

使用复合语法

尽可能使用符合语法。

  1. // 糟糕
  2. .someclass {
  3. padding-top: 20px;
  4. padding-bottom: 20px;
  5. padding-left: 10px;
  6. padding-right: 10px;
  7. background: #000;
  8. background-image: url(../imgs/carrot.png);
  9. background-position: bottom;
  10. background-repeat: repeat-x;
  11. }
  12. // 好的
  13. .someclass {
  14. padding: 20px 10px 20px 10px;
  15. background: #000 url(../imgs/carrot.png) repeat-x bottom;
  16. }

避免不必要的命名空间

  1. // 糟糕
  2. .someclass table tr.otherclass td.somerule {..}
  3. //好的
  4. .someclass .otherclass td.somerule {..}

避免不必要的重复

尽可能组合重复的规则。

  1. // 糟糕
  2. .someclass {
  3. color: red;
  4. background: blue;
  5. font-size: 15px;
  6. }
  7. .otherclass {
  8. color: red;
  9. background: blue;
  10. font-size: 15px;
  11. }
  12. // 好的
  13. .someclass, .otherclass {
  14. color: red;
  15. background: blue;
  16. font-size: 15px;
  17. }

尽可能精简规则

在上面规则的基础上,你可以进一步合并不同类里的重复的规则。

  1. // 糟糕
  2. .someclass {
  3. color: red;
  4. background: blue;
  5. height: 150px;
  6. width: 150px;
  7. font-size: 16px;
  8. }
  9. .otherclass {
  10. color: red;
  11. background: blue;
  12. height: 150px;
  13. width: 150px;
  14. font-size: 8px;
  15. }
  16. // 好的
  17. .someclass, .otherclass {
  18. color: red;
  19. background: blue;
  20. height: 150px;
  21. width: 150px;
  22. }
  23. .someclass {
  24. font-size: 16px;
  25. }
  26. .otherclass {
  27. font-size: 8px;
  28. }

避免不明确的命名约定

最好使用表示语义的名字。一个好的CSS类名应描述它是什么而不是它像什么。

避免 !importants

其实你应该也可以使用其他优质的选择器。

遵循一个标准的声明顺序

虽然有一些排列CSS属性顺序常见的方式,下面是我遵循的一种流行方式。

  1. .someclass {
  2. /* Positioning */
  3. /* Display & Box Model */
  4. /* Background and typography styles */
  5. /* Transitions */
  6. /* Other */
  7. }

组织好的代码格式

代码的易读性和易维护性成正比。下面是我遵循的格式化方法。

  1. // 糟糕
  2. .someclass-a, .someclass-b, .someclass-c, .someclass-d {
  3. ...
  4. }
  5. // 好的
  6. .someclass-a,
  7. .someclass-b,
  8. .someclass-c,
  9. .someclass-d {
  10. ...
  11. }
  12. // 好的做法
  13. .someclass {
  14. background-image:
  15. linear-gradient(#000, #ccc),
  16. linear-gradient(#ccc, #ddd);
  17. box-shadow:
  18. 2px 2px 2px #000,
  19. 1px 4px 1px 1px #ddd inset;
  20. }

 

显然,这些只是极少数的规则,是我在我自己的CSS中,本着更高效和更易维护性而尝试遵循的规则。如果你想阅读更多的知识,我建议阅读MDN上的编写高效的CSS和谷歌指南上的优化浏览器渲染。

译者注

本文为译文,原文为“Writing Better CSS”

 支持我继续翻译吧。

更多文章请访问的我的博客。

 关注我的微博吧。