tags:

views:

39

answers:

2

hey,

if I set visibility:hidden on a nested li element, how do I set it back on hover?

eg.

#menu li ul li {
visibility: hidden;
}

I tried:

#menu li ul li:hover {
visibility: visible;
}

But it doesn't work - so clearly I haven't got the syntax right!

cheers

+3  A: 

visibility: hidden hides the element and leaves no hoverable surface, so there will never be a hover event triggered.

Try opacity: 0 (or even opacity: 0.00001, not sure right now whether the surface remains with 0) to get the desired effect. Note that IE < 8 needs special treatment (filter: alpha(opacity=0))

Other browsers need other opacity settings as well, check out @Nick Craver's link for a full list.

Pekka
+1 - This is correct, though you'll need to include some [other opacity values for various browsers](http://css-tricks.com/css-transparency-settings-for-all-broswers/)
Nick Craver
@Nick good point, you're right of course, it's not just IE that can't always deal with `opacity`. I added a note to that effect.
Pekka
this won't work in ie6 as lis do not allow hover - this would need to be applied with JS in ie6
matpol
@matpol that, or using whatever:hover: http://www.xs4all.nl/~peterned/csshover.html
Pekka
This seems like an unnecessary hack to me? Am I crazy? :/
ewwwyn
@eww you're right, your way is much better if the OP can use it in their layout. (There may be some cases where it can't be applied.) You could still be crazy however, I can't judge that! ;)
Pekka
@matpol - I think the rate of supporting IE6 for *new* projects is dropping off rapidly, which is a good thing for everyone. Don't convolute your page to support it unless you know you need that audience IMO.
Nick Craver
Awesome thanks guys - makes sense now that it is explained!Thanks heaps!
unhitched
+2  A: 

Why not add a child wrapper in each <li> like this (could be a p or a div):

<li><p>dadada</p></li>

Then, for styling:

#menu ul li p {
 visibility: hidden;
}

#menu ul li:hover p {
 visibility: visible;
}
ewwwyn
+1 This is going to be the best and most easy way in most scenarios.
Pekka
+1 absolutely elegant
Stephan Kristyn