I have the following structure:
<ul id="test">
<li class="myclass">Item1</li>
<li class="myclass">Item2</li>
<li class="myclass">Item3</li>
<li class="myclass">Item4</li>
<li class="myclass">Item5</li>
</ul>
I want to hide the first three items. I wrote the following code but it only hides the first child and not the next two.
#test li:first-child
{
display:none;
}
How do I hide the other two also?
jQuery selector is described in the Selectors/nthChild docs, and the above can be accomplished with $("li. mybutton:nth-child(2)"). hide() .
You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.
You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <ul> element is not visible, but it maintains its position on the page. Removing the hidden attribute makes it re-appear.
You can use the nth-child selector:
#test li:nth-child(-n+3) {
display: none;
}
From the linked MDN doc:
Matches if the element is one of the first three children of its parent
Use CSS3:
#test li:nth-child(-n+4)
{
display:none;
}
Keep in mind that this property is supported in all major browsers, except IE8 and earlier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With