tags:

views:

123

answers:

2

Example:

<ul class="mybuttons">
   <li class="mybutton"></li>
   <li class="mybutton"></li>
   <li class="mybutton"></li>
</ul>

Is it possible to hide the 2nd item using css?

+7  A: 

n-th child pseudo selectors do this, but they're not widely supported yet and won't be for a while. You'll either need Javascript / jQuery or to write out a special class for the items you want to hide or just hide the items directly.

Here's how you'd do it with jQuery:

$("ul.mybuttons li:nth-child(2)").hide();
Jon Galloway
Thanks for the reply Jon. To clarify...JQuery has internal procedures that process CSS3 pseudo selectors passed as parameters thus making this solution back browser compatible? I only have access to the CSS for the purpose of this question but it's worth verifying my understanding for future projects. Thanks again.
GollyJer
Yes, jQuery has a very advanced selector engine that handles CSS3+ and is backward compatible to IE6.
Jon Galloway
+4  A: 

nth-child is indeed the CSS way.

In pure CSS, the syntax is simply

li.mybutton:nth-child(2){
   display:none;
}

nth-of-type(2) works in this case too.

Edit: Though this is the CSS answer, as noted, this is CSS3 and implemented only in some browsers. IE and FF3 and below do not support this natively. Implemented in FF3.5, Konqueror, and incorrectly in Chrome, Safari, and Opera. nth-of-type() implementations are better.

Support in older browsers will require javascript (simplified with jQuery, et al). jQuery selector is described in the Selectors/nthChild docs, and the above can be accomplished with $("li.mybutton:nth-child(2)").hide().

bill weaver
Worked perfectly. Thanks Bill. For the record this is a CSS3 implementation and, as Jon Galloway states, is only supported by 'modern' browsers.http://www.webdevout.net/browser-support-css#css3pseudoclasses
GollyJer
I don't think this is a real solution right now unless you can require your users to run the newest versions of non-IE browsers.
Jon Galloway
@Jon, it is a real solution to *his* question. He asked for CSS and tagged it css, so i gave him the CSS answer. But, i'll add a caveat to the answer.
bill weaver
bill - I agree, hope I didn't sound argumentative. Can't wait for wide browser support of CSS3!
Jon Galloway
@Jon, no, no worries. Yep! It will be nice when CSS3 is more widely adopted. Makes things *so* much easier.
bill weaver