Does the length of the css selector affect efficiency?

does the length of the css selector affect efficiency?
such as

.grid_col4 .user .user_avatar a 

and write directly

.user_avatar a 
Sep.16,2021

performance impact I think can be ignored, after all, the code will be smaller when packaged. But the weight of the selector is influential, for example, if you add multiple Class, and id, to the tag and write styles through future generations or sub-selectors, then you should pay special attention to the influence of weight, which may lead to some styles not working.


css selector lookup is from back to front, and the deeper the level, the worse the performance, which is one of the css optimizations. The efficiency of


is negligible, but your code will become larger and less readable!


The effect of

on efficiency is negligible. If the same effect can be achieved, the shorter it is, the more convenient it will be.

but the priority of the two selectors is different, and sometimes the length of the selector is deliberately increased in order to increase the priority.


the general writing style is class,. If it is js, it generally adopts id. Of course, it depends on what framework you use. Now it's all V8 engines, and what you're talking about is basically negligible.


it doesn't matter if you have a single item. Maybe 5000 items are stacked together before you start to consider the performance impact.
but .grid _ col4 .user _ avatar a is more intuitive than .user _ avatar a , and the short selector can easily stack selectors to raise priority when priority conflicts are involved. So I think it doesn't matter that the latter improves development efficiency, performance and so on.


there must be, but the time is too short to be ignored, but there is a large amount of code redundancy


it is not easy to read and maintain


of course it does.

  1. the browser must have a lot of computation to find dom backtracking according to your iterator level, no matter how slow it is.
  2. Selector is written for such a long time, and it is not easy to maintain in the future, which is not good for reading
  3. .
  4. as an aspiring developer, he pursues clarity, simplicity and elegance of the code.

influence, css parses from back to front, and the deeper the nesting level, the more it affects performance

.
Menu