Tuesday, June 03, 2008

CSS Performance

I have been reading CSS Mastery lately - its a great medium/advanced guide to CSS. If you know roughly about CSS but you need serious helping turning that in to mastery, then this book is for you. The book provides both a quick introduction and specific solutions. However, the book provides advice that may be at odds with Firefox performance guidelines.

The book starts with a quick introduction, and then delves in to the CSS box model, then straight in to rounded corner techniques. It provides pragmatic advice along the way - such as what works on which browsers and making design cross-browser.

However, the book recommends avoiding the use of too many classes - calling that design "classitus". By using the descent selector you can target your styles without using too many classes. If you read the mozilla developer guide to efficient CSS, the advice there is substantially different.

The guide advises, among other things, that the descendant selector has poor performance. They recommendation is to use targeted class styles instead. This may lead to an expanded use of classes in your application, but the performance increase may be worth it. The best strategy is to measure. Since querying certain properties will block until the CSS styling is finished, you can measure the performance with code like so:

var start = new Date().getTime();
element.innerHTML = 'a ton of html and css';
var w = element.offsetWidth;
var end = new Date().getTime();
alert(end - start);

Thanks to my Nameless Coworker who shared those tips via email.

Remember: always measure before you attempt any performance optimizing. That way you can be sure your hard work has ultimately benefited the end user.

1 comment:

Anonymous said...

Online Buy Viagra usa Buy Viagra pfizer soft
[url=http://wikipatterns.com/display/~buy-cialis-genericoviagra]Online Buy Viagra canada Buy Viagra usa[/url]
Online Buy Viagra mexico Buy Viagra canada http://www-01.ibm.com/redbooks/community/display/~average_viagra_price