12

I have a navigation menu that I'd like to hide the first item in it. e.g.

<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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
  • 1
    add a class to your navigator ul – Pedro Estrada Mar 01 '13 at 13:41
  • Related to : http://stackoverflow.com/questions/977883/selecting-only-first-level-elements-in-jquery , http://stackoverflow.com/questions/3892111/how-to-select-only-top-level-li-in-recursive-menu , http://stackoverflow.com/questions/9084705/jquery-child-selector-only-select-top-level-child-elements – Milche Patern Mar 01 '13 at 13:45

6 Answers6

26

Give your first ul a class "yourclass", then .yourclass > li:first-child { visibility: hidden; }

To hide the first child of the second ul use .yourclass > li > ul > li:first-child

Merec
  • 2,751
  • 1
  • 14
  • 21
2

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;
}

Also see http://www.w3.org/TR/CSS2/selector.html

Adrian
  • 2,233
  • 1
  • 22
  • 33
Techmonk
  • 1,459
  • 12
  • 20
2
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.

Michael
  • 7,016
  • 2
  • 28
  • 41
1
<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;
   }
PSR
  • 39,804
  • 41
  • 111
  • 151