谈谈CSS性能

吴小倩

Copyright © 2014 W3C ® (MIT, ERCIM, Keio, Beihang)

更快,更流畅

谈谈CSS性能

吴小倩 <xiaoqian@w3.org>

W3C前端交互领域

2015-01-10, CSS开发者大会

CSS性能优化

慎重选择高消耗的样式(Expensive Styles)

1. 合理安排Selectors(selectors瘦身);

2. 高消耗属性?-> 绘制前需要浏览器进行大量计算:

box-shadows border-radius
transparency transforms
CSS filters(性能杀手)

避免过分重排(Reflow)

浏览器重新计算布局位置大小

常见的重排元素;

widthheightpaddingmargin
displayborder-widthbordertop
positionfont-sizefloattext-align
overflow-yfont-weightoverflowleft
font-familyline-heightvertical-alignright
clearwhite-spacebottommin-height

避免过分重绘(Repaints)

常见的重绘元素;

colorborder-stylevisibilitybackground
text-decorationbackground-imagebackground-positionbackground-repeat
outline-coloroutlineoutline-styleborder-radius
outline-widthbox-shadowbackground-size

CSS性能优化

CSS Will Change

requestAnimationFrame

Web Animations & Smoothness Timing

为更未来流畅的动画效果提供准备

一份标准的诞生:问题-用例和目标-编写标准

别靠猜,别靠理论,靠测试

W3C Web性能工作组

发布Resource Priorities, Resource Hints, Navigation Timing等规范;

订阅public-web-perf@w3.org邮件组;

围观讨论;

谢谢!

Slides制作:shower

will-change图片来源:w3c meme

RAF图片来源:thetop10s.net

Ref:

1. Everything You Need to Know About the CSS will-change Property by Sara Soueidan

2. High Performance Animations by Paul Lewis and Paul Irish

3. We’re Gonna Need A Bigger API! by Jake Archibald

4. PERFORMANCE TRICKS FOR (MOBILE) WEB DEVELOPMENT

5. CSS performance revisited: selectors, bloat and expensive styles

Special thanks to PLH, for telling me all those stories about web-perf, and the W3C Beihang Team, for their help on Cookie.