PageSpeed: Use efficient CSS selectors (deprecated)
Overview
Avoiding inefficient key selectors that match large numbers of elements can speed up page rendering.
How does your site score on this recommendation?
Details from Google
As the browser parses HTML, it constructs an internal document tree representing all the elements to be displayed. It then matches elements to styles specified in various stylesheets, according to the standard CSS cascade, inheritance, and ordering rules. In Mozilla's implementation (and probably others as well), for each element, the CSS engine searches through style rules to find a match. The engine evaluates each rule from right to left, starting from the rightmost selector (called the "key") and moving through each selector until it finds a match or discards the rule. (The "selector" is the document element to which the rule should apply.)
According to this system, the fewer rules the engine has to evaluate the better. So, of course, removing unused CSS is an important step in improving rendering performance. After that, for pages that contain large numbers of elements and/or large numbers of CSS rules, optimizing the definitions of the rules themselves can enhance performance as well. The key to optimizing rules lies in defining rules that are as specific as possible and that avoid unnecessary redundancy, to allow the style engine to quickly find matches without spending time evaluating rules that don't apply.
The following categories of rules are considered to be inefficient:
- Rules with descendant selectors
- Rules with child or adjacent selectors
- Rules with overly qualified selectors
- Rules that apply the
:hover
pseudo-selector to non-link elements