Reputation: 811
As the title suggests, I'm making a CSS dropdown menu.
My first problem was that when I hovered, the dropdown menu covered the hamburger icon. So I added margin-top
which fixed that issue but caused another. When I would move my mouse off of the hamburger icon to select one of the drop down options, the dropdown menu would disappear. So my question is, how can I have a dropdown menu, which still displays the hamburger icon on display and lets me actually click the options without disappearing.
/* Global Styles */
* {
font-family: 'Open Sans', sans-serif;
}
a:link,
a:hover,
a:active,
a:visited {
text-decoration: none;
color: inherit;
}
/* Nav Bar Styling */
div.nav {
width: 100%;
background-color: #1c1c1c;
color: white;
position: absolute;
top: 0;
left: 0;
right: 0;
margin-top: 0;
height: 50px;
}
#title {
text-align: center;
vertical-align: middle;
margin: 0 auto;
font-size: 1.5em;
position: fixed;
top: 15px;
left: 50%;
right: 50%;
}
span.dropbutton {
text-align: left;
vertical-align: middle;
color: white;
position: fixed;
top: 15px;
left: 2%
}
#lines:hover {
transform: scale(1.1);
}
/* Dropdown Styling */
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #1c1c1c;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
padding: 12px 16px;
display: block;
}
.dropdown-content a:hover {
background-color:
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="nav">
<span id="title">Title</span>
<div class="dropdown">
<span class="dropbutton">☰</span>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
Upvotes: 0
Views: 195
Reputation: 86
Just edit the last selector
.dropdown:hover{display:block;}
.dropdown:blur{display:none;}
OR You may you javascript for this task.
<script>
document.getElementById("dropdown").onfocus = function () {
document.getElementById("dropdown").style.display = block;
}
document.getElementById("dropdown").onblur = function () {
document.getElementById("dropdown").style.display = none;
}
</script>
Add this script just before ending body tag
Upvotes: 0
Reputation: 2728
You are using absolute position on .dropdown-content so on that css I just add top:35px . If you have any question ask me in comment :)
/* Global Styles */
* {
font-family: 'Open Sans', sans-serif;
}
a:link,
a:hover,
a:active,
a:visited {
text-decoration: none;
color: inherit;
}
/* Nav Bar Styling */
div.nav {
width: 100%;
background-color: #1c1c1c;
color: white;
position: absolute;
top: 0;
left: 0;
right: 0;
margin-top: 0;
height: 50px;
}
#title {
text-align: center;
vertical-align: middle;
margin: 0 auto;
font-size: 1.5em;
position: fixed;
top: 15px;
left: 50%;
right: 50%;
}
span.dropbutton {
text-align: left;
vertical-align: middle;
color: white;
position: fixed;
top: 15px;
left: 2%
}
#lines:hover {
transform: scale(1.1);
}
/* Dropdown Styling */
.dropdown {
position: relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #1c1c1c;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
top:35px;
}
.dropdown-content a {
padding: 12px 16px;
display: block;
}
.dropdown-content a:hover {
background-color:
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="nav">
<span id="title">Title</span>
<div class="dropdown">
<span class="dropbutton">☰</span>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
Upvotes: 1
Reputation: 53
Lower the dropdown-content by 20px. This way there won't be a gap between the hamburger icon and the sub-menu. A gap disables the hover effect.
.dropdown-content {
display: none;
position: absolute;
background-color: #1c1c1c;
margin-top: 20px;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
Upvotes: 1
Reputation: 13558
Something like this,
Giving height
and width
to .dropdown
and top:100%;
to .dropdown-content
will solve problem
.dropdown {
position: relative;
display: inline-block;
height: 50px;
width: 40px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #1c1c1c;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
top: 100%;
}
/* Global Styles */
* {
font-family: 'Open Sans', sans-serif;
}
a:link,
a:hover,
a:active,
a:visited {
text-decoration: none;
color: inherit;
}
/* Nav Bar Styling */
div.nav {
width: 100%;
background-color: #1c1c1c;
color: white;
position: absolute;
top: 0;
left: 0;
right: 0;
margin-top: 0;
height: 50px;
}
#title {
text-align: center;
vertical-align: middle;
margin: 0 auto;
font-size: 1.5em;
position: fixed;
top: 15px;
left: 50%;
right: 50%;
}
span.dropbutton {
text-align: left;
vertical-align: middle;
color: white;
position: fixed;
top: 15px;
left: 2%
}
#lines:hover {
transform: scale(1.1);
}
/* Dropdown Styling */
.dropdown {
position: relative;
display: inline-block;
height: 50px;
width: 40px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #1c1c1c;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
top: 100%;
}
.dropdown-content a {
padding: 12px 16px;
display: block;
}
.dropdown-content a:hover {
background-color:
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="nav">
<span id="title">Title</span>
<div class="dropdown">
<span class="dropbutton">☰</span>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
Upvotes: 1
Reputation: 1513
Just add a padding to the menu container, remove the background color to make it transparent and add the background-color to the elements of the menu
/* Global Styles */
* {
font-family: 'Open Sans', sans-serif;
}
a:link,
a:hover,
a:active,
a:visited {
text-decoration: none;
color: inherit;
}
/* Nav Bar Styling */
div.nav {
width: 100%;
background-color: #1c1c1c;
color: white;
position: absolute;
top: 0;
left: 0;
right: 0;
margin-top: 0;
height: 50px;
}
#title {
text-align: center;
vertical-align: middle;
margin: 0 auto;
font-size: 1.5em;
position: fixed;
top: 15px;
left: 50%;
right: 50%;
}
span.dropbutton {
text-align: left;
vertical-align: middle;
color: white;
position: fixed;
top: 15px;
left: 2%
}
#lines:hover {
transform: scale(1.1);
}
/* Dropdown Styling */
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
padding-top: 36px;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
padding: 12px 16px;
display: block;
background-color: #1c1c1c;
}
.dropdown-content a:hover {
background-color:
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="nav">
<span id="title">Title</span>
<div class="dropdown">
<span class="dropbutton">☰</span>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
Upvotes: 1