0

I am trying to create an effect where if a user hovers over an element, the element disappears. I have tried the code below, but it seems that the display:none; breaks the CSS. I am wondering why my CSS does not work, and how I would solve my problem.

http://jsfiddle.net/2c42U/

<div class="foo">text</div> 

.foo:hover {
    color: red;
    display: none;
}
4
  • You can't hide element on hover with display: none. It doesn't work because it has no sense. Commented Mar 12, 2013 at 20:02
  • Do you mean you cannot apply display:none; to yourself? Commented Mar 12, 2013 at 20:03
  • You can, but it will not work. just think what should happen next? Div disappeared, should it appear again because it's no longe hovered? This is why it just does nothing. Commented Mar 12, 2013 at 20:03
  • @dfsq - That makes sense, but the same logic should apply to changing the position of an element when it is :hover'ed, and yet I can do that: jsfiddle.net/XtmVQ/2 I wonder if there's something in the specs which say you can't change display with a :hover pseudo-class... Commented Mar 12, 2013 at 20:17

6 Answers 6

10

Try changing the element's opacity instead: http://jsfiddle.net/XtmVQ/

.foo:hover {
    opacity:0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sweet! I got so fixated on trying to hide the element I forgot about just making the user think the element was hidden!
@icu222much Make sure you check that in various versions of IE. It needs "alpha-opacity" set instead of just opacity.
3

try this instead of display:none

visibility:hidden

Comments

1

Try this:

.foo:hover {
    opacity: 0;
}

What is your final intent for this?

Comments

1

as @Richard said, use opacity

also, to be backwards ie, do as follows:

filter:alpha(opacity=0); /* For IE8 and earlier */

and you can also add a transition:

transition: 0.5s

so that it is not instant.

Comments

1

You can do what the others suggested, use opacity: 0; or visibility:hidden;, but if you must have it hidden from the flow of the page. Then do the following:

Use a CSS like this:

.hidden{
    display: none !important;
}

You can use the class hidden and apply it to any element to hide it. For the hover behaviour you want, you'll require JavaScript/jQuery to apply the class name. See http://jsfiddle.net/rkH7F/

Comments

-2

i think css will not work. use jquery

UPDATE

ohh, my bad. css will work but the jquery will have a better effect

     $('#outer').mouseenter(function() {
       $("#outer").hide();
     }); $('#outer').mouseleave(function() {
       $("#outer").show();
     });

FIXED

     $('#outer').mouseenter(function() {
       $("#outer").slideUp();
     }); $('#outer').mouseleave(function() {
       $("#outer").slideDown();
     });

5 Comments

WRT "css will work but the jquery will have a better effect" - how is it a better effect?
.slideUp() .slideDown() .slideToggle() is what I mean, I got confused grr. sorry
What is #outer? Also, even your updated solution behaves strangely: jsfiddle.net/XtmVQ/3
for example ID of a div :)
Why not use the element identifier (.foo) from the OP's code snippet?

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.