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.
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
- Adding a timeout to a render function in ReactJS
:last-child is CSS3 and has no IE support while :first-child is CSS2, I believe the following is the safe way to implement it using jquery
One alternative for IE7+ and other browsers may be to use
:first-child
instead, and invert your styles.For example, if you're setting the margin on each
li
:You could replace it with this:
This will work well for some other cases like borders.
According to sitepoint,
:first-child
buggy, but only to the extent that it will select some root elements (the doctype or html), and may not change styles if other elements are inserted.2015 Answer: CSS last-of-type allows you to style the last item.
If you know there are three li's in the list you're looking at, for example, you could do this:
In IE6 you can use expressions:
I would recommend using a specialized class or Javascript (not IE6 expressions), though, until the
:last-child
selector gets better support.I usually do this by creating a htc file (ex. last-child.htc):
And call it from my IE conditional css file with:
ul { behavior: url("/javascripts/htc/last-child.htc"); }
Whereas in my main css file I got:
Another solution (albeit slower) that uses your existent css markup without defining any .last-child class would be Dean Edwards ie7.js library.
$('li').last().addClass('someClass');
if you have multiple