Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am aware than ID rules are faster than class rules, which are faster than tag rules, which are faster than universal rules, as noted on MDN. My question pertains to CSS performance with respect to inheritance. For example, which of the following is more efficient?

body {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 16px;
}

h1 {
  font-family: Georgia, serif;
  font-size: 36px;
  font-weight: 700;
}

or

h1 {
  font-family: Georgia, serif;
  font-size: 36px;
  font-weight: 700;
}

.article-text {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 16px;
}

While there is only one <body> tag in the DOM, there could be hundreds of p.article-text elements. Since there is only one <body> tag, does that mean that a <body> style is more efficient, even though I'd be restyling the <h1> elements? Or is it more efficient to style the .article-text elements, since doing so would use a more specific selector and I wouldn't need to worry about restyling the <h1> elements?

Always wondered this. Thoughts?

share|improve this question
    
Do a benchmark and find out? –  cimmanon Jul 20 '13 at 0:49
1  
I'd guess that the "cascading" of CSS (inheriting styles from parent elements) is highly efficient. This doesn't require a selector at all. My understanding is that body should be styled the way you want the main body of plain text within the document styled -- that what it's the container for. –  Thomas W Jul 20 '13 at 1:04
3  
In the grand scheme of page performance, it's not worth worrying about (or at least not worth worrying about until you've addressed many other factors: concatenation, minification, etc.) –  steveax Jul 20 '13 at 1:41
    
@Thomas W: Cascading has a very specific meaning in CSS. See w3.org/TR/CSS21/cascade.html. Cascading is cascading, and inheritance is inheritance (although one could say that inheritance is part of the cascade). –  BoltClock Jul 20 '13 at 19:23
    
I know this is about details, but even with insane CSS sheets with thousands of many dynamic bindings like div[^="someField"] { }, ... #someField1314 {} modern browsers (even on phones) do this blazingly fast. Also notice that once the CSS file is cached, it doesn't matter if it's hundreds of kilobytes. I build a heavy CSS animation-driven website some time ago which the CSS files together were over 3 MegaBytes big (compressed :P). I just used an AJAX loader once (a bit like Flash clips do). Once cached every page load was instant :) I would sweat more over big JavaScript chunks ;) –  Allendar Jul 20 '13 at 21:22

3 Answers 3

up vote 2 down vote accepted

I think that the MDN article you showed is written in a harmful way, as it proposes to focus on CSS micro-optimizations while almost always page speed can be increased in different ways (eg. Google Page Speed has interesting hints).

I can't remember any situation in my websites where CSS was a bottleneck. You might want to see "Timeline" in Chrome Developer Tools on some pages and notice that the amount of time spent on working with styles is usually insignificant compared to other events.

If I was to give advice, I'd rather go the other way and use CSS preprocessing tools like SASS or LESS to improve maintainability. This might even eventually help to reduce number of rules.

share|improve this answer

As mentioned by steveax. It's not worth worrying about. Moreover, the answer really depends on the browser and on the HTML actually given.

share|improve this answer

i would say the top one is probably faster out of the two but tbh like everyone has said with the browsers we have ATM and devices and connections speeds you wouldn't notice a difference unless you have a huge site that has more than 40 - 50 pages that's when you should look into slim lining your code

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.