Search code examples
htmlcssdrop-down-menuresponsive-designnav

How to change navigation drop down from hover to click


I'm making a responsive navigation bar that has a dropdown that expands when you hover over it. My problem is that this hover feature is not suitable for mobile devices. As such, I am wanting to change my navigation bar to expand on click instead of on hover.

I have tried using the checkbox method but cannot get it to work. I am wanting to only use HTML and CSS if it is possible. Any help will be greatly appreciated!

See it here: JSFiddle

HTML:

<ul id="navigation">
<li><a href="#">Product 1</a>
<ul>
<li><a href="#">rx.com </a></li>
<li><a href="#">Resources</a></li>
<li><a href="#">Copay Cards</a></li>
</ul>
</li>
<li><a href="#">Product 2</a>
<ul>
<li><a href="#">e.com </a></li>
<li><a href="#">Resources</a></li>
<li><a href="#">Copay Cards</a></li>
<li><a href="#">Informational Video</a></li>
</ul>
</li>
<li><a href="#">INSOMNIA EDUCATION</a>
<ul>
<li><a href="#">Complete Night Sleep Resources</a></li>
<li><a href="#">sleep.com </a></li>
</ul>
</li>
<li><a href="#">PAIN MGMT EDUCATION</a>
<ul>
<li><a href="#">Safer Pain Management Resources</a></li>
<li><a href="#">painmanagement.com </a></li>
</ul>
</li>
<li><a href="#">MEDICAL AFFAIRS</a>
<ul>
<li><a href="#">Request Clinical Reprints</a></li>
<li><a href="#">Schedule a Scientific Session </a></li>
</ul>
</li>
</ul>

CSS:

/* Main Navigation */
#nav {
    position:relative;
    width:620px;
    margin:0 auto;
    margin-top:50px;
    padding:10px;
}

ul#navigation {
    margin:0px auto;
    position:relative;
    float:left;
}

ul#navigation li {
    display:inline;
    font-size:12px;
    font-weight:bold;
    margin:0;
    padding:0;
    float:left;
    position:relative;
}

ul#navigation li a {
    width: 234px;
    padding:10px 25px;
    color: black;
    text-decoration:none;
    display:inline-block;
    background: #ebebeb;
}

ul#navigation li a:hover {
    background:#ebebeb;
    color:black;
}

ul#navigation li a.first {
    border-left: 0 none;
}

ul#navigation li a.last {
    border-right: 0 none;
}

ul#navigation li:hover > a {
    background:#ebebeb;
}

/* Drop-Down Navigation */
ul#navigation li:hover > ul
{
/*these 2 styles are very important,
being the ones which make the drop-down to appear on hover */
    display: block; /* here change visiblity > display */
    opacity:1;
}

ul#navigation ul, ul#navigation ul li ul {
    list-style: none;
    margin: 0;
    padding: 0;
/*the next 2 styles are very important,
being the ones which make the drop-down to stay hidden */
    /*visibility:hidden;*/
    opacity:0;
    /*position: absolute; */
    display: none; /* change visibility > display */
    z-index: 99999;
    width:234px;
    background:#ebebeb;
}

ul#navigation ul {
    top: 43px;
    left: 1px;
}

ul#navigation ul li ul {
    top: 0;
    left: 181px; /* strong related to width:180px; from above */
}

ul#navigation ul li {
    clear:both;
    width:55px;

}

ul#navigation ul li a {
    background:none;
    padding:7px 15px;
    color:black;
    text-decoration:none;
    display:inline-block;
    border:0 none;
    float:left;
    clear:both;
    width:150px;
}


Solution

  • Add these lines at the bottom of your css file or tag.

    .hidden{
        display: none;
    }
    .shown{
        display: block;
        opacity: 1;
    }
    

    The html file:

    <li>
      <a id="prod1" href="#">Product 1</a> 
      <ul id="prod1list" class="hidden"> 
        <li><a href="#">rx.com </a></li> 
        <li><a href="#">Resources</a></li> 
        <li><a href="#">Copay Cards</a></li> 
      </ul> 
    </li>
    <script>
        document.getElementById('prod1').addEventListener('click', function(e){
            e.preventDefault();
            document.getElementById('prod1list').classList.toggle('shown');
        });
    </script>
    

    I tried it and it seems to work fine.