1

I am having a problem with mouse out transition I have used a few examples from similar questions but had no luck as it is a slightly different case:

When the page loads I have a menu bar with one of the options triggering a hidden div:

.top-links div.musictest {
    display: none;
    position:absolute;
    float:left;
    }

The particular option when hovered triggers div.musictest to display as a block.

.top-links li.music:hover + div.musictest {
        display:block;
    }

So when this drop down div.musictest is displaying as a block to maintain it showing as it includes options itself to be clicked I have CSS to say:

.top-links div.musictest:hover {
     display:block;
    }

So here is my problem, from everywhere I have read if I want .top-links div.musictest to fade out when I stop hovering it needs a transition delay however when I put one in nothing happens as I believe display:none is stopping it from fading out. But without the display:none it shows the hidden div.musictest always.

At the moment the code above works fine if I want the hidden div to disappear immediately, but I am wanting it to have a transition time before disappearing.

Any ways around this or am I putting it in the wrong place completely?

Thanks!

5
  • 1
    Can you provide an example of your code? Commented Jul 9, 2016 at 13:22
  • That is all the css for the particular part. And here is the html for the same part: <div class="top-links"> <toplinks> <ul id="menu"> <li>Home</li> <li class="music"></li> <div class="musictest"> Commented Jul 9, 2016 at 13:27
  • can you put it in a fiddle, so i could see how it works and what doesn't? Commented Jul 9, 2016 at 13:28
  • jsfiddle.net/212zeg0u Well it all works when hover li class="music" it shows the div class="musictest" when hover div class="musictest" is maintains showing div class="musictest" when stop hovering div class="musictest" is disappears instantly I'm trying to make it delay for a second before it disappears. Thanks Shlomi Commented Jul 9, 2016 at 13:35
  • You cannot animate the display proeprty, that's why Commented Jul 9, 2016 at 13:45

2 Answers 2

1

You can set opacity: 0 instead of display: none

(Edit: I just read your title again, you can check this link for only fade out)

So here is an example with both:

.top-links li.music:hover {
  background-color: #EEE;
}
.top-links li.music:hover + div.musictest {
  display: block;
  opacity: 1;
}
.top-links div.musictest {
  opacity: 0;
  transition: 1s;
  -moz-transition: 1s;
  -webkit-transition: 1s;
  -o-transition: 1s;
  position: absolute;
  float: left;
  top: 44px;
  width: 800px;
  left: 22px;
}
.top-links div.musictest:hover {
  display: block;
}
<div class="top-links">
  <toplinks>
    <ul id="menu">
      <li>Home</li>
      <li class="music">Music</li>
      <div class="musictest">
        Show when hover li class="music"
      </div>
    </ul>
  </toplinks>
</div>

2
  • Great thanks! this is perfect is there a way to make it stop fading if mouse hovers back over like a second chance thing incase your mouse stopped hovering by accident? Commented Jul 9, 2016 at 18:46
  • The only thing I know of is that you could add a transition-delay: 2s so the fade out starts later, (but I am a newbie to Website design so there will most likely be another, better way too) And I moved the opacity: 1; to .top-links li.music:hover + div.musictestso it reacts on hovering on the <li> and not the <div>
    – A_A
    Commented Jul 10, 2016 at 7:13
0

I set the opacity of the div to zero so it won't be visible but it's still a in display mode block and when your mouse is out of the element it's start to fade out as you wanted.

Add this to your css:

.top-links li.music:hover + div.musictest {

       opacity:1;
       transition: opacity 0.1s;
       -moz-transition: opacity 0.1s;
       -webkit-transition: opacity 0.1s;
       -o-transition: opacity 0.1s;
    }

  .top-links li.music + div.musictest {
       opacity:0;
       display:block;
       transition: opacity 2s;
       -moz-transition: opacity 2s;
       -webkit-transition: opacity 2s;
       -o-transition: opacity 2s;
    }

And here is a working example

Not the answer you're looking for? Browse other questions tagged or ask your own question.