0

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>

1
  • no answer accepted yet? Commented Nov 11, 2016 at 8:41

5 Answers 5

1

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>

Sign up to request clarification or add additional context in comments.

2 Comments

this is basically what I tried, it has the same issue. you can't actually click any of the options before it disappears. try it out
@oneman Update the answer basically you set inline-block to dropdown class with position relative. And on .dropdown:hover you set display:block. So I just remove the display inline-block from dropdown call.. Rest are same :) good luck
1

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>

Comments

1

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>

Comments

1

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);
}

Comments

0

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

Comments

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.