Why isn't my dropdown menu appearing correctly?

After trying numerous approaches, I am still struggling to create a drop-down menu from an image. The cursor does change to a pointer, but something is clearly not right.

Here are the HTML and CSS snippets I am currently working with:

<a>

  <img class="dropbtn" id="profile" src="img/icons/Profiel.png" alt="Profiel" width="50px" heigt="50px">

    <div id="myDropdown" class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </img>
</a>
/* 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;}

Answer №1

Refer to the code snippet below

$(".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">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
 
</a>

Answer №2

Would you be interested in utilizing jQuery for this task?

To achieve your desired outcome, all you need to do is add the following code:

$(".dropbtn").click(function() {
  $("#myDropdown").toggleClass("open");
});

Take a look at the Codepen Demo here

$(".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">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  </img>
</a>

Answer №3

Did you remember to change the dropdowns CSS to display: block; when the image is clicked?

If not, give this a try with (JavaScript):

var trigger = document.getElementById("profile"),
    dropdown = document.getelementById("myDropdown");

trigger.onclick = function(){ // click on the image
    dropdown.classList.toggle("active"); // toggle the display of dropdown
});

CSS:

or use jQuery (if it's included in your website):

$("#profile").click(function(){ // click on the image
    $("#myDropdown").toggleClass("active"); // toggle the display of dropdown
});

Ensure (if you are unfamiliar) to add the above code within a script tag in your <head> or <body> tags in your HTML document!

#myDropdown.active{ /* if #myDropdown has class "active" */
    display: block; /* show the dropdown */
}

Hopefully, this provides some assistance! :-)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mastering the intricacies of Angular bindings through intuition

I am attempting to grasp the concept of distinguishing between square brackets binding <elem [...]="..."><elem> and parentheses binding <elem (...)="..."<elem>. Is there a rule of thumb that can help differentiate between the two? Pe ...

The behavior of Django Admin is rather peculiar

My Django app is running smoothly, but I've noticed some strange behavior in the Django Admin interface. When I click on a model, instead of being taken to the list of records for that model, I'm redirected to the same model list but with a pecul ...

Can you affix a tag to the picture?

Currently, I have a static page located at . However, I am planning to dynamically generate this page by retrieving data from a database. My challenge lies in assigning a position ID obtained from the database to each of the tyres on the page. Any sugges ...

How to Change Background Color to Black for a PNG Image Using CSS in Ionic

When applying CSS in Ionic, I encountered an issue with the background image causing the background to turn black. Without the image, everything looks fine. ion-content { --background: url("/assets/product_background.png") 0 0/100% 95% no-re ...

What are some more efficient methods for implementing page location detection with Angular directives?

My website currently features a navigation bar with 4 anchor links that direct users to different sections of the page. As you scroll down the page, the corresponding link on the nav bar lights up to signify your position on the site. Experience it yourse ...

Click to Rotate Angular Chevron

Is it possible to animate the rotation of a chevron icon from left-facing to right-facing using Angular? CSS: .rotate-chevron { transition: .1s linear; } HTML: <button [class.button-open]="!slideOpen" [class.button-close]="slideOpe ...

What could be causing jQuery.css() to not function properly?

I've been encountering some challenges with a piece of code and haven't been able to make it function as intended. I'm relatively new to JavaScript/jQuery, so bear with me as I navigate through this learning process. As part of the Free Cod ...

Integrating CSS into dynamically generated table rows using jQuery

Having trouble applying CSS to table rows created via ajax request. Despite using jQuery to add CSS to newly generated rows, it only affects the existing table headers. The table already has a 'sortable' class which also doesn't apply to dy ...

Is it possible to create HTML code that remains unaffected by alterations in CSS class names?

Having been using Twitter Bootstrap 3 for some time, the arrival of Twitter Bootstrap 4 has caught my attention. The new Twitter Booststrap comes with various changes, such as the renaming of .table-condensed to .table-sm, for instance. In anticipation of ...

"Encountering a glitch with masterpage.cs in Aspx.net and C#

Currently facing a perplexing issue while developing a website using aspx.net and c#. The following code snippet is on my masterpage: protected void Page_Load(object sender, EventArgs e) { if (HttpContext.Current.Request.Url.AbsolutePath == "/ ...

Creating a CSS class dynamically with AngularJS: A step-by-step guide

I'm working on an Angular JS website that pulls data from an API to create a dynamic CSS class. This class will be used to format other data being displayed on the page. Is there a way for Angular JS $scope data to be generated in the Controller whil ...

Can a circular design incorporating an arrow and gradient background be created?

I'm looking to create a circular design with an arrow and a gradient inside that can adjust dynamically based on screen resizing. I know it can be done using an image, but I'm wondering if it's possible to achieve this effect using just a si ...

JQuery modal for creating vibrant and personalized color schemes

I have a basic jQuery modal code that uses grey as the default color. How can I customize this to create a colorful toolbox, for example changing the color from grey to blue? Also, how can I use this script for multiple dialogs, like setting #dialog1 to be ...

I am currently working on integrating a Netlify form into a contact form using React.js

I attempted various strategies but couldn't achieve any progress. I also consulted this post However, none of the suggestions seemed to work for me. Is there any other way to accomplish this? import React, { Component } from 'react'; impor ...

Quick way to include id="" and class="" attributes to HTML elements using Sublime Text 2

Is there a way to configure Sublime Text 2 where inputting a . (dot) automatically generates class=" " and adding a # (hash) creates id=" " while typing an HTML opening tag? ...

Heading made of ribbon without increasing the scrolling space

Incorporating a unique ribbon-style heading that stretches out from the content area on both sides to create a 3D effect using an image background without relying on CSS3 techniques. I experimented with floating elements, negative margins, and relative po ...

How can I align content to the left within a div that is right-aligned?

When I attempt this, the content aligns to the right as expected: <div class="text-right"> ... ... ... </div> I had hoped that this would result in left-aligned content in relation to itself, but right-aligned within the ...

A space designated for numerous receivers

Is there a way to create a field that contains other elements, similar to sending messages to multiple users in a social network? https://i.stack.imgur.com/P9e24.png I attempted to understand the code for this, but it's quite complex. If anyone could ...

Changing the alignment of divs to center instead of the right side

I am currently working on a project that involves creating a tiled interface with responsive tiles. One issue I have encountered is that all the tiles are shifting to the left side of the div and not getting centered, no matter what I try. To see the pro ...

chosen=chosen based on data from mysql database

Currently, I'm developing my own CMS that allows users to add and update their projects. While the adding function works smoothly, the updating process also functions properly. However, a problem arises when I click on a project (retrieving informatio ...