6

I have the following code:

ul.myList li{
     border-right: 1px dotted #000;
}

However, on the last element, I need to remove that border as the design that I am working from dictates that the last item does not require a border as a separator.

So, I need to target the last child of a list and so within my css I have added

ul.myList li:last-child{
     border-right: none;
}

Which as we all know, works fine in Firefox, Safari and Chrome.

The problem lies when we view the page in Internet Explore 6 through to 8.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ant Ali
  • 380
  • 2
  • 3
  • 12

4 Answers4

11

So, after some digging around, I found the answer:

If the browser is IE<8, specify a stylesheet like this:

<!--[if lt IE 8]>
<link rel="stylesheet" href="css/ie_all.css" type="text/css" />
<![endif]-->

And within your IE stylesheet specify the following rules:

ul.myList li{
     border-right: expression(this.nextSibling==null?'none':'inherit');
}

The nextSibling expression looks to see if there is an element after it and if there is inherits the rule specified in the default stylesheet, if not it applys a new rule.

More information can be found here

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Ant Ali
  • 380
  • 2
  • 3
  • 12
  • 1
    +1 It's horrible, but it might just work. Plus one from me for coming up with a workable solution for IE. (And minus one million points for IE for making us come up hacks like this in the first place). Worth pointing out that IE9 (currently in beta) may support it, so you may want to adjust the [if IE] to be for IE8 or lower only. – Spudley Sep 24 '10 at 11:19
  • Thanks for the reply, I wasn't 100% if IE 9 would be supporting it. I will adjust my post to reflect for IE 8 and below. – Ant Ali Sep 24 '10 at 11:21
  • 1
    IE9 WILL support it, according to their own documentation.. However reliable that is. – Kyle Sep 24 '10 at 11:21
  • @Kyle, their doucmentation is usually perfectly reliable. It's just their definition of 'support' and 'implemented' that's occasionally vague, and incomplete. – David Thomas Sep 24 '10 at 15:19
2

IE8< does not support this pseudo selector. Check the MSDN article for all supported features :)

You could take a look at this jQuery solution to Enable pseudo selectors in IE, or just leave it as is in IE.

Kyle
  • 65,599
  • 28
  • 144
  • 152
2

As Internet Explorer before version 9 (which is still in development) doesn't support :last-child selector at all, my best solution, unfortunately, would be to set a class or id on the last element in your list and try to select that.

Of course, if leaving the right border in for IE won't break the layout completely, you may want to leave your code as is, if you don't mind IE screwing up rendering just a little.

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
1

You can do this using jQuery. So instead of relying on CSS. Use jQuery Selectors to set the property of your last element. I understand that u havent tagged your question with it.

jQuery('ul.myList li:last-child').css("Key","value");
Kyle
  • 65,599
  • 28
  • 144
  • 152
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128