codecamp

性能优化

性能优化

  1. 1、合并margin、padding、border的-left/-top/-right/-bottom的设置,尽量使用短名称。

  2. 2、选择器应该在满足功能的基础上尽量简短,减少选择器嵌套,查询消耗。但是一定要避免覆盖全局样式设置。

  3. 3、注意选择器的性能,不要使用低性能的选择器,例如:

    1
    2
    3
    div > * {}
    ul > li > a {}
    body.profile ul.tabs.nav li a {}
  4. 4、禁止在css中使用*选择符

  5. 5、除非必须,否则,一般有class或id的,不需要再写上元素对应的tag,例如:

    1
    div#test { width: 100px; }
  6. 6、0后面不需要单位,比如0px可以省略成0,0.8px可以省略成.8px

  7. 7、如果是16进制表示颜色,则颜色取值应该大写。

  8. 8、如果可以,颜色尽量用三位字符表示,例如#AABBCC写成#ABC

  9. 9、如果没有边框时,不要写成border:0,应该写成border:none

  10. 10、尽量避免使用AlphaImageLoader

  11. 11、在保持代码解耦的前提下,尽量合并重复的样式,例如:

    1
    2
    3
    4
    5
    h1 { color: black; }
    p { color: black; }
    -->
    h1,
    p { color: black; }
  12. 12、background、font等可以缩写的属性,尽量使用缩写形式

    1
    2
    background: color image repeat attachment position;
    font: style weight size/lineHeight family;


属性编写顺序
CSS属性取值规范
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }