0

I want my menu to be hidden and when i hover an image i would like the menu to appear. I've tried following something that i've seen here in Stackoverflow but it's not working. How do i do it only with css ?

Html:

     <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>                        
          </button>
          <a class="navbar-brand" href="#">Test</a>
        </div>
        <div>
          <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
              <li><a href="#section1">Section 1</a></li>
              <li><a href="#section2">Section 2</a></li>
              <li><a href="#section3">Section 3</a></li>
    </ul>
          </div>
        </div>
      </div>
    </nav>    
<img src="img/ea.PNG" class="img-responsive img-center" id="imgmenu">

CSS:

.navbar{
    display:none !important;
}

#imgmenu:hover + .navbar{
    display:block;
}
user3375665
  • 73
  • 1
  • 2
  • 9
  • You missed the `` element here, I guess you didn't in your project right ? – Anwar Jul 21 '15 at 11:11
  • I forgot it here... I've already updated it – user3375665 Jul 21 '15 at 11:18
  • Possible duplicate - http://stackoverflow.com/questions/1817792/is-there-a-previous-sibling-selector So, currently not possible. The image must come **before** the menu **and** share a common parent. – Paulie_D Jul 21 '15 at 11:18
  • 1
    Or use jQuery `$('#imgmenu').hover(function () { $('.navbar').toggle(); });`, note that you'll need to drop the !important rule to use this code. – GreyRoofPigeon Jul 21 '15 at 11:22

1 Answers1

1

First you must place the image before the Menu , look this example (without js):

.navbar{
    display:none;
  margin-top: -10px;
  padding-top: 10px;
  position: relative;
}

#imgmenu:hover + .navbar{
    display:block;
}
.navbar:hover{
    display:block;
}
<div id="main-menu">
<img src="http://www.shoredreams.net/wp-content/uploads/2014/02/show-menu-icon.png" class="img-responsive img-center" width="50px" height="50px" id="imgmenu">  
<nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>                        
          </button>
          <a class="navbar-brand" href="#">Test</a>
        </div>
        <div>
          <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
              <li><a href="#section1">Section 1</a></li>
              <li><a href="#section2">Section 2</a></li>
              <li><a href="#section3">Section 3</a></li>
    </ul>
          </div>
        </div>
      </div>
    </nav>    
</div>
Jose Paredes
  • 3,882
  • 3
  • 26
  • 50