Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested selectors performance impact and LESS

I have been reading for the past 1.5 hours about this and still couldn't find a concise and decisive answer.

As far as I understood browsers parse CSS selectors from right to left.

That means a long CSS selector such as this:

.card .container .businesscard .pinfo li.pinfo-box span:first-child

is one of the least efficient lines of code to ever appear here in SO.

First of all, am I right on this one?

Secondly, I am designing a rich UI using LESS, which ultimately produces this kind of mammoth selectors out of the nested designs I am coding.

What can be done to avoid this kind of selectors? Rely on classes and IDs alone? But then again what is the purpose of using LESS if you can't write nested CSS?

Your input is appreciated.

like image 380
pilau Avatar asked Dec 11 '12 23:12

pilau


People also ask

What is the role of nesting in less programming?

We can very easily define nested rules in LESS. Nested rules are defined as a set of CSS properties that allow the properties of one class to be used for another class and contain the class name as its property. In LESS, you can use class or ID selectors to declare mixin in the same way as CSS styles.

What are nested selectors?

Nesting is a shortcut to create CSS rules for multiple selectors for a specific property. So, instead of rewriting the same set of properties for the different selectors, we can simply nest selectors inside other selectors.

What is wrong with Sass nesting?

Nesting makes code complicated Both of the examples shown above use some selector functions (from Sass 3.4) to partially rewrite the current selector context ( & ). So if I'm not mistaken, both examples involve more code in order to write less code, all of this in addition to some extra complexity.


2 Answers

That is correct. Browsers evaluate selectors from right to left. It will try to find a span inside a li.pinfo-box and so on.

One rule of thumb to folllow when writing LESS is: do not nest more than 3-4 levels.

This will prevent your selectors from growing to big, while you will still be able to benefit from the nesting feature in LESS.

A good example of "useless" nesting is when styling lists. Sometimes I write the selectors like this:

#wrapper .blog-post ul, #wrapper .blog-post ul li

Is it really necessary to specify that the li must be inside a ul? It will probably be enough writing:

#wrapper .blog-post li

All of this is good to know. BUT: This is not the first thing to dive into when trying to optimize your sites performance. Spend some time lower the number of request or something else instead.

like image 121
AntonNiklasson Avatar answered Oct 23 '22 03:10

AntonNiklasson


Selector parsing and matching is unlikely to be a big factor unless you have pretty unusual content. I would suggest using whatever is maintainable and gets the job done, up until the point where testing shows a performance issue. Then I'd get out a profiler (on OSX I'd use Instruments, but there should be a decent one available for most platforms) and attach it to the browser; if selector matching shows up high on the profile, then look into replacing slow selectors with faster ones (id selectors are definitely a good bet).

like image 40
Catfish_Man Avatar answered Oct 23 '22 03:10

Catfish_Man