0

I have downloaded a boostrap template online and is making modifications to the template. However, I'd like my menu bar to hoverable dropdowns for each item in the menu bar. I have this for the css of the dropdown:

.dropdown {
position: relative;
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

.dropdown-content a:hover {background-color: #ddd;}

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: #3e8e41;}

This is the html part:

<ul class="navbar-nav text-uppercase ml-auto dropdown">
    <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#services">Services</a></li>
    <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#experts">Experts</a>
    <ul class="dropdown-content">
        <li><a href="#">Test dropdown</a></li>
    </ul>
    </li>
</ul>

I have been just following instructions online but I can't seem to get it right.

This is the output that I kept on getting:

Output

It shows the submenu even if the cursor is not hovering over the menu.

3
  • Do you want it to only display when you hover over the Experts link? Commented Jul 31, 2018 at 14:34
  • Yes I would like to make it work with one item first Commented Jul 31, 2018 at 14:39
  • I was going to say that you need to add the dropdown class to the relevant li and specify that you need .dropdown:hover > .dropdown-content instead of .dropdown:hover .dropdown-content, but I see that @HasuKimchi has beaten me to it :-) Commented Jul 31, 2018 at 14:42

1 Answer 1

1

Well you forgot to add the dropdownclass to your nav-item:

<ul class="navbar-nav text-uppercase ml-auto">
    <li class="nav-item"><a class="nav-link js-scroll-trigger" href="#services">Services</a></li>
    <li class="nav-item dropdown"><a class="nav-link js-scroll-trigger" href="#experts">Experts</a>
    <ul class="dropdown-content">
        <li><a href="#">Test dropdown</a></li>
    </ul>
    </li>
</ul>

And then you need to specify the direct child with > in if you hover selector.

.dropdown:hover > .dropdown-content {display: block;}
Sign up to request clarification or add additional context in comments.

1 Comment

At first it didn't work, but upon checking my css files I've noticed that there are two similar css there and I was only applying changes to one. I tried applying same changes to the other one and it worked! Thank you!

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.