Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Created May 1, 2011 19:26
Show Gist options
  • Save nathansmith/950767 to your computer and use it in GitHub Desktop.
Save nathansmith/950767 to your computer and use it in GitHub Desktop.
Force IE7 & IE8 to respect :last-child
// Heavy-handed way to force IE7 & IE8 to respect :last-child.
// Note: Using jQuery. Adjust to suit your library, etc.
//
// Style via...
//
// element:last-child,
// element.last-child {
// /* Style here */
// }
//
// Yes, I realize this is not performant. But IE7 & IE8
// suck. Those users are probably used to sluggishness.
function last_child() {
if ($.browser.msie && parseInt($.browser.version, 10) <= 8) {
$('*:last-child').addClass('last-child');
}
}
@tzi
Copy link

tzi commented Mar 6, 2013

@webmediahelden
The first-child selector is a part of the CSS2 specification.
So it's available since IE7 ;)

@bytehead
Copy link

With the new jQuery >= 1.9.0 $.browseris not more supported. (http://api.jquery.com/jQuery.browser/)

I created a new Gist, which is jQuery 1.9.x compatible:
https://gist.github.com/bytehead/5748928

@oktak
Copy link

oktak commented Mar 19, 2014

element:last-child,
element.last-child {
    /* Style here */
}

This CSS rule will be skipped in IE7, 8.
IE 7, 8 would skip the whole rule when there are :last-child exist.

It's better to separate into two rules, ie:

element:last-child{
    /* Style here */
}
element.last-child {
    /*  and Style here */
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment