Is the :last-child selector bad for performance?

前端 未结 3 2107
迷失自我
迷失自我 2021-02-09 05:47

I use :last-child selector plenty of times, mostly when using border-bottom in a list where I use border: none; for the last child, or whe

3条回答
  •  迷失自我
    2021-02-09 06:27

    If it was deferred from CSS2 for performance concerns, but reintroduced in Selectors 3, I suspect that it's because performance is no longer an issue as it previously was.

    Remember that :last-child is the definitive and only way to select the last child of a parent (besides :nth-last-child(1), obviously). If browser implementers no longer have performance concerns, neither should we as authors.

    The only compelling reason I can think of for overriding a border style with :first-child as opposed to :last-child is to allow compatibility with IE7 and IE8. If that boosts performance, let that be a side effect. If you don't need IE7 and IE8 support, then you shouldn't feel compelled to use :first-child over :last-child. Even if browser performance is absolutely critical, you should be addressing it the proper way by testing and benchmarking, not by premature optimization.

提交回复
热议问题