alignment problems in dropdown menus

I'm currently working with 3 bootstrap dropdown elements. The toggle for the first two is not controlled by jQuery, but for the third one, I am using jQuery to show and hide it. However, I've encountered some issues that need fixing. When clicking on the buttons for the first two dropdowns, their menus display correctly, while the third dropdown has problems. First of all, its menu doesn't show on the first click; you have to click it a second time to make the menu appear. Secondly, the positioning of the third dropdown menu is incorrect initially, but scrolling on the body fixes this issue. Upon further investigation, I discovered that removing the other two dropdown elements resolves the third dropdown's functionality and corrects the second problem. Despite controlling its toggle with jQuery, I can't seem to figure out what the underlying issue is. How can I go about resolving these issues? Thank you in advance.

$(function() {
  $(".account button").click(function() {
    $(".account .dropdown-menu").toggle();
  });
});
.header {
  display: flex;
  flex-direction: row;
  justify-content: right;
  align-items: center;
  column-gap: 4%;
  width: 100%;
  height: 20vh;
  background-color: gold;
}

.header .dropdown {
  border: none;
}

.header .dropdown button:focus {
  box-shadow: none;
}

.header .dropdown.account ul{
    transform: translate(0px, 40px) !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css" integrity="sha384-3B6NwesSXE7YJlcLI9RpRqGf2p/EgVH8BgoKTaUrmKNDkHPStTQ3EyoYjCGXaOTS" crossorigin="anonymous" />

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5954544f484f495a4b7b0e150a1508">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0828f8f949394928190a0d5ced1ced3">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcded3d3c8cfc8ceddccfc89928d928f">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<div class="header">
  <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown account">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-user"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
</div>

Answer №1

Do you mind checking if this code snippet is functioning properly?

.header {
  display: flex;
  flex-direction: row;
  align-items: center;
  column-gap: 4%;
  width: 100%;
  height: 20vh;
  background-color: gold;
}

.header .dropdown {
  border: none;
}

.header .dropdown button:focus {
  box-shadow: none;
}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Dropdown</title>
    
    <!-- CSS -->
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css" integrity="sha384-3B6NwesSXE7YJlcLI9RpRqGf2p/EgVH8BgoKTaUrmKNDkHPStTQ3EyoYjCGXaOTS" crossorigin="anonymous" />

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e98b86869d9a9d9b8899a9dcc7d8c7da">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  </head>
  <body>
    <div class="header">
     <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown account">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-user"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
</div>
    
    <!-- Js -->
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c5c8c8d3d4d3d5c6d7e792899589978ac5c2d3c696">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </body>
</html>

Answer №2

There's no requirement to include the JavaScript code. Verify if it functions correctly without it.

Answer №3

Take a look at this example: transform: translate(0px, 40px). Add it to the class header .dropdown.account ul and it should work perfectly fine.

$(function() {
  $(".account button").click(function() {
    $(".account .dropdown-menu").toggle();
  });
});
.header {
  display: flex;
  flex-direction: row;
  align-items: center;
  column-gap: 4%;
  width: 100%;
  height: 20vh;
  background-color: gold;
}

.header .dropdown {
  border: none;
}

.header .dropdown button:focus {
  box-shadow: none;
}

.header .dropdown.account ul{
      transform: translate(0px, 40px) !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css" integrity="sha384-3B6NwesSXE7YJlcLI9RpRqGf2p/EgVH8BgoKTaUrmKNDkHPStTQ3EyoYjCGXaOTS" crossorigin="anonymous" />

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c6e6363787f787e6d7c4c39223d223f">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d1dcdcc7c0c7c1d2c3f3869d829d80">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a2afafb4b3b4b2a1b080f5eef1eef3">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<div class="header">
  <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown account">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-user"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
</div>

Answer №4

Adding the CDN below should help resolve this particular issue.

<script
   src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd9f9292898e898f9c8dbdc8d3ccd3ce">[email protected]</a>/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
      crossorigin="anonymous">
</script>

Answer №5

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css" integrity="sha384-3B6NwesSXE7YJlcLI9RpRqGf2p/EgVH8BgoKTaUrmKNDkHPStTQ3EyoYjCGXaOTS" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9dbd6d6cdcacdcbd8c9f98c9788978a">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script
  src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab8b5b5aea9aea8bbaa9aeff4ebf4e9">[email protected]</a>/dist/js/bootstrap.bundle.min.js"
      integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
      crossorigin="anonymous"
    ></script>
<style>
.header {
  display: flex;
  flex-direction: row;
  align-items: center;
  column-gap: 4%;
  width: 100%;
  height: 20vh;
  background-color: gold;
}

.header .dropdown {
  border: none;
}

.header .dropdown button:focus {
  box-shadow: none;
}

.header .dropdown.account ul{
      transform: translate(0px, 40px) !important;
}
</style>
<script>
$(document).ready(function(){
  $(".btn btn-secondary").click(function(){
    $(".dropdown-menu").toggle();
  });
});
</script>
</head>
<body>

<div class="header">
  <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-magnifying-glass"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
  <div class="dropdown account">
    <button class="btn btn-secondary" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa-solid fa-user"></i>
                    </button>
    <ul class="dropdown-menu">
      <li><a class="dropdown-item switchProfile">option 1</a></li>
      <li><a class="dropdown-item myAccount">option 2</a></li>
      <li><a class="dropdown-item logOut">option 3</a></li>
    </ul>
  </div>
</div>

</body>
</html>

Verify that everything is functioning correctly.

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

The search feature using MySQL, PHP, and AJAX is malfunctioning

Hey everyone, I'm currently working on setting up a live search feature using PHP, MySQL, and AJAX. However, I seem to be encountering some issues in my implementation. My database is hosted on phpMyAdmin with the name "Info", and the specific table I ...

CSS class is functioning properly in conjunction with the textarea element, but it is not properly

I have a CSS class called ".form_bg_color" with the following definition: .form_bg_color{ background: red; } When I apply this class to my text field in the view like so: <%= f.text_field :source, class: "form_bg_color" %> The color of the input ...

Guide on adding JSON data from a web service whenever a function is invoked in AngularJS

I am currently calling the getData function every second and everything is working fine except for the appending functionality. Whenever the function is called, AngularJS replaces the old data with new data, but I actually want to append the new data aft ...

What is the reason behind the inability to set a maximum width for containers?

Here's a piece of XML code that I'm working with, and I need to figure out how to set the max width for the linear layout to 600dp. Why is it that Android doesn't allow this kind of customization? EDIT: The main question here is not about h ...

Create a function that binds a select dropdown to each table column header using JavaScript Object Notation (JSON), and then populate an HTML table with the

I have a dynamic table populated with JSON data, and I would like to add a select dropdown for each column. The challenge is that the number of columns is not fixed and they are also populated by JSON. Therefore, I want the select dropdown at the top of ea ...

Adjust the color of the entire modal

I'm working with a react native modal and encountering an issue where the backgroundColor I apply is only showing at the top of the modal. How can I ensure that the color fills the entire modal view? Any suggestions on how to fix this problem and mak ...

Can anyone tell me how to retrieve the value of {{org}} in this situation?

<head> <title>My Unique Page</title> </head> <body> <input type="text" ng-model="selectedOrg" ng-show="!isSuperAdmin" readonly /> <select id="nameOrg" ng-model="selectedOrg" ng-cha ...

I'm struggling to successfully insert and retrieve data from a MySQL database using Express.js. I need to figure out how to make these operations work seamlessly across the board

HTML CODE <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>CHAT APPLICATION</title> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+ ...

Dealing with Ajax errors in Django reponses

My code includes an ajax call to a Django view method: $("#formi").submit(function(event){ event.preventDefault(); var data = new FormData($('form').get(0)); $.ajax({ type:"POST", url ...

Production environment poses a challenge as Rails 4 struggles to locate fonts

Situation: I am facing an issue with my Rails 4.0.0 application deployed using capistrano, where the asset precompilation does not work properly on my production server. Challenge: Despite successfully adding a font and using it with @font-face locally, t ...

Issues with implementing Bootstrap 4 navbar on a Rails 5 application

I'm currently in the process of transitioning my static HTML/CSS/JS website to a Rails application. Using Bootstrap 4, I had all the functionality and CSS classes working perfectly on the static site. However, after the migration to the Ruby app, the ...

Using jQuery to Obtain a Random Item

Just started diving into ajax requests using jQuery. While I've checked out some other posts, I'm not quite sure how to go about implementing it. Right now my code is only grabbing the first 3 items, but I want it to grab 3 random items from the ...

What modifications can be made to the code in order to show the upload progress for each individual file when uploading multiple files simultaneously?

I am currently working on an uploader that can handle multiple file uploads and displays the progress of each uploaded file. However, I am facing some difficulties in showing the progress of every individual file. Can anyone provide me with assistance? &l ...

Why isn't my function being triggered by the Click event?

I can't figure out why the click event isn't triggering the btn() function. function btn() { var radio = document.getElementsByTagName("input"); for (var i = 0; i > radio.length; i++){ if (radio[i].checked){ alert(radio[i].value); } ...

Unleashing the Power of Wildcards in HTML with XPath

When using XPath to extract values from DOM elements, I've encountered inconsistent XPaths. To select all DOM elements on the same level, I've resorted to some wildcard magic. For instance, in an HTML document, my XPaths may look like: //div[@i ...

Unsuccessful attempt at implementing knockout data-binding on a dynamic webpage utilizing the Hot Towel API

I'm facing an issue while trying to bind knockout data to a dynamically generated input field. To briefly explain the canActivate(id) function, it retrieves details of all necessary objects for the page and stores them in vm.OPCLayoutVm. ...

Some websites are not displaying the CSS code for the inspector

In my experience, I have always relied on Firefox Inspector to troubleshoot CSS issues without any problems. However, when I attempted to analyze the CSS of a specific webpage (only encountering difficulties while logged in), I noticed that the inspector ...

Prevent additional clicks on the image

Currently, I am dealing with a situation where I have a list containing a jQuery handler for mouse clicks. The dilemma is that I need to insert an image into the list, but I want clicking on the image to trigger a different function than clicking elsewhere ...

Manipulating the DOM by linking to a div and using JSON information for opening and closing

I have retrieved data from a JSON file and displayed it as a link. I am looking to implement functionality that allows me to hide and show a div when the link is clicked. Below is the code I'm currently using: $(document).ready(function() { $ ...

Adjust the Placement of Images in Relation to Mouse Movements

Is there a way to creatively animate the positions of images or background images based on mouse movement? Let's take Github, for instance: https://github.com/thispagedoesntexist The 404 page on Github is truly impressive. I aim to captivate my use ...