views:

275

answers:

2

Hey Everyone,

So I have some nested lists (only one level deep) and I'm running into trouble with the CSS :hover feature. I only want the hover to apply to the parent class, but I can't figure that one out.

Here's my CSS

<style type="text/css" media="screen">
.listblock li img {
   visibility: hidden;   
}  
.listblock li:hover img {
   visibility: visible;
} 
</style> 

And here is a sample of one of the lists.

<ul>
    <li>One <a href="#"><img src="img/basket.png" height="16" width="16" alt="Buy" class="buy" onClick="pageTracker._trackEvent('Outbound Links', 'Amazon');"/></a></li>
    <li>Two <a href="#"><img src="img/basket.png" height="16" width="16" class="buy" /></a>
        <ul>
            <li>Uno<a href="#"><img src="img/basket.png" height="16" width="16" class="buy" /></a></li>
            <li>Dos <a href="#"><img src="img/basket.png" height="16" width="16" class="buy" /></a></li>
        </ul>
    </li>
    <li>Three <a href="#"><img src="img/basket.png" height="16" width="16" alt="Buy" class="buy" onClick="pageTracker._trackEvent('Outbound Links', 'Amazon');"/></a></li>
</ul>

The problem is that the image in the Uno and Dos list items also hovers. :(

Help please! Thanks a lot

+1  A: 

You can add something like:

.listblock li:hover li img {
   visibility: hidden;
}
jeroen
+1  A: 

You can use the child selector >, like this:

.listblock > ul > li:hover img {
   visibility: visible;
} 

It's not in your pasted code, so this assumes your <ul> is immediately wrapped by a class="listblock" item, if there's something else in-between just add it in with the same format. This selects only the direct child <il> that's a direct child of a <ul> that's a direct of .listbox, so it won't work on the <li> elements further down.

Nick Craver
although I'm pretty sure this doesn't work in IE6 ? Who really cares, this is definitely the *better* way to do this, target the exact element you're looking for instead of jeroen's which just adds unnecessary code
brad
hover on the li won't work in IE6 anyway...
CurtainDog
Correct :) If you're using `:hover` off an anchor you've pretty much said screw IE6 anyway, which I applaud you for :)
Nick Craver
@brad, that's not entirely true, with this solution all img's will show when you hover over a parent level li. You will need to target the images precisely to avoid that and then you will need an extra rule just the same to target the second level list items on hover. If they need to hover on their own that is, that's not entirely clear from the question.
jeroen
@jeroen In this case, Nick's code targets only the child ul of .listblock (not descendants, note the difference in the docs:http://www.w3.org/TR/CSS2/selector.html#child-selectors). You're right that all images in the li will hover (his html only has 1 per li anyway), but ONLY from the first level ul, not any underneath. That's what the .listblock > ul is doing. Of course this also assumes that the ul is a child of .listblock which the html doesn't specify, but seems to be the case.
brad
@brad, his item **Two** has 3 images in the li so all 3 will show when you hover over **Two**. The problem is that there are two ways to read the OP's question: 1. Only wants hover effect of first level li and 2. Has a problem with images in second level popping up when hovering over 1st level. I assumed the second, Nick Craver and you the first. Reading the question literally, you both are right; reading the code, I think I might be...
jeroen
@jeroen, fair enough... so a solution to your 'option 2' would be more child selectors before the image:.listblock > ul > li:hover > a > img {visibility: visible;} using this, only images that are direct children of "one" and "two" will be visible when their li's are hovered.This way you don't need extra statements or need to 'override' other selectors with the opposite effect.This could also be made significantly smaller by just applying a class to the img. Something like:.listblock li:hover img.topLevel
brad