Simple drop down menu button
I'm trying to combine the drop down button found here
Dropdown button with the menu icon found here Menu icon. The animation would be nice to keep but is not the priority. In my attempt I simply replaced the "Dropdown" text with the divs from the menu icon example, i.e. from this:
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
to this:
<button onclick="myFunction()" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
and of course added the css classes. Now, the problem is that the bars inside the button are no longer clickable (At least not in Chrome. Firefox seems to work fine). I've tried to add onclick="myFunction()" to the divs aswell but to no avail.
Any tips?
javascript html css
add a comment |
I'm trying to combine the drop down button found here
Dropdown button with the menu icon found here Menu icon. The animation would be nice to keep but is not the priority. In my attempt I simply replaced the "Dropdown" text with the divs from the menu icon example, i.e. from this:
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
to this:
<button onclick="myFunction()" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
and of course added the css classes. Now, the problem is that the bars inside the button are no longer clickable (At least not in Chrome. Firefox seems to work fine). I've tried to add onclick="myFunction()" to the divs aswell but to no avail.
Any tips?
javascript html css
Could you please post the full code you tried so we can test it? Since it works in one browser but not the other, it's hard to find the exact issue with only the two seperate examples, neither demonstrating the problem.
– Shilly
Dec 28 '18 at 11:04
BTW, using pure css solution (based on pseudoelements and css gradients) for generating the bars would be a nice improvement.
– plvice
Dec 28 '18 at 11:06
add a comment |
I'm trying to combine the drop down button found here
Dropdown button with the menu icon found here Menu icon. The animation would be nice to keep but is not the priority. In my attempt I simply replaced the "Dropdown" text with the divs from the menu icon example, i.e. from this:
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
to this:
<button onclick="myFunction()" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
and of course added the css classes. Now, the problem is that the bars inside the button are no longer clickable (At least not in Chrome. Firefox seems to work fine). I've tried to add onclick="myFunction()" to the divs aswell but to no avail.
Any tips?
javascript html css
I'm trying to combine the drop down button found here
Dropdown button with the menu icon found here Menu icon. The animation would be nice to keep but is not the priority. In my attempt I simply replaced the "Dropdown" text with the divs from the menu icon example, i.e. from this:
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
to this:
<button onclick="myFunction()" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
and of course added the css classes. Now, the problem is that the bars inside the button are no longer clickable (At least not in Chrome. Firefox seems to work fine). I've tried to add onclick="myFunction()" to the divs aswell but to no avail.
Any tips?
javascript html css
javascript html css
asked Dec 28 '18 at 10:55
pLazepLaze
115
115
Could you please post the full code you tried so we can test it? Since it works in one browser but not the other, it's hard to find the exact issue with only the two seperate examples, neither demonstrating the problem.
– Shilly
Dec 28 '18 at 11:04
BTW, using pure css solution (based on pseudoelements and css gradients) for generating the bars would be a nice improvement.
– plvice
Dec 28 '18 at 11:06
add a comment |
Could you please post the full code you tried so we can test it? Since it works in one browser but not the other, it's hard to find the exact issue with only the two seperate examples, neither demonstrating the problem.
– Shilly
Dec 28 '18 at 11:04
BTW, using pure css solution (based on pseudoelements and css gradients) for generating the bars would be a nice improvement.
– plvice
Dec 28 '18 at 11:06
Could you please post the full code you tried so we can test it? Since it works in one browser but not the other, it's hard to find the exact issue with only the two seperate examples, neither demonstrating the problem.
– Shilly
Dec 28 '18 at 11:04
Could you please post the full code you tried so we can test it? Since it works in one browser but not the other, it's hard to find the exact issue with only the two seperate examples, neither demonstrating the problem.
– Shilly
Dec 28 '18 at 11:04
BTW, using pure css solution (based on pseudoelements and css gradients) for generating the bars would be a nice improvement.
– plvice
Dec 28 '18 at 11:06
BTW, using pure css solution (based on pseudoelements and css gradients) for generating the bars would be a nice improvement.
– plvice
Dec 28 '18 at 11:06
add a comment |
7 Answers
7
active
oldest
votes
Here is the working fiddle for the same:
https://jsfiddle.net/w5qftsgm/
I used a single function for both dropdown and bars which is written below:
function myFunction(x) {
x.classList.toggle("change");
document.getElementById("myDropdown").classList.toggle("show");
}
Please feel free to ask if you have any queries
Thanks! Is the JS function the only thing you changed to get it to work? Is there any way to keep the function that closes the menu when clicking outside the menu? I played around with it but it wouldn't sync with the cross icon.
– pLaze
Dec 28 '18 at 13:54
Here is the updated fiddle fulfilling your requirement: jsfiddle.net/w5qftsgm/2
– vishul garg
Dec 28 '18 at 14:08
For some reason the bars get unclickable as soon as I add that extra snippet to the JS script. It works in firefox but not in chrome...
– pLaze
Dec 28 '18 at 14:47
add a comment |
Content inside the button tag is for visual purposes only.
All interactivity inside is strictly limited to the button itself.
Therefore you should have the bars on the same level as the button.
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
1
Added potential with solution.
– ThatBrianDude
Dec 28 '18 at 11:05
add a comment |
if you need dropdown, i recommend change your code, for e.x.
function myFunc(e){
console.log(e.target)
}
document.getElementById("button").addEventListener("click",myFunc)<div id="button" class="dropbtn">
<button class="bar1">1</button>
<button class="bar2">2</button>
<button class="bar3">3</button>
</div>add a comment |
Don't use a button with div's in it. Try this solution from W3schools:
<style>
/* Style The 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 {
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 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;
}
</style>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
Here you have a live demo: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button
I don't want a text in the button, I want bars. Besides, that example is hover drop down and I want a clickable.
– pLaze
Dec 28 '18 at 14:41
add a comment |
use an anchor tag and style it you can use an anchor tag without a href and
make sure you are using a high z-index
ie <a style='z-index:999;'>bars go here</a>
Doesn't seem to make a difference..
– pLaze
Dec 28 '18 at 14:50
add a comment |
I created fiddle for you. First you have to style your bars. Then you need to create function that will add or remove show class in order to display your content. Something like that:
const btn = document.querySelector('.dropbtn');
const content = document.querySelector('.content');
btn.addEventListener('click', function() {
if(!content.classList.contains('show')) {
content.classList.add('show')
}
else {
content.classList.remove('show')
}
})
Here you have styles:
.bar {
width:20px;
height:3px;
background:black;
margin: 2px;
}
.content {
width:100px;
height:200px;
background:gray;
display:none;
}
.content.show {
display: block;
}
https://jsfiddle.net/ajp02uby/
The bars stop being clickable as soon as I add the snippet that removes the menu when a click is outside of it.
– pLaze
Dec 28 '18 at 15:06
add a comment |
this is your HTML
<div class="dropdown">
<button onclick="myFunction(this)" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
And your script
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(x) {
x.classList.toggle("change");
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');
}
}
}
}
And CSS goes here
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
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 a:hover {background-color: #ddd;}
.show {display: block;}
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
Please consider changing the div inside the button to some inline elements like span. In HTML try to avoid using of block elements inside inline elements.
This seems to be buggy, the dropdown menu is quickly getting out of sync with the cross when clicking. I also tried span but they behaved the same way.
– pLaze
Dec 28 '18 at 15:19
My answer has been updated.. You may please check.
– hakkim
Dec 31 '18 at 5:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53957395%2fsimple-drop-down-menu-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is the working fiddle for the same:
https://jsfiddle.net/w5qftsgm/
I used a single function for both dropdown and bars which is written below:
function myFunction(x) {
x.classList.toggle("change");
document.getElementById("myDropdown").classList.toggle("show");
}
Please feel free to ask if you have any queries
Thanks! Is the JS function the only thing you changed to get it to work? Is there any way to keep the function that closes the menu when clicking outside the menu? I played around with it but it wouldn't sync with the cross icon.
– pLaze
Dec 28 '18 at 13:54
Here is the updated fiddle fulfilling your requirement: jsfiddle.net/w5qftsgm/2
– vishul garg
Dec 28 '18 at 14:08
For some reason the bars get unclickable as soon as I add that extra snippet to the JS script. It works in firefox but not in chrome...
– pLaze
Dec 28 '18 at 14:47
add a comment |
Here is the working fiddle for the same:
https://jsfiddle.net/w5qftsgm/
I used a single function for both dropdown and bars which is written below:
function myFunction(x) {
x.classList.toggle("change");
document.getElementById("myDropdown").classList.toggle("show");
}
Please feel free to ask if you have any queries
Thanks! Is the JS function the only thing you changed to get it to work? Is there any way to keep the function that closes the menu when clicking outside the menu? I played around with it but it wouldn't sync with the cross icon.
– pLaze
Dec 28 '18 at 13:54
Here is the updated fiddle fulfilling your requirement: jsfiddle.net/w5qftsgm/2
– vishul garg
Dec 28 '18 at 14:08
For some reason the bars get unclickable as soon as I add that extra snippet to the JS script. It works in firefox but not in chrome...
– pLaze
Dec 28 '18 at 14:47
add a comment |
Here is the working fiddle for the same:
https://jsfiddle.net/w5qftsgm/
I used a single function for both dropdown and bars which is written below:
function myFunction(x) {
x.classList.toggle("change");
document.getElementById("myDropdown").classList.toggle("show");
}
Please feel free to ask if you have any queries
Here is the working fiddle for the same:
https://jsfiddle.net/w5qftsgm/
I used a single function for both dropdown and bars which is written below:
function myFunction(x) {
x.classList.toggle("change");
document.getElementById("myDropdown").classList.toggle("show");
}
Please feel free to ask if you have any queries
answered Dec 28 '18 at 11:06
vishul gargvishul garg
914
914
Thanks! Is the JS function the only thing you changed to get it to work? Is there any way to keep the function that closes the menu when clicking outside the menu? I played around with it but it wouldn't sync with the cross icon.
– pLaze
Dec 28 '18 at 13:54
Here is the updated fiddle fulfilling your requirement: jsfiddle.net/w5qftsgm/2
– vishul garg
Dec 28 '18 at 14:08
For some reason the bars get unclickable as soon as I add that extra snippet to the JS script. It works in firefox but not in chrome...
– pLaze
Dec 28 '18 at 14:47
add a comment |
Thanks! Is the JS function the only thing you changed to get it to work? Is there any way to keep the function that closes the menu when clicking outside the menu? I played around with it but it wouldn't sync with the cross icon.
– pLaze
Dec 28 '18 at 13:54
Here is the updated fiddle fulfilling your requirement: jsfiddle.net/w5qftsgm/2
– vishul garg
Dec 28 '18 at 14:08
For some reason the bars get unclickable as soon as I add that extra snippet to the JS script. It works in firefox but not in chrome...
– pLaze
Dec 28 '18 at 14:47
Thanks! Is the JS function the only thing you changed to get it to work? Is there any way to keep the function that closes the menu when clicking outside the menu? I played around with it but it wouldn't sync with the cross icon.
– pLaze
Dec 28 '18 at 13:54
Thanks! Is the JS function the only thing you changed to get it to work? Is there any way to keep the function that closes the menu when clicking outside the menu? I played around with it but it wouldn't sync with the cross icon.
– pLaze
Dec 28 '18 at 13:54
Here is the updated fiddle fulfilling your requirement: jsfiddle.net/w5qftsgm/2
– vishul garg
Dec 28 '18 at 14:08
Here is the updated fiddle fulfilling your requirement: jsfiddle.net/w5qftsgm/2
– vishul garg
Dec 28 '18 at 14:08
For some reason the bars get unclickable as soon as I add that extra snippet to the JS script. It works in firefox but not in chrome...
– pLaze
Dec 28 '18 at 14:47
For some reason the bars get unclickable as soon as I add that extra snippet to the JS script. It works in firefox but not in chrome...
– pLaze
Dec 28 '18 at 14:47
add a comment |
Content inside the button tag is for visual purposes only.
All interactivity inside is strictly limited to the button itself.
Therefore you should have the bars on the same level as the button.
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
1
Added potential with solution.
– ThatBrianDude
Dec 28 '18 at 11:05
add a comment |
Content inside the button tag is for visual purposes only.
All interactivity inside is strictly limited to the button itself.
Therefore you should have the bars on the same level as the button.
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
1
Added potential with solution.
– ThatBrianDude
Dec 28 '18 at 11:05
add a comment |
Content inside the button tag is for visual purposes only.
All interactivity inside is strictly limited to the button itself.
Therefore you should have the bars on the same level as the button.
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
Content inside the button tag is for visual purposes only.
All interactivity inside is strictly limited to the button itself.
Therefore you should have the bars on the same level as the button.
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
edited Dec 28 '18 at 11:03
answered Dec 28 '18 at 11:00
ThatBrianDudeThatBrianDude
1,059627
1,059627
1
Added potential with solution.
– ThatBrianDude
Dec 28 '18 at 11:05
add a comment |
1
Added potential with solution.
– ThatBrianDude
Dec 28 '18 at 11:05
1
1
Added potential with solution.
– ThatBrianDude
Dec 28 '18 at 11:05
Added potential with solution.
– ThatBrianDude
Dec 28 '18 at 11:05
add a comment |
if you need dropdown, i recommend change your code, for e.x.
function myFunc(e){
console.log(e.target)
}
document.getElementById("button").addEventListener("click",myFunc)<div id="button" class="dropbtn">
<button class="bar1">1</button>
<button class="bar2">2</button>
<button class="bar3">3</button>
</div>add a comment |
if you need dropdown, i recommend change your code, for e.x.
function myFunc(e){
console.log(e.target)
}
document.getElementById("button").addEventListener("click",myFunc)<div id="button" class="dropbtn">
<button class="bar1">1</button>
<button class="bar2">2</button>
<button class="bar3">3</button>
</div>add a comment |
if you need dropdown, i recommend change your code, for e.x.
function myFunc(e){
console.log(e.target)
}
document.getElementById("button").addEventListener("click",myFunc)<div id="button" class="dropbtn">
<button class="bar1">1</button>
<button class="bar2">2</button>
<button class="bar3">3</button>
</div>if you need dropdown, i recommend change your code, for e.x.
function myFunc(e){
console.log(e.target)
}
document.getElementById("button").addEventListener("click",myFunc)<div id="button" class="dropbtn">
<button class="bar1">1</button>
<button class="bar2">2</button>
<button class="bar3">3</button>
</div>function myFunc(e){
console.log(e.target)
}
document.getElementById("button").addEventListener("click",myFunc)<div id="button" class="dropbtn">
<button class="bar1">1</button>
<button class="bar2">2</button>
<button class="bar3">3</button>
</div>function myFunc(e){
console.log(e.target)
}
document.getElementById("button").addEventListener("click",myFunc)<div id="button" class="dropbtn">
<button class="bar1">1</button>
<button class="bar2">2</button>
<button class="bar3">3</button>
</div>answered Dec 28 '18 at 11:04
Vadim HulevichVadim Hulevich
4867
4867
add a comment |
add a comment |
Don't use a button with div's in it. Try this solution from W3schools:
<style>
/* Style The 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 {
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 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;
}
</style>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
Here you have a live demo: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button
I don't want a text in the button, I want bars. Besides, that example is hover drop down and I want a clickable.
– pLaze
Dec 28 '18 at 14:41
add a comment |
Don't use a button with div's in it. Try this solution from W3schools:
<style>
/* Style The 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 {
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 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;
}
</style>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
Here you have a live demo: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button
I don't want a text in the button, I want bars. Besides, that example is hover drop down and I want a clickable.
– pLaze
Dec 28 '18 at 14:41
add a comment |
Don't use a button with div's in it. Try this solution from W3schools:
<style>
/* Style The 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 {
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 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;
}
</style>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
Here you have a live demo: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button
Don't use a button with div's in it. Try this solution from W3schools:
<style>
/* Style The 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 {
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 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;
}
</style>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
Here you have a live demo: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button
answered Dec 28 '18 at 11:04
Guim GonzalezGuim Gonzalez
85112
85112
I don't want a text in the button, I want bars. Besides, that example is hover drop down and I want a clickable.
– pLaze
Dec 28 '18 at 14:41
add a comment |
I don't want a text in the button, I want bars. Besides, that example is hover drop down and I want a clickable.
– pLaze
Dec 28 '18 at 14:41
I don't want a text in the button, I want bars. Besides, that example is hover drop down and I want a clickable.
– pLaze
Dec 28 '18 at 14:41
I don't want a text in the button, I want bars. Besides, that example is hover drop down and I want a clickable.
– pLaze
Dec 28 '18 at 14:41
add a comment |
use an anchor tag and style it you can use an anchor tag without a href and
make sure you are using a high z-index
ie <a style='z-index:999;'>bars go here</a>
Doesn't seem to make a difference..
– pLaze
Dec 28 '18 at 14:50
add a comment |
use an anchor tag and style it you can use an anchor tag without a href and
make sure you are using a high z-index
ie <a style='z-index:999;'>bars go here</a>
Doesn't seem to make a difference..
– pLaze
Dec 28 '18 at 14:50
add a comment |
use an anchor tag and style it you can use an anchor tag without a href and
make sure you are using a high z-index
ie <a style='z-index:999;'>bars go here</a>
use an anchor tag and style it you can use an anchor tag without a href and
make sure you are using a high z-index
ie <a style='z-index:999;'>bars go here</a>
answered Dec 28 '18 at 11:04
Harley UrquhartHarley Urquhart
4114
4114
Doesn't seem to make a difference..
– pLaze
Dec 28 '18 at 14:50
add a comment |
Doesn't seem to make a difference..
– pLaze
Dec 28 '18 at 14:50
Doesn't seem to make a difference..
– pLaze
Dec 28 '18 at 14:50
Doesn't seem to make a difference..
– pLaze
Dec 28 '18 at 14:50
add a comment |
I created fiddle for you. First you have to style your bars. Then you need to create function that will add or remove show class in order to display your content. Something like that:
const btn = document.querySelector('.dropbtn');
const content = document.querySelector('.content');
btn.addEventListener('click', function() {
if(!content.classList.contains('show')) {
content.classList.add('show')
}
else {
content.classList.remove('show')
}
})
Here you have styles:
.bar {
width:20px;
height:3px;
background:black;
margin: 2px;
}
.content {
width:100px;
height:200px;
background:gray;
display:none;
}
.content.show {
display: block;
}
https://jsfiddle.net/ajp02uby/
The bars stop being clickable as soon as I add the snippet that removes the menu when a click is outside of it.
– pLaze
Dec 28 '18 at 15:06
add a comment |
I created fiddle for you. First you have to style your bars. Then you need to create function that will add or remove show class in order to display your content. Something like that:
const btn = document.querySelector('.dropbtn');
const content = document.querySelector('.content');
btn.addEventListener('click', function() {
if(!content.classList.contains('show')) {
content.classList.add('show')
}
else {
content.classList.remove('show')
}
})
Here you have styles:
.bar {
width:20px;
height:3px;
background:black;
margin: 2px;
}
.content {
width:100px;
height:200px;
background:gray;
display:none;
}
.content.show {
display: block;
}
https://jsfiddle.net/ajp02uby/
The bars stop being clickable as soon as I add the snippet that removes the menu when a click is outside of it.
– pLaze
Dec 28 '18 at 15:06
add a comment |
I created fiddle for you. First you have to style your bars. Then you need to create function that will add or remove show class in order to display your content. Something like that:
const btn = document.querySelector('.dropbtn');
const content = document.querySelector('.content');
btn.addEventListener('click', function() {
if(!content.classList.contains('show')) {
content.classList.add('show')
}
else {
content.classList.remove('show')
}
})
Here you have styles:
.bar {
width:20px;
height:3px;
background:black;
margin: 2px;
}
.content {
width:100px;
height:200px;
background:gray;
display:none;
}
.content.show {
display: block;
}
https://jsfiddle.net/ajp02uby/
I created fiddle for you. First you have to style your bars. Then you need to create function that will add or remove show class in order to display your content. Something like that:
const btn = document.querySelector('.dropbtn');
const content = document.querySelector('.content');
btn.addEventListener('click', function() {
if(!content.classList.contains('show')) {
content.classList.add('show')
}
else {
content.classList.remove('show')
}
})
Here you have styles:
.bar {
width:20px;
height:3px;
background:black;
margin: 2px;
}
.content {
width:100px;
height:200px;
background:gray;
display:none;
}
.content.show {
display: block;
}
https://jsfiddle.net/ajp02uby/
answered Dec 28 '18 at 11:08
hetioushetious
1896
1896
The bars stop being clickable as soon as I add the snippet that removes the menu when a click is outside of it.
– pLaze
Dec 28 '18 at 15:06
add a comment |
The bars stop being clickable as soon as I add the snippet that removes the menu when a click is outside of it.
– pLaze
Dec 28 '18 at 15:06
The bars stop being clickable as soon as I add the snippet that removes the menu when a click is outside of it.
– pLaze
Dec 28 '18 at 15:06
The bars stop being clickable as soon as I add the snippet that removes the menu when a click is outside of it.
– pLaze
Dec 28 '18 at 15:06
add a comment |
this is your HTML
<div class="dropdown">
<button onclick="myFunction(this)" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
And your script
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(x) {
x.classList.toggle("change");
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');
}
}
}
}
And CSS goes here
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
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 a:hover {background-color: #ddd;}
.show {display: block;}
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
Please consider changing the div inside the button to some inline elements like span. In HTML try to avoid using of block elements inside inline elements.
This seems to be buggy, the dropdown menu is quickly getting out of sync with the cross when clicking. I also tried span but they behaved the same way.
– pLaze
Dec 28 '18 at 15:19
My answer has been updated.. You may please check.
– hakkim
Dec 31 '18 at 5:58
add a comment |
this is your HTML
<div class="dropdown">
<button onclick="myFunction(this)" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
And your script
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(x) {
x.classList.toggle("change");
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');
}
}
}
}
And CSS goes here
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
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 a:hover {background-color: #ddd;}
.show {display: block;}
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
Please consider changing the div inside the button to some inline elements like span. In HTML try to avoid using of block elements inside inline elements.
This seems to be buggy, the dropdown menu is quickly getting out of sync with the cross when clicking. I also tried span but they behaved the same way.
– pLaze
Dec 28 '18 at 15:19
My answer has been updated.. You may please check.
– hakkim
Dec 31 '18 at 5:58
add a comment |
this is your HTML
<div class="dropdown">
<button onclick="myFunction(this)" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
And your script
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(x) {
x.classList.toggle("change");
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');
}
}
}
}
And CSS goes here
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
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 a:hover {background-color: #ddd;}
.show {display: block;}
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
Please consider changing the div inside the button to some inline elements like span. In HTML try to avoid using of block elements inside inline elements.
this is your HTML
<div class="dropdown">
<button onclick="myFunction(this)" class="dropbtn">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
And your script
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction(x) {
x.classList.toggle("change");
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');
}
}
}
}
And CSS goes here
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
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 a:hover {background-color: #ddd;}
.show {display: block;}
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
Please consider changing the div inside the button to some inline elements like span. In HTML try to avoid using of block elements inside inline elements.
edited Dec 31 '18 at 5:58
answered Dec 28 '18 at 11:10
hakkimhakkim
170423
170423
This seems to be buggy, the dropdown menu is quickly getting out of sync with the cross when clicking. I also tried span but they behaved the same way.
– pLaze
Dec 28 '18 at 15:19
My answer has been updated.. You may please check.
– hakkim
Dec 31 '18 at 5:58
add a comment |
This seems to be buggy, the dropdown menu is quickly getting out of sync with the cross when clicking. I also tried span but they behaved the same way.
– pLaze
Dec 28 '18 at 15:19
My answer has been updated.. You may please check.
– hakkim
Dec 31 '18 at 5:58
This seems to be buggy, the dropdown menu is quickly getting out of sync with the cross when clicking. I also tried span but they behaved the same way.
– pLaze
Dec 28 '18 at 15:19
This seems to be buggy, the dropdown menu is quickly getting out of sync with the cross when clicking. I also tried span but they behaved the same way.
– pLaze
Dec 28 '18 at 15:19
My answer has been updated.. You may please check.
– hakkim
Dec 31 '18 at 5:58
My answer has been updated.. You may please check.
– hakkim
Dec 31 '18 at 5:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53957395%2fsimple-drop-down-menu-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you please post the full code you tried so we can test it? Since it works in one browser but not the other, it's hard to find the exact issue with only the two seperate examples, neither demonstrating the problem.
– Shilly
Dec 28 '18 at 11:04
BTW, using pure css solution (based on pseudoelements and css gradients) for generating the bars would be a nice improvement.
– plvice
Dec 28 '18 at 11:06