I have a navigation menu that I'd like to hide the first item in it. eg
<ul>
<li>Home</li>
<li>About</li>
<li>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
</ul>
I then hide the first element like this in CSS:
li:first-child{
visibility: hidden;
}
But then what happens is that all the first li items in the navigation disspears, like 'Item 1' in the navigation menu. How would I select only the 'home' li item and hide it?
Give your first ul a class "yourclass", then .yourclass > li:first-child { visibility: hidden; }
.yourclass > li:first-child { visibility: hidden; }
To hide the first child of the second ul use .yourclass > li > ul > li:first-child
Assign a id to the first ul and then apply the css style to that id only?
<ul id="someid">
<li>Home</li>
<li>About</li>
<li>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
</ul>
#someid > li:first-child{
visibility: hidden;
}
ul > li:first-child { (note you can do :last-child to target the last item)
display: none;
}
Another option, especially if you need to target other children for other reasons is to use :nth-child(). Inside the parentheses you put a number http://www.w3schools.com/cssref/sel_nth-child.asp
Using nth-child to mimick the same as the above.
ul > li:nth-child(1) {
display: none;
}
Note that nth-child is not supported by IE without a jquery library like modernizr to help it out.
<ul id="abc">
<li>Home</li>
<li>About</li>
<li>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
</ul>
#abc > li:first-child{
display: none;
}
I would consider using:
body> ul>:first-child {
display: none;
}
Rather than:
ul li:first-child{
visibility: hidden;
}
You would need the element or class which is above the first ul, then specify
css soluce : {parent-selector} > ul > li.
jQuery soluce : $( "ul > li" );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.