1

I know:

div > p

is faster to render than

div p

but, in the other hand, it occupies one more character, so it increase the time to send the CSS file.

I know the speed difference is very little, but if you have a very large CSS file with a lot of selectors it can start being important.

So, my question is: what is better, to lose some time rendering and do not use child selectors or use child selectors and lose some more time sending the CSS file?

animuson
  • 53,861
  • 28
  • 137
  • 147
Aljullu
  • 434
  • 3
  • 12
  • Until you actually measure it and demonstrate that it is a noticeable hindrance to the user, why bother? – Diodeus - James MacFarlane Nov 17 '11 at 20:02
  • "if you have a very large CSS file with a lot of selectors it can start being important" Only for IE. – BoltClock Nov 17 '11 at 20:48
  • 2
    If you are this worried about bytes (I mean they're just a few **bytes**; as no-brainer as it sounds it takes **a thousand requests** of an extra byte to reach a single **kilo** byte), realize that `div>p` works just as well. No extra space characters. – BoltClock Nov 17 '11 at 20:49

2 Answers2

2

Div > p and div p are not the same

if you have the following structure:

<div>
  <p id="p1">first p</p>
  <section>
     <p id="p2">second p</p>
  </section>
</div>

div > p will apply to only p1, while div p to both p1 and p2.

The speed of the selectors will depend on your html structure.

Emil
  • 8,449
  • 3
  • 27
  • 44
1

You should distinguish between loading the data from the server and parse the HTML+CSS.

at loading time you right (it will be slower)

but on render you are wrong (it will be faster)

P.S. don't forget: once you have the CSS - it won't be downloaded again.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • ok. I hadn't thought about the cache. so, maybe it's better to use efficient CSS selectors despite it slows a bit the download. – Aljullu Nov 17 '11 at 20:53