tags:

views:

76

answers:

4

In this condition is it possible to not to apply margin-right to last li.

I need pure css way and support in IE6 and 7 also and without changing HTML. is there any way to achieve this.

ul li {display:inline;margin-right:10px}

<ul id="nav">
    <li><a href="#nowhere" >Lorem</a></li>
    <li><a href="#nowhere" >Aliquam</a></li>
    <li><a href="#nowhere" >Morbi</a></li>
    <li><a href="#nowhere" >Praesent</a></li>
    <li><a href="#nowhere" >Pellentesque</a></li>
</ul>
A: 

Give the last <li> a class and then apply margin-right: 0px to the class.

There is a :last-child pseudo class in CSS3 but won't work in IE 6 or 7.

rahul
no I need without changing HTML
metal-gear-solid
Can you use javascript?
rahul
A: 

Normally I'd advise using the :last-child selector, but IE 6 and 7 do not support this.

You could add a class to the last li element and apply a style like margin-right: 0 to it.

Without changing the HTML, I'm not aware of any pure css way to make that work in IE 6 or 7.

Syntactic
+1  A: 

You could put a negative bottom margin on the tag. I don't even know if that would work, but it's worth a try.

Joe Mills
negative bottom margin??
metal-gear-solid
`margin-bottom: -10px;` where 10px is the size of the margin on the `li`.
AaronSieb
You'd want to do that with the IE 6 hack or in an IE 6 specific style sheet of course.
Joe Mills
+4  A: 

You can use IE CSS expressions to achieve this.

ul#nav li {
    margin-right: 10px;
    /* for IE only: */
    margin-right: expression(this.nextSibling == null ? '0' : '10px' );
}

/* for standards browsers: */
ul#nav li:last-child {
    margin-right: 0;
}

Tested and it works in IE7. I don't have access to IE6 but it should work the same.

If possible, it would be best to put the expression rule in an IE-only stylesheet.

DisgruntledGoat
in place of expression i would like to use javascript
metal-gear-solid
Why? You asked for a pure CSS solution, that's what you got.
DisgruntledGoat
will expression work without javascript? and why i do not prefer expression see here http://developer.yahoo.net/blog/archives/2007/07/high_performanc_6.html
metal-gear-solid
Well sorry but you can't support ancient relics like IE6 without some compromise. One expression wouldn't make a massive difference and it doesn't apply to IE8, Firefox and other browsers.
DisgruntledGoat