I'm trying to learn to write more efficient CSS, particularly as I'm working with a fairly complex site that needs to render fast.
I'm used to having a lot of this in my HTML/CSS (mainly because I like the readability):
.spotlight {}
.spotlight ul {}
.spotlight ul li {}
.spotlight ul li a {color: #333;}
<div class="spotlight">
<ul>
<li><a href="">link</a></li>
<li><a href="">link</a></li>
<li><a href="">link</a></li>
</ul>
</div>
I now understand that browsers run the CSS rule matching process from right to left, meaning that the <a>
element in the last CSS rule above would first match every link on the page, leading to performance loss.
So from what I gather, the browser-friendly solution would be to be more specific, and use, for example:
.spotlight {}
.spotlight-link {color: #333;}
<div class="spotlight">
<ul>
<li><a class="spotlight-link" href="">link</a></li>
<li><a class="spotlight-link" href="">link</a></li>
<li><a class="spotlight-link" href="">link</a></li>
</ul>
</div>
(assuming I'm using inheritance where possible but often still need specific control over the last element down the tree)
What is causing me doubt is: doesn't all the extra HTML bloat from printing class names on elements throughout the page negate performance gains made from avoiding nested CSS child selectors? I'm used to trying to write less HTML and this sort of goes against it. Any insight would be appreciated.