Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide first 3 childs of UL using CSS

Tags:

html

css

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?

like image 215
Frank Martin Avatar asked Dec 09 '13 11:12

Frank Martin


People also ask

How do I hide the nth child in CSS?

jQuery selector is described in the Selectors/nthChild docs, and the above can be accomplished with $("li. mybutton:nth-child(2)"). hide() .

How do I hide a list item in CSS?

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.

How do I hide my UL tag?

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.


2 Answers

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

like image 56
CodingIntrigue Avatar answered Nov 10 '22 00:11

CodingIntrigue


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.

like image 42
Bud Damyanov Avatar answered Nov 10 '22 01:11

Bud Damyanov