how to add a clickable dropdown menu in blogger? - html

So, I found this link on clickable dropdown menus but when I implement that, it doesn't work for some reason. I tried adding the html into the gadget but I don't know where to put the JavaScript in.

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
This is the same code used in the example you sent, you can get the javascript and insert at the end of the html between two script tags like this:
<script type="text/javascript">
<!-- javascript GOES HERE -->
<script>

You can add javascript code inside html putting it inside <script></script> tag

Create Drop down menu in blogger using add custom Gadget. In this you can add your own codes. This requires a little bit of CSS and HTML knowledge. Try my code to create drop down menu in blogger. referenced from this blogger post
.dropdown-section1{
border-radius:3px;
width:80%;margin:0 auto;
background:#607d8b;
height:38px;
-webkit-box-shadow: 0 3px 4px 0 rgba(50,50,50,.6);
-moz-box-shadow: 0 3px 4px 0 rgba(50,50,50,.6);
box-shadow: 0 3px 4px 0 rgba(50,50,50,.6);
}
.dropdown-section1 .menu li a {
color:#fff;
font-weight:bold;
text-decoration:none;
}
.dropdown-section1 .menu li {
list-style: none;
float: left;
padding: 10px;
background:#607d8b;
}
.dropdown-section1 .menu li:last-child{
border-right:none !important;
}
.dropdown-section1 .menu .sub-menu li{
float: none;
margin-bottom:0px;
padding: 10px;
}
.dropdown-section1 .sub-menu {
display: none;
position: absolute;
z-index: 100;
width:100px;
margin-left: -50px !important;
margin-top: 10px !important;
}
.dropdown-section1 .menu li:hover > .sub-menu {
display: block;
}
.dropdown-section1 .menu li:hover {
background:#94c018;
}
.dropdown-section1 .menu > li {
border-right:1px solid #ccc;
}
.dropdown-section1 .place-right{
left: 150px;
top: 30px;
}
<div class="dropdown-section1">
<ul class="menu">
<li>Home</li>
<li>Coose Niche
</li><li>Create Blog
</li><li>Moniter Blog
</li><li>DropDown
<ul class="sub-menu">
<li>String</li>
<li>Array
<ul class="sub-menu place-right">
<li>String</li>
<li>Array</li>
<li>Match</li>
</ul>
</li>
<li>Match</li>
</ul>
</li>
<li>About Me</li>
<li>Reach Me</li>
</ul>
</div>

Related

Open drop-down menu onClick in sidebar

I am working on a project and I have created one navbar. In this dropdown menu also present. In this dropdown, the hover effect is there.
Now I am trying to click event and open and close dropdown's submenu. But not working. I am using only HTML and CSS.
Now on hover dropdown is open. But I am trying to do:
open and close drop-down when I click.
My code is:
/* define a fixed width for the entire menu */
.navigation {
width: 300px;
}
/* reset our lists to remove bullet points and padding */
.mainmenu, .submenu {
list-style: none;
padding: 0;
margin: 0;
}
/* make ALL links (main and submenu) have padding and background color */
.mainmenu a {
display: block;
background-color: #CCC;
text-decoration: none;
padding: 10px;
color: #000;
}
/* add hover behaviour */
.mainmenu a:hover {
background-color: #C5C5C5;
}
/* when hovering over a .mainmenu item,
display the submenu inside it.
we're changing the submenu's max-height from 0 to 200px;
*/
.mainmenu li:hover .submenu {
display: block;
max-height: 200px;
}
/*
we now overwrite the background-color for .submenu links only.
CSS reads down the page, so code at the bottom will overwrite the code at the top.
*/
.submenu a {
background-color: #ddd;
}
/* hover behaviour for links inside .submenu */
.submenu a:hover {
background-color: #993;
}
/* this is the initial state of all submenus.
we set it to max-height: 0, and hide the overflowed content.
*/
.submenu {
overflow: hidden;
max-height: 0;
-webkit-transition: all 0.5s ease-out;
}
<nav class="navigation">
<ul class="mainmenu">
<li>Home</li>
<li>About</li>
<li>Products
<ul class="submenu">
<li>Tops</li>
<li>Bottoms</li>
<li>Footwear</li>
</ul>
</li>
<li>Contact us</li>
</ul>
</nav>
You can use a checkbox (<input type="checkbox">) with the adjacent sibling combinator (+) to toggle the sub menu. Convert the link (a) to a label. Set the label's for attribute to the input's id, and hide the input using display: none.
/* define a fixed width for the entire menu */
.navigation {
width: 300px;
}
/* reset our lists to remove bullet points and padding */
.mainmenu,
.submenu {
list-style: none;
padding: 0;
margin: 0;
}
/* make ALL links (main and submenu) have padding and background color */
.mainmenu a,
.mainmenu label {
display: block;
background-color: #CCC;
text-decoration: none;
padding: 10px;
color: #000;
}
.mainmenu input {
display: none;
}
/* add hover behaviour */
.mainmenu a:hover {
background-color: #C5C5C5;
}
/* when hovering over a .mainmenu item,
display the submenu inside it.
we're changing the submenu's max-height from 0 to 200px;
*/
.mainmenu :checked+.submenu {
display: block;
max-height: 200px;
}
/*
we now overwrite the background-color for .submenu links only.
CSS reads down the page, so code at the bottom will overwrite the code at the top.
*/
.submenu a {
background-color: #ddd;
}
/* hover behaviour for links inside .submenu */
.submenu a:hover {
background-color: #993;
}
/* this is the initial state of all submenus.
we set it to max-height: 0, and hide the overflowed content.
*/
.submenu {
overflow: hidden;
max-height: 0;
-webkit-transition: all 0.5s ease-out;
}
<nav class="navigation">
<ul class="mainmenu">
<li>Home</li>
<li>About</li>
<li><label for="products">Products</label><input type="checkbox" id="products">
<ul class="submenu">
<li>Tops</li>
<li>Bottoms</li>
<li>Footwear</li>
</ul>
</li>
<li>Contact us</li>
</ul>
</nav>
This is your solution
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.navigation{
width: 300px;
}
.mainmenu,
.submenu {
list-style: none;
padding: 0;
margin: 0;
}
.mainmenu a {
display: block;
background-color: #CCC;
text-decoration: none;
padding: 10px;
color: #000;
}
.mainmenu a:hover {
background-color: #C5C5C5;
}
/**/
.dropbtn {
background-color: #ccc;
color: #000;
border: none;
cursor: pointer;
padding: 10px;
display: block;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #ccc;
}
.dropbtn:hover{
background-color: #C5C5C5;
}
.dropdown {
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #ddd;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: 300px;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #993;}
.show {display: block;}
<nav class="navigation">
<ul class="mainmenu">
<li><a>Home</a></li>
<li><a>About</a></li>
<div class="dropdown">
<li onclick="myFunction()" class="dropbtn">Products</li>
<div id="myDropdown" class="dropdown-content">
<a>Tops</a>
<a>Bottoms</a>
<a>Footwear</a>
</div>
</div>
<li>Contact us</li>
</ul>
</nav>
u can achieve with javascript and this is right solution

second column in drop down menu list in navigation bar

Hi I have made a web page with a navigation bar. There is mouse hover drop down menu on a navigation bar. I have take the code for this navigation bar from w3school web site. The code is
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
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;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div style="display:flex">
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
<div class="dropdown-content">
List 4
List 5
List 6
</div>
</div>
</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
Now the problem is that I want to add second column in front of first column in the drop down list.For this I add second div but all the stuff of the second div appears in place of first div and hides the content of first div. Please tell me that how can I place the second div in next to first div so that they appear parallel to each other.Please answer this question. I will be very thank full to you.
You can place it by wrapping the .dropdown-content div with a flexbox.
I have added .wrapper a class you can change it to whatever you like.
Here is the code:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.wrapper {
display: none;
position: absolute;
background-color: #f9f9f9;
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;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .wrapper{
display: flex;
justify-content: center;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="wrapper">
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
<div class="dropdown-content">
demo
demo
demo
</div>
</div>
</li>
</ul>
P.S. In the same way you can create multiple, side by side divs using flexbox. You can learn about flex here

Displaying a drop down on hover

Below is the code I have written to display a dropdown list. The dropdown items display on click, but I want the dropdown list to be displayed on hover and I want the page to redirect to the link provided.
<select id="marketplace">
<option>Select a dropdown</option>
<option>Amazon</option>
<option>Flipkart</option>
<option>Snapdeal</option>
<option>paytm</option>
</select>
As W3Shcools You can use instead of select tag div like this implementation
/* Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.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;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: #ddd
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
<div class="dropdown">
<button class="dropbtn">Select a dropdown</button>
<div class="dropdown-content">
Amazon
Flipkart
napdeal
paytm
</div>
</div>
Also I tried to define onmouseenter event for your select tag and open its options on this event in javascript. But according to this it is not possible. So you must change your tag!
Codes I try:
function f() {
console.log('here')
var select = document.getElementById('marketplace');
select.click();
}
<select id="marketplace" onmouseenter="f()">
<option>Select a dropdown</option>
<option>Amazon</option>
<option>Flipkart</option>
<option>Snapdeal</option>
<option>paytm</option>
</select>
Tip Use option tag as direct child of select tag and put a tags inside options
I made it using this website
http://www.devcurry.com/2009/04/using-javascript-to-dropdown-html.html
And then i add function to redirect to the link ^^
Check this :D
The code demo is over at http://www.dotnetcurry.com/Demos/javascript/HoverSelect.htm
function DropList() {
var n = document.getElementById("sel").options.length;
document.getElementById("sel").size = n;
}
function handleSelect(elm){
window.location = elm.value;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<select id="sel" onmouseover="DropList()" onmouseout="this.size=1;" onchange="javascript:handleSelect(this)">
<option value="https://www.amazon.in/">Amazon</option>
<option value="https://www.flipkart.com/">Flipkart</option>
<option value="https://www.snapdeal.com/">Snapdeal</option>
<option value="https://www.paytm.com/">paytm</option>
</select>
</div>
You can make this by using other html elements such as ul and li like this
.menu {
}
.submenu {
position: absolute;
left: 0;
top: 100%;
display: none; // initially submenu is hidden
}
.menu li {
position: relative;
}
.menu li:hover .submenu {
display: block; // shows submenu on hover
}
<ul class="menu">
<li>Select a dropdown
<ul class="submenu">
<li>Amazon</li>
<li>Flipkart</li>
<li>Snapdeal</li>
</ul>
</li>
</ul>
You can achieve this using javascript
function DropList() {
var n = document.getElementById("sel").options.length;
document.getElementById("sel").size = n;
}
function handleSelect(elm){
window.location = elm.value;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<select id="sel" onmouseover="DropList()" onmouseout="this.size=1;" onchange="javascript:handleSelect(this)">
<option>Select a dropdown</option>
<option>Amazon</option>
<option>Flipkart</option>
<option>Snapdeal</option>
</select>
</div>
For creating similar/repetitive elements in HTML, I prefer to use ul li, it makes controlling them more easier.
NO NEED OF JS OR JQUERY, TRY USING MORE CSS!
Code for dropdown
(YOU MIGHT FIND THE CSS WAY TOO LONG, ITS BECAUSE I CUSTOMISED THE ELEMENTS TO LOOK MORE NEAT)
HTML
<div class="navigationWrap">
<ul>
<li class="dropdown">Dropdown
<ul class="subNav">
<li>Dummy One</li>
<li>Dummy Two</li>
<li>Dummy Three</li>
<li>Dummy Four</li>
</ul>
</li>
</ul>
</div>
CSS
Using position absolute & relative to display element on hover, and WE ARE DONE!
ul { margin: 0px; padding: 0px; list-style: none; }
.navigationWrap ul li { width: 100%; float: left; color: #000; font-size: 30px; position: relative; }
.navigationWrap ul li a { text-decoration: none; color: #000; display: block; }
.navigationWrap ul li a:hover { color: #04A000; }
.navigationWrap ul li ul.subNav { position: absolute; width: 150px; padding:10px; background-color: #fff; border: #000 solid 1px; left: 0; top: 100%; display: none; z-index: 999; }
.navigationWrap ul li ul.subNav li { float: left; width: 100%; font-size: 20px; letter-spacing: 1px; padding: 0; }
.navigationWrap ul li ul.subNav a { float: left; width: 100%; transition: all 0.3s ease-in-out; display: block; padding: 0 0 0 15px; }
.navigationWrap ul li ul.subNav li a:hover { color: #04A000; padding-left: 20px; }
.navigationWrap ul li ul.subNav li.active a { color: #04A000; }
.navigationWrap ul li.dropdown:hover ul.subNav { display: block; }
JSFIDDLE -- https://jsfiddle.net/neel448/u0Lmb0jv/

Drop down menu doesn't work how I want it to

I am trying to make a menu which contains one menu-item with a dropdown, however I can't seem to get it quite right.
I started off with combining my existing menu bar to a piece of code for a dropdown, I have managed to get de drop down item in the menu bar, but I don't know how to get the menu bar back to the top again and it still has a blank on the left.
There is also an extra empty item in my drop down menu and I don't know how to get rid off it?
Can someone please help me I feel like I am overlooking something
.menu-bar {
position: fixed;
width: 100vw;
background-color: #134c95;
color: white;
display: flex;
flex-direction: row;
justify-content: center;
opacity: 0.8;
}
.menu-item {
padding: 10px;
cursor: default;
font-size: 15pt;
margin-left: 15px;
margin-right: 15px;
}
.menu-item:hover,
a:hover {
background-color: white;
cursor: pointer;
color: 234c95;
}
.menu-bar a {
color: white;
}
.menu-bar a:visited {
color: white;
}
.menu-bar a:link,
.menu-bar a:visited {
text-decoration: none;
}
/*
.menu-bar a:hover {
color: #134c95;
}
*/
/* Dropdown Button */
.dropbtn {
background-color: inherit;
color: white;
/* padding: 16px;*/
/* font-size: 16px;*/
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
/*
.dropdown {
position: fixed;
display: inline-block;
}
*/
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: white;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
/* z-index: 1;*/
}
/* Links inside the dropdown */
.dropdown-content a {
color: #234c95;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: 234c95;
color: white;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: white;
color: #234c95;
}
<div class="menu-bar">
<a href="instructiestest.html">
<div class="menu-item">Welkom</div>
</a>
<a href="instructie2.html">
<div class="menu-item">Aanmelden Portbase</div>
</a>
<a href="instructie3.html">
<div class="menu-item">De App</div>
</a>
<a href="testing.html">
<div class="dropdown">
<button class="dropbtn">
<div class="menu-item">Het team</div>
<div class="dropdown-content">
Google
Facebook
YouTube
</div>
</button>
</div>
</a>
</div>
Remove the a tag from your dropdown item, like below:
<div class="menu-bar">
<a href="instructiestest.html">
<div class="menu-item">Welkom</div>
</a>
<a href="instructie2.html">
<div class="menu-item">Aanmelden Portbase</div>
</a>
<a href="instructie3.html">
<div class="menu-item">De App</div>
</a>
<div class="dropdown">
<button class="dropbtn">
<div class="menu-item">Het team</div>
<div class="dropdown-content">
Google
Facebook
YouTube
</div>
</button>
</div>
</div>
Also, your .dropdown-content a:hover rule is missing a # in your background-color attribute:
.dropdown-content a:hover {
background-color: #234c95;
color: white;
}
Here is a working Fiddle
Something seems to have an extra margin or similar. Try to load the code, and check with the inspector. It'll show you the different spacing types in nice colours: https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector/How_to/Examine_and_edit_the_box_model
In all honesty, you might be better re structuring this menu completely. I don't mean to be harsh but the code isn't structured well enough and is a little confusing to work with.
Ive just tweaked your code a little to make it easier to work with. I'm not exactly sure what it is your trying to achieve but here is something for you to take a look at:
CSS
#menu-bar {
position: fixed;
width: 100vw;
background-color: #134c95;
padding: 15px;
}
#menu-bar ul {
list-style: none;
padding: 0;
margin: 0;
font-size: 22px;
font-family:sans-serif;
}
#menu-bar ul li {
margin-bottom: 10px;
width: 100%;
}
#menu-bar ul li a {
display: block;
padding: 10px;
color: #fff;
text-decoration: none;
}
#menu-bar ul li a:hover {
color: #134c95;
background-color: #fff;
}
#menu-bar ul li ul {
display: none;
}
#menu-bar ul li:hover ul {
display: block;
}
HTML
<nav id="menu-bar">
<ul>
<li>Welkom</li>
<li>Aanmelden Portbase</li>
<li>De App</li>
<li class="">Het team
<ul>
Google
Facebook
YouTube
</ul>
</li>
</ul>
</nav>
As you can see this is much easier to understand and follow. The ID of the nav element is all you need to style the child elements.
Hope that helps as a starting point!

Dropdown Menu Somehow Not Working

I have went through W3Schools to attempt understanding the coding structure of dropdown menus. When opening the page and hovering your cursor to the 'Merch' text it is supposed to display the dropdown menu. For some reason, however, it is not showing. Please amplify for me and show me where I went wrong.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
li a:hover {
background-color: gray;
}
#dropdown {
position: absolute;
display: none;
background-color: darkgray;
min-width: 140px;
}
#dropdown a {
color: white;
text-align: left;
padding: 10px;
display: block;
text-align: left;
}
#dropdown a:hover {
background-color: gray;
}
#dropbtn:hover #dropdown {
display: block;
}
</style>
</head>
<body>
<ul>
<li>Home</li>
<li>Merch
<div id="dropdown">
Shirts
Pants
</div>
</li>
<li>About Us</li>
</ul>
</body>
</html>
You need this change in CSS
#dropbtn:hover + #dropdown {
display: block;
}
Dropdown is not a child, it is next element in your current html setup, so, this selector will find it.
Or, better, place id on li element (parent):
<ul>
<li>Home</li>
<li id="dropbtn"><a href="#" >Merch</a>
<div id="dropdown">
Shirts
Pants
</div>
</li>
<li>About Us</li>
</ul>
#dropbtn:hover #dropdown {
display: block;
}
Demo: https://jsfiddle.net/3bfgzf37/
If you use first solution, dropdown dissapears fast, and behave strange...
Explanation: hover on a is not hover on dropdown (a is sibling), hover on li element, is, in the same time, hover on dropdown (parent-child, dropdown is 'inside' li - that produces consistent, desired, behavior).
Second one is better.
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
li a:hover {
background-color: gray;
}
#dropdown{
position: absolute;
display:none;
background-color: darkgray;
min-width: 140px;
}
#dropdown a {
color: white;
text-align: left;
padding: 10px;
display: block;
text-align: left;
}
#dropdown a:hover {
background-color: gray;
}
#dropbtn:hover #dropdown {
display: block;
}
<ul>
<li>Home</li>
<li id="dropbtn"><a href="#" >Merch</a>
<div id="dropdown">
Shirts
Pants
</div>
</li>
<li>About Us</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
Your style tag should be outside the head tag. Plus, the dropdown in this code works now. Just do some slight changes to the colors to your desire. :)
<html>
<head></head>
<style>
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
/* Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: gray;
}
</style>
<body>
<ul>
<li>Home</li>
<li>
<div class="dropdown">
<button class="dropbtn">Merch</button>
<div class="dropdown-content">
Shirts
Pants
</div>
</div>
</li>
<li>About Us</li>
</ul>
</body>
</html>