Aweber - Email Marketing Made Easy
How To Get the CSS3 :last-child Pseudo Class To Work in IE Photo by: kiwins

How To Get the CSS3 :last-child Pseudo Class To Work in IE

November 2, 2010 · 1 comment

in Thesis

Big surprise. Here’s just another thing that IE8 doesn’t support. There ought to be some sort of law against producing a web browser that doesn’t support web standards. The css3 :last-child pseudo-class is supported in IE9, but if you need to control the styling on the last item in a list for IE6 to 8, then you have a problem.

You could hard code a class of ‘last’ on the last list item and then target that class in your css. But what about a list that is generated dynamically in WordPress such as a menu? I’m working on a site for a client and there is a menu toward the bottom of the page near the footer. I have a 1px border between each list item, but it just doesn’t look right to have the line after the last list item.

Of course, the right way to do this is simply to use something like this in your css code:

.custom #menu li:last-child { border-right:none; }

But if you want that border to not show up in IE, you’re out of luck. So one solution would be to use a tool called selectivizr, but I came up with this quick method using jQuery which just works.

We’re going to use jQuery to insert a class of ‘last’ or ‘last-child’ or whatever you want on the last item in the list. Insert this code into your custom-functions.php file:

function last_child() {
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script>
 $(document).ready(function() {
	$("#nav li:last-child").addClass('last');
 });
</script>
<?php
}

add_action('thesis_hook_after_html','last_child');

This will add a class of ‘last’ to the last list item in the list with an ID of #nav. Now you can simply style this as follows:

.custom #nav .last { border-right:none; }

Important: Don’t reload jQuery if you already have it referenced elsewhere, so you would not include line #3

You can learn more about CSS3 browser support here.

Was this post helpful? Consider subscribing to my blog via RSS or

This Site Runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced user, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 1 comment… read it below or add one }

Waqas Sher June 8, 2011 at 10:58 pm

Thanks Brother. It help me a lot to teach Internet Explorer how to handle the last-child :D

Reply

Leave a Comment

When commenting, you can use basic HTML tags. If you are pasting in any code, please escape it here and then include that code within <pre></pre> tags. Thanks!

CommentLuv badge

Previous post:

Next post: