3

Consider following HTML, I have a div class magic inside main. The magic class is hidden by default (with CSS display:none). I want to show the div class magic if the mouse goes anywhere in the main div. Can this be done with CSS only? or it has to use jQuery?

<div class="main">       
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
        <div class="magic">
            Hello, world
        </div>
</div>

CSS:

.main{  
    width: 400px; 
    border:1px solid red;
}

.magic{
    display:none; 
    margin-top:10px; 
    background:yellow; 
    padding:5px;
}

jsFiddle: http://jsfiddle.net/uRrn8/1/

Thanks for any help.

4 Answers 4

4
.main:hover .magic{  
     display:block;
}

That should do it for you in most browsers. However IE6 can be very strict about not supporting the hover psuedo class for anything other than links. If you wish to support IE6 you will have to use a javascript helper function to fire the mouse enter events and a CSS class you can append to the items. Examples of this technique are available here.

Sign up to request clarification or add additional context in comments.

2 Comments

One side note is that this will not work in IE6 unless you use a fix such as blog.delivi.com/javascript/…
That is true. IE6 is very spotty on their support for psuedo classes like hover. I am updating it to include your link. Thank you for reminding me.
2

yes write like this:

.main:hover .magic{  
    display:block
}

check this http://jsfiddle.net/sandeep/uRrn8/2/

3 Comments

One side note is that this will not work in IE6 unless you use a fix such as blog.delivi.com/javascript/…
@rtpharry; every buddy knows that & whooooooooo cares IE6
probably your client or your clients customer that says "is that google" when you ask them what web browser they use.
2

With jquery you could do:

$('.main').hover(function() {
    $('.magic').css('display', 'block');
},

function() {
    $('.magic').css('display', 'none');
});

Comments

1

Updated your fiddle, http://jsfiddle.net/uRrn8/4/

.main{width: 400px; border:1px solid red;}
.main:hover .magic{display:block;}
.magic{margin-top:10px; display:none; background:yellow; padding:5px;}

The code above should work. When hovering your div with the class main (.main:hover) you tell the div with the class magic to show.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.