编写更好的CSS代码
編寫(xiě)好的CSS代碼,有助提升頁(yè)面的渲染速度。本質(zhì)上,引擎需要解析的CSS規(guī)則越少,性能越好。MDN上將CSS選擇符歸類(lèi)成四個(gè)主要類(lèi)別,如下所示,性能依次降低。
ID 規(guī)則
Class 規(guī)則
標(biāo)簽規(guī)則
通用規(guī)則
對(duì)效率的普遍認(rèn)識(shí)是從Steve Souders在2009年出版的《高性能網(wǎng)站建設(shè)進(jìn)階指南》開(kāi)始,雖然該書(shū)中羅列的更加詳細(xì),但你也可以在這里查看完整的引用列表,也可以在谷歌的《高效CSS選擇器的最佳實(shí)踐》中查看更多的細(xì)節(jié)。
本文我想分享一些我在編寫(xiě)高性能CSS中用到的簡(jiǎn)單例子和指南。這些都是受到MDN 編寫(xiě)的高效CSS指南的啟發(fā),并遵循類(lèi)似的格式。
避免過(guò)度約束
一條普遍規(guī)則,不要添加不必要的約束。
| 1234567 | // 糟糕ul#someid {..}.menu#otherid{..}// 好的#someid {..}#otherid {..} |
后代選擇符最爛
不僅性能低下而且代碼很脆弱,html代碼和css代碼嚴(yán)重耦合,html代碼結(jié)構(gòu)發(fā)生變化時(shí),CSS也得修改,這是多么糟糕,特別是在大公司里,寫(xiě)html和css的往往不是同一個(gè)人。
| 12 | // 爛透了html div tr td {..} |
避免鏈?zhǔn)?#xff08;交集)選擇符
這和過(guò)度約束的情況類(lèi)似,更明智的做法是簡(jiǎn)單的創(chuàng)建一個(gè)新的CSS類(lèi)選擇符。
| 12345 | // 糟糕.menu.left.icon{..}// 好的.menu-left-icon{..} |
堅(jiān)持KISS原則
想象我們有如下的DOM:
| 12345 | <ulid="navigator"><li><ahref="#"class="twitter">Twitter</a></li><li><ahref="#"class="facebook">Facebook</a></li><li><ahref="#"class="dribble">Dribbble</a></li></ul> |
下面是對(duì)應(yīng)的規(guī)則……
| 12345 | // 糟糕#navigator li a {..}// 好的#navigator {..} |
使用復(fù)合(緊湊)語(yǔ)法
盡可能使用復(fù)合語(yǔ)法。
| 1234567891011121314151617 | // 糟糕.someclass {padding-top: 20px;padding-bottom: 20px;padding-left: 10px;padding-right: 10px;background: #000;background-p_w_picpath: url(../imgs/carrot.png);background-position: bottom;background-repeat: repeat-x;}// 好的.someclass {padding: 20px10px20px10px;background: #000url(../imgs/carrot.png) repeat-xbottom;} |
避免不必要的命名空間
| 12345 | // 糟糕.someclass table tr.otherclass td.somerule {..}//好的.someclass .otherclass td.somerule {..} |
避免不必要的重復(fù)
盡可能組合重復(fù)的規(guī)則。
| 123456789101112131415161718192021 | // 糟糕.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;} |
盡可能精簡(jiǎn)規(guī)則
在上面規(guī)則的基礎(chǔ)上,你可以進(jìn)一步合并不同類(lèi)里的重復(fù)的規(guī)則。
| 1234567891011121314151617181920212223242526272829303132 | // 糟糕.someclass {color: red;background: blue;height: 150px;width: 150px;font-size: 16px;}.otherclass {color: red;background: blue;height: 150px;width: 150px;font-size: 8px;}// 好的.someclass, .otherclass {color: red;background: blue;height: 150px;width: 150px;}.someclass {font-size: 16px;}.otherclass {font-size: 8px;} |
避免不明確的命名約定
最好使用表示語(yǔ)義的名字。一個(gè)好的CSS類(lèi)名應(yīng)描述它是什么而不是它像什么。
避免 !importants
其實(shí)你應(yīng)該也可以使用其他優(yōu)質(zhì)的選擇器。
遵循一個(gè)標(biāo)準(zhǔn)的聲明順序
雖然有一些排列CSS屬性順序常見(jiàn)的方式,下面是我遵循的一種流行方式。
| 1234567 | .someclass {/* Positioning *//* Display & Box Model *//* Background and typography styles *//* Transitions *//* Other */} |
組織好的代碼格式
代碼的易讀性和易維護(hù)性成正比。下面是我遵循的格式化方法。
| 12345678910111213141516171819202122 | // 糟糕.someclass-a, .someclass-b, .someclass-c, .someclass-d {...}// 好的.someclass-a, .someclass-b, .someclass-c, .someclass-d {...}// 好的做法.someclass {background-p_w_picpath:linear-gradient(#000, #ccc),linear-gradient(#ccc, #ddd);box-shadow:2px2px2px#000,1px4px1px1px#dddinset;} |
顯然,這里只講述了少數(shù)的規(guī)則,是我在我自己的CSS中,本著更高效和更易維護(hù)性而嘗試遵循的規(guī)則。如果你想的知識(shí),我建議閱讀MDN上的編寫(xiě)高效的CSS和谷歌的優(yōu)化瀏覽器渲染指南。
原文鏈接: Mathew Carella ? 翻譯: 伯樂(lè)在線 - yanhaijing
譯文鏈接: http://blog.jobbole.com/55067/
轉(zhuǎn)載于:https://blog.51cto.com/2string/1352672
總結(jié)
以上是生活随笔為你收集整理的编写更好的CSS代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Grails示例程序-导出Excel文档
- 下一篇: 拥抱开源IaaS云平台:360度盘点Op