DEV Community

[Comment from a deleted post]
Collapse
 
pheeria profile image
Olzhas Askar

An explanation would be really helpful!

Collapse
 
arberbr profile image
Arber Braja • Edited

:first-child and :last-child are called psuedo-selectors in CSS.

So in the first example shown he has shown the default style an image should have then has overwritten it.

Second example, or the correct one as he has stated it, he didnt overwrite antything. He directly used psuedo-selectors to specifically target only what he really wanted to target.
Code on the second example is shorter, also what is good is that the browser rendering engine will not have to calculate and re-calculate this elements since nothing is overwritten.

First example and second example both achieve the same thing, the second example is better for the reasons explained above.

Another good example of using :not psedo-selector would be if you want to have some kind of realtime UI validity output for the user if he has left a certain required field empty or invalid.