I am wondering if there is some way to change a CSS attribute for the last li in a list using CSS. I have looked into using :last-child, but this seems really buggy and I can’t get it to work for me. I will use JavaScript to do this if necessary, but I want to know if anyone can think up a solution in CSS.
I am wondering if there is some way to change a CSS attribute for
Share
:last-childis really the only way to do it without modifying the HTML – but assuming you can do that, the main option is just to give it aclass="last-item", then do:Obviously, you can automate this in the dynamic page generation language of your choice. Also, there is a
lastChildJavaScript property in the W3C DOM.Here’s an example of doing what you want in Prototype:
Or even more simply:
In jQuery, you can write it even more compactly:
Also note that this should work without using the actual
last-childCSS selector – rather, a JavaScript implementation of it is used – so it should be less buggy and more reliable across browsers.