Is CSS faster when you are specific?

后端 未结 3 711
一生所求
一生所求 2020-12-13 23:49

Is div.container faster than .container ? You know like in jquery if you be more specific with your selectors it is faster since it iterat

3条回答
  •  盖世英雄少女心
    2020-12-14 00:17

    delta between the best case and the worst case was 50ms. In other words, consider selector performance but don’t waste too much time on it. See: https://smacss.com/book/selectors

    So I do not think it makes much sense to extend CSS rules ONLY to get a higher performance. Just consider the higher amount of network traffic, maybe worse maintainability, ... However in the link you can read, which rules to consider without having to increase the CSS size.

    If .container and div.container match exactly the same elements on your page, the first one might be even faster: If the browser evaluates .container at first, actually it would have been finished, but with div.container it has ADDITIONALLY to check, whether the element is a div.

    disclaimer: I do not know how browsers really implement these things. My conclusions are based on the linked article.

提交回复
热议问题