Thinking in programming

Things I like to talk about programming

Posts Tagged ‘optimize css selectors

Deepening at CSS selectors

leave a comment »

It is always good to ask ourselves if the code we write can be improved in some way – is it possible to improve the quality and/or its performance?. In this case, I’m going deepen a bit in the way the browsers read the CSS “code”.

Web browsers use a engine in charge of the process of rendering the DOM elements. For each element, the engine looks at which CSS styles should be applied to it (according to the CSS rules matching it.) Each rule is evaluated by parts, from right to left (yes, in reverse). Starting from the one located on the far right (called the “key”, because it indicates which elements should be chosen) and sequentially moves through each part until it reach the one located at the far left, or alternatively, until the rule is discarded for that item (if it is concluded that there is no match).

Under the previous system, the fewer rules have to evaluate the engine and the more specific are these rules, it will be better (in terms of performance).

CSS Selectors are classified accordingly to their efficiency in the following groups (in order from most to least efficient):

  1. #element_id (selector by ID) The ID are uniques by definition, so, this is the fastest rule.
  2. .main_menu (selector by class)
  3. span (selector by tag)
  4. Universal Rules – All the others rules, goes inside this category

For example:

<ul id="”ul_id”" class="”ul-class”">
  <li id="”li_id”" class="”li-class”">
    <span id="”span_id”" class="”span-class”"></span>
  </li>
  <li>
    <p></p>
  </li>
</ul>

Supposing it is desired to put in red-color the text inside the span tag, then some possible selectors are the following (from the most to the least efficient):

/* Usin this selector, the browser will look element by element until it finds one with this ID */
#span_id {
  color: #ff0033;
}

/* In this case, it will look in all elements and it will select the ones having this class */
.span-class {
  color: #ff0033;
}

/* Here, it will look in all elements and it will select the span tags */
span  {
  color: #ff0033;
}

/* This time it will look in all the elements and it will pre-select the ones having this class, following that, it will inspect the parent of each pre-selected element and then filtering the elements being childrens of an element having this ID */
#li_id > .span-class {
  color: #ff0033;
}

/* Using the following selector, it will look in all the elements and it will pre-select the ones having this class, following that, it will inspect the ancestors of each pre-selected element and then it will filter the descendant elements of one element having this ID */
#li_id .span-class {
  color: #ff0033;
}

Conclusion:

  1. #element_id (selector by ID)
    Uses the element’s ID always possible.
  2. ul#main_menu (selector by tag and ID)
    Do NOT add a tag or any other selector as the prefix of a class if it is not necessary, such thing would only adds redundant information which would be also evaludated, slowing down the process.
  3. .main_menu > li (child selector)
    Child selectors are inefficient, because of, by each element matching the criteria indicated by the key, the browser must evaluate the parent of this, which results in a double expensive process. At less specified is the key, greater will be the number of nodes to be evaluated. It should be clarified that, in terms of performance, it is still preferable to use child-selectors instead of descendant-selectors.
  4. .main_menu li (descendant selector)
    Descendant selectors are very inneficients because of, by each element matching the criteria indicated by the key, evaluating each one of its ancestors through the DOM tree until it found a matching or until it reaches the root element. Again, at least specified is the key, the number of nodes to be evaluated will be greater.
  5. At last but not at least, it is necessary to keep in mind that there should be a balance between readibility and performance. In short, it is generally preferable to sacrifice a bit of speed in favor of readibility and manteanibility of the code. At the same way, it is good to keep present that a mobil phone will not interpret a web page with the same speed than a desktop computer

You can find the Spanish version of this article here.

References:
Mozilla – Writing efficient CSS
Google – Optimize browser rendering

Written by roger.padilla

June 2, 2010 at 22:27