So I've basically tried everything I could think off. I want to make a drop down menu from an image I'm using. The cursor does manage to become a pointer. I'm really lost to what I've done wrong.
This is the HTML I have right now:
<a>
<img class="dropbtn" id="profile" src="img/icons/Profiel.png" alt="Profiel" width="50px" heigt="50px">
<div id="myDropdown" class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</img>
</a>
And here's the CSS:
/* Dropdown Button */
.dropbtn {
cursor: pointer;
background-color: 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: #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 (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}
Use below code
$(".dropbtn").click(function() {
$("#myDropdown").toggleClass("show");
});
.dropbtn {
cursor: pointer;
background-color: 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: #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;
}
a:hover .dropdown-content {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;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a>
<img class="dropbtn" id="profile" src="img/icons/Profiel.png" alt="Profiel" width="50px" heigt="50px">
<div id="myDropdown" class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</a>
Are you open to using jQuery?
You can achieve what you want by simply adding:
$(".dropbtn").click(function() {
$("#myDropdown").toggleClass("open");
});
Codepen Demo
$(".dropbtn").click(function() {
$("#myDropdown").toggleClass("open");
});
/* Dropdown Button */
.dropbtn {
cursor: pointer;
background-color: 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: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content.open {
display: block;
}
/* 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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<a>
<img class="dropbtn" id="profile" src="http://placehold.it/350x350" alt="Profiel" width="50px" height="50px">
<div id="myDropdown" class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</img>
</a>
Have you made sure to change the dropdowns CSS to display: block; when the image is clicked?
If not, try this out (JavaScript):
var trigger = document.getElementById("profile"),
dropdown = document.getelementById("myDropdown");
trigger.onclick = function(){ // on image click
dropdown.classList.toggle("active"); // toggle dropdown display
});
CSS:
or jQuery (if you have it referenced within your website):
$("#profile").click(function(){ // on image click
$("#myDropdown").toggleClass("active"); // toggle dropdown display
});
Make sure (if you don't already know) to add the above to a script tag within your <head> or <body> tags, inside of your HTML document!
#myDropdown.active{ /* if #myDropdown has class "active" */
display: block; /* make the dropdown visible */
}
Hope this helps! :-)
Related
I have this drop down which looks like this
Now when I click on Add Equipment everything works fine, but when I click on the Deployed Equipments the dropdown of the Add Equipment drops down.
Basically this is what happens when I click Deployed Equipments
Am I doing something wrong?
Here's my css:
/* Dropdown Button */
.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;}
Here's my html:
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Add Equipment</button>
<div id="myDropdown" class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Deployed Equipments</button>
<div id="myDropdown" class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
Here's my JavaScript:
<script>
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');
}
}
}
}
</script>
Both the divs have same id myDropdown. You can remove ids for the div and use the below code to access the dropdown using nextElementSibling of the clicked button. Hope this helps.
function myFunction() {
//Remove class 'show' for dropdown except the current one
[].slice.call(document.getElementsByClassName("dropdown-content")).map(function(el){
if (this.event.target.nextElementSibling !== el)
el.classList.remove("show");
});
//this.event.target refers to the button clicked. Get nextelement and toggle class 'show'
this.event.target.nextElementSibling.classList.toggle("show");
}
Example:
function myFunction() {
[].slice.call(document.getElementsByClassName("dropdown-content")).map(function(el){
if (this.event.target.nextElementSibling !== el)
el.classList.remove("show");
});
this.event.target.nextElementSibling.classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function (event) {
if (!event.target.matches('.dropbtn')) {
[].slice.call(document.getElementsByClassName("dropdown-content")).map(function(el){
el.classList.remove("show");
});
}
}
/* Dropdown Button */
.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">Add Equipment</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Deployed Equipments</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
You should not use the same ID for 2 different elements on the same page. myDropdown appears to be on both.
Here you can find a solution made using jQuery:
$('.dropdown').click(function() {
$(this).find(".dropdown-content").toggleClass("show");
});
/* Dropdown Button */
.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;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown">
<button class="dropbtn">Add Equipment</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Deployed Equipments</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
I want to implement a navigation menu that displays another detail menu on hover.
just like the one used in ktm website.
/* 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);
}
/* 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;
}
<div class="dropdown">
<a class="dropbtn">Dropdown</a>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
you can edit the css file to add your custom style when dropping down
I have this dropdown menu, for my mobile site. The problem is, it will not go away when clicking outside of the dropdown menu. So when clicking the button, there is no way of getting rid of the menu again, besides choosing one of the other sites from the menu. Is it possible, when it shows, to make it go away if you click outside of it?
This is my code:
/* Dropdown Button */
.dropbtn {
background-color: #948a69;
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);
}
/* 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: #9e9370;
}
<div style="position:absolute; top:15px; left:15px;">
<div class="dropdown">
<button class="dropbtn"><b>MENU</b></button>
<div class="dropdown-content">
Velkommen
Produkter
Fadøl
Historie
Kontakt
</div>
</div>
</div>
I just had this same issue and found a solution from here:
http://www.cssplay.co.uk/menus/cssplay-ipad-dropdown-menu-fixed.html
The fix centers around using a transparent image to force the browser behavior we want. Add this CSS:
// For the Safari mobile browser
div img.close {
display: none;
width: 100%;
height: 100%;
position: fixed;
left: 0;
top: 0;
z-index: -1;
}
div button:hover + img {
display: block;
}
Then add this in your HTML, before your last </div>:
<img class="close" src="ipad/trans.gif" />
I personally am using this with lists rather than buttons, so there might be some tweaking necessary. But I can confirm it works using lists (<ul> and <li> based menu).
Hi there I have this button that has been driving me crazy!
Here is the jfiddle
HTML:
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
CSS:
/* Dropdown Button */
.dropbtn {
background-color: Transparent;
background-repeat:no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
outline:none;
text-shadow:none !important;
box-shadow:none !important;
}
/* 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);
}
/* 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 {
background-color: #3e8e41;
}
.dropbtn:hover
{
text-color: black;
}
As you can see when the button is clicked or currently showing the child links it is green! I'm trying to make it transparent so only the text shows.
I thought it was a :hover or :active but that never seemed to work so I have no idea what is causing it to turn green or how I can fix this. Any help?
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover {
background-color: #3e8e41;
}
Should be
.dropdown:hover {
background-color: transparent;
}
To make it solid - use a couple pseudo-classes
.dropdown:hover,
.dropdown:focus,
.dropdown:active {
background-color: transparent;
}
I am trying to create pure and simple HTML+CSS drop down menu which will open in a full width mode underneath each item and will also push content. This image and jsfiddle better explains what I am trying to achieve:
jsfiddle.net/66qez5tn
How do I do this ?
This code can do the drop down menu with HTML and CSS:
<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);
}
/* 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">
Link 1
Link 2
Link 3
</div>
</div>