how to select the last div that has class 'visible' in a container using CSS selector?
Hi guys, i want to select the last div has class visible using css selector. Is this possible?
Here is my example:
8 Replies
It not work
It works but I dont really understand the selector. As I understand it will go through bottom to the top of the container and
of
keyword means the child has class .visible
right?Honestly no idea i just used https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child and did trial and error to get expected result. I'm really bad with the nth selector but somehow managed to make it work 😛
MDN Web Docs
:nth-last-child() - CSS: Cascading Style Sheets | MDN
The :nth-last-child() CSS pseudo-class matches elements based on their position among a group of siblings, counting from the end.
thanks to Kevin's latest video i found simpler solution
basically this goes from the bottom and selects only first item that it finds with the class visible
So changing that "1" to a "2" would select the penultimate element with the class .visible, interesting!
I need to explore the "of" syntax more 🤔
Be careful: only 93.23% of browsers support this. Dont use it on "big" pages