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 when using margins. So my question is, is the :last-child
selector bad from a performance point of view?
Also I've heard that it was removed from the CSS2 specification because using :first-child
is easy for the browser to detect, but for detecting :last-child
it needs to loop back.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With