I am having trouble getting my dropdown to line up with the drop button labeled "MORE"

Despite trying multiple approaches, I am still struggling to align the dropdown content with the dropbtn. My goal is to have the content consistently positioned below the more menu.

HTML:

`
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
<head>
    <link rel="stylesheet" href="css-exper.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="jquery-3.3.1.min.js"></script>
</head>
<body>
    <div id = "container">
    <div class="nav" id="mynav">
        <ul class="menu" id ="menu">
        <li><a href="#">Menu</a></li>
        <li><a href="#">Menu</a></li>
        <li><a href="#">Menu</a></li>
        <li><a href="#">Menu</a></li>
        <li><a href="#">Menu</a></li>
        <li id ="more">
        <div class="dropdown">
          <a class="dropbtn" >More
            <i class="fa fa-caret-down"></i></a></div></li>
    </ul>
      <ul class="dropdown-content" id="drop">
        <li id ="stay"><a href="#">Menu</a></li>
        <li id ="stay"><a href="#">Menu</a></li>
      </ul>
      </div>
  </div>

<script src ="test.js"></script>
    </body>

CSS:

#container{ 
      width:1000px; 
      padding:100px 0; 
      margin:0px auto; 

}

.menu{ 
  padding:0;
   }

.nav{
    overflow: hidden; 
    text-align:center;
    margin: 0 auto;
}

.nav a, .dropbtn {
    display: inline-block;
    color: black;
    font-family: sans-serif;
    text-decoration:none;
    font-size: 17px;
    padding: 40px;
}

.nav a:hover{
    color: #DBB569;
}

.nav li{
    display: inline-block;
}

li a.active {
    color: #DBB569;
}

.nav .icon {
    display: none;
}

.dropdown .dropbtn {   
    border: none;
    outline: none;
    background-color: white;
}

.dropdown-content {
    display: none;
    position: absolute;
    max-width:200px;
    -webkit-box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    background-color:white;
    z-index: 1;
    margin-top: -1.5em;
    padding: 0;
    list-style: none;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;

}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 959px){

  #container,p{
    width: 100%;
  }

  .nav a, .dropbtn{
    padding: 30px;
  }
}

@media screen and (max-width: 640px){

  .nav a, .dropbtn {
    padding:20px;
  }
}

@media screen and (max-width: 320px){

#container{
  width: 320px;
}

}

JS (Responsible for hiding items along the way in More):

var all_widths = [110.64,144.64,199.14,135.19,162.59,115.63];

$(document).ready(function(){

 var item_count = ($( "#menu li" ).length);
 var menu_width = ($("#menu").width());
 var items_width = 0;

  for(var i = 0; i<item_count;i++){
    items_width += all_widths[i];
  }



while(menu_width < items_width + 20){
  items_width-= $('#menu li').not('#more').last().width();
  $('#menu li').not('#more').last().appendTo($('.dropdown-content'));
  $('#more').appendTo($('#menu'));
  $('#more').show();
}




$(window).resize(function(){ 

var item_count = ($( "#menu li" ).length);
var menu_width = ($("#menu").width());
var items_width = 0;
var bool = new Boolean(false);


for(var i = 0; i < item_count; i++){
  items_width += all_widths[i];
}

while(menu_width < items_width + 20){
  items_width -= $('#menu li').not('#more').last().width();
  $('#menu li').not('#more').last().appendTo($('.dropdown-content'));
  $('#more').appendTo($('#menu'));
  $('#more').show();
  bool = true;

}

while ((menu_width >= items_width + all_widths[item_count-1]) && bool == false){
  items_width += all_widths[item_count-1];
  $('.dropdown-content li').not('#stay').last().appendTo($('#menu'));
  $('#more').appendTo($('#menu'))
}


});

  $('#more').click(function(){
   $('.dropdown-content').slideToggle(); 
  });
  });


    `

I require assistance in resolving this issue. Please advise on how I can achieve a static yet responsive alignment. Your help is greatly appreciated!

Answer №1

When the dropbtn is clicked, the position of it (using the left position) is obtained and added to the dropdown's CSS (with a -50px adjustment to center it beneath the button).

(Only the click function on #more was modified, everything else remains the same)

var all_widths = [110.64, 144.64, 199.14, 135.19, 162.59, 115.63];

$(document).ready(function() {

  var item_count = ($("#menu li").length);
  var menu_width = ($("#menu").width());
  var items_width = 0;

  for (var i = 0; i < item_count; i++) {
    items_width += all_widths[i];
  }



  while (menu_width < items_width + 20) {
    items_width -= $('#menu li').not('#more').last().width();
    $('#menu li').not('#more').last().appendTo($('.dropdown-content'));
    $('#more').appendTo($('#menu'));
    $('#more').show();
  }

  


  $(window).resize(function() {

    var item_count = ($("#menu li").length);
    var menu_width = ($("#menu").width());
    var items_width = 0;
    var bool = new Boolean(false);


    for (var i = 0; i < item_count; i++) {
      items_width += all_widths[i];
    }

    while (menu_width < items_width + 20) {
      items_width -= $('#menu li').not('#more').last().width();
      $('#menu li').not('#more').last().appendTo($('.dropdown-content'));
      $('#more').appendTo($('#menu'));
      $('#more').show();
      bool = true;

    }

    while ((menu_width >= items_width + all_widths[item_count - 1]) && bool == false) {
      items_width += all_widths[item_count - 1];
      $('.dropdown-content li').not('#stay').last().appendTo($('#menu'));
      $('#more').appendTo($('#menu'))
    }


  });

  $('#more').click(function() {
  var btnPos = this.getBoundingClientRect();
  
    $('.dropdown-content').css({'left': btnPos.left - 50}).slideToggle();
  });
});
#container {
  width: 1000px;
  padding: 100px 0;
  margin: 0px auto;
}

.menu {
  padding: 0;
}

.nav {
  overflow: hidden;
  text-align: center;
  margin: 0 auto;
}

.nav a,
.dropbtn {
  display: inline-block;
  color: black;
  font-family: sans-serif;
  text-decoration: none;
  font-size: 17px;
  padding: 40px;
}

.nav a:hover {
  color: #DBB569;
}

.nav li {
  display: inline-block;
}

li a.active {
  color: #DBB569;
}

.nav .icon {
  display: none;
}

.dropdown .dropbtn {
  border: none;
  outline: none;
  background-color: white;
}

.dropdown-content {
  display: none;
  position: absolute;
  max-width: 200px;
  -webkit-box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  background-color: white;
  z-index: 1;
  margin-top: -1.5em;
  padding: 0;
  list-style: none;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
}

.dropdown:hover .dropdown-content {
  display: block;
}

@media screen and (max-width: 959px) {
  #container,
  p {
    width: 100%;
  }
  .nav a,
  .dropbtn {
    padding: 30px;
  }
}

@media screen and (max-width: 640px) {
  .nav a,
  .dropbtn {
    padding: 20px;
  }
}

@media screen and (max-width: 320px) {
  #container {
    width: 320px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container">
  <div class="nav" id="mynav">
    <ul class="menu" id="menu">
      <li><a href="#">Menu</a></li>
      <li><a href="#">Menu</a></li>
      <li><a href="#">Menu</a></li>
      <li><a href="#">Menu</a></li>
      <li><a href="#">Menu</a></li>
      <li id="more">
        <div class="dropdown">
          <a class="dropbtn">More
            <i class="fa fa-caret-down"></i></a></div>
      </li>
    </ul>
    <ul class="dropdown-content" id="drop">
      <li id="stay"><a href="#">Menu</a></li>
      <li id="stay"><a href="#">Menu</a></li>
    </ul>
  </div>
</div>

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

How can I adjust the width of a handle/thumb for NoUiSlider?

Looking to adjust the width of a NoUiSlider using CSS: .noUi-horizontal .noUi-handle { width:8px; height:25px; left: 0px; top: -8px; border: 0px solid #000000; border-radius: 0px; background: #000; cursor: default; box- ...

"Struggling to make the 'overflow: hidden' property work for an absolutely positioned

I'm struggling to conceal an absolutely positioned image within a CSS grid layout. Below is the code snippet: HTML: <div class="relative-parent"> <div v-for="item in 12" class="hiding-parent"> <div c ...

Icon alignment to wrapped text width has been successfully achieved with the implementation of Flexbox onto a single

I am facing an issue with text wrapping within a flexbox. I want the width of the flex item to always match the length of the wrapped text so that an icon placed next to the item stays adjacent to the text. However, due to text wrapping, there is a gap bet ...

Animate the transition of the previous element moving downward while simultaneously introducing a new element at the top

I currently have a hidden element called "new element" that is controlled by v-if. My goal is to create a button labeled "display" that, upon clicking, will reveal the new element on top after sliding down an old element. How can I achieve this using CSS ...

"Utilizing flex-box to create a hover effect for child elements

I am attempting to create a grid caret with a top and bottom caret that displays a grey area when the user hovers over each side. <div class="control-wrap"> <div class="caret-wrap"> <span class="caret">▲</span> </div&g ...

Troubleshooting a problem with the Bootstrap dropdown menu

Can anyone assist with an issue regarding Bootstrap 4.4.1? I am facing a problem where two consecutive dropdown links+menus are not displaying correctly. The second dropdown menu is showing the content of the previous link instead of its own, even though t ...

Is there a way to use a less mixin or selector to adjust the position depending on the sibling

Currently, I am working on adjusting the position of multiple sibling divs depending on their order. Although they are all currently set to position: absolute, this may change. Each div is a few hundred pixels tall, but I want them to overlap in such a wa ...

How can I align a mapped button to appear on the opposite side of the data using Tailwind CSS?

I've been struggling to find a straightforward solution to this issue for a while now. Let me break it down - I need to display some data on the left side of a post card component with a button located right next to it. Here's an illustration: ...

The CSS on the maps infobox is not rendering properly when an icon is clicked

Hey everyone, I'm in the process of creating a travel blog on WordPress and have encountered some issues with the CSS on my site. I've included maps and tables that look fine in my fiddle, but when I transfer them to my website, they appear disto ...

Initiate a page break at the beginning of the table in case there are not enough lines on the first page

Does anyone know how to ensure that a table starts on a new page when printing, rather than continuing at the bottom of the previous page? I have tables with repeating headers and many rows, and sometimes a new table will start at the very end of a page. ...

Is it possible to generate an image from HTML using PHP?

Can anyone suggest a way to generate an image from pure HTML using PHP, possibly utilizing the GD library or other similar tools? I am looking to display icons from external sites and considering if creating an image would help improve load times. If not ...

What is the process for disabling the CSS module feature in Next.js?

In Next.js, Global CSS can only be imported in _App.js. However, importing global CSS in every component is not allowed, so we have to use CSS modules to comply with this restriction imposed by Next.js. Currently, I am in the process of migrating a large ...

"Customize your WordPress theme by moving the sidebar from left to right for

Currently, I am utilizing the flat theme sourced from . In an attempt to customize the appearance, I made modifications within style.css: #page:before, .sidebar-offcanvas, #secondary {float: right !important;} However, this adjustment only applies to a ...

Lower the placement of Glyphicons

I have recently implemented a custom font on my website. Unfortunately, this font is not standard and its alignment is off. As a result, when I use this font with Twitter Bootstrap glyphicon, they do not appear in the same line. Is there a way to adjust t ...

What sets apart auto, 0, and no z-index values?

Can you explain the distinctions between the following: z-index: auto z-index: 0 the absence of z-index In all scenarios mentioned, we have a container div that contains two inner divs, named div1 and div2, with respective z-index values of 9 and 10. T ...

Issue with continuous loader malfunction

I integrated a 3-second mini-loading animation on my website. It shows up every time I refresh the site or navigate to another page. The issue I'm facing is that once I add the loading section, it never stops (it should only last for 3 seconds). Usua ...

Tips for addressing the issue of mat-list-item not occupying the entire row's space

Hello everyone, I am currently trying to render an article.component.html within my article-list-component.html in a list format. When I use plain HTML, it renders correctly as shown in picture 1: Title - author - Date Here is the code for my article-list. ...

How to switch around div elements using CSS

There are two unordered list items in a container div and one swap button. When the swap button is clicked, the order of items needs to change. This functionality can be achieved using the following swap function. var ints = [ "1", "2", "3", "4" ], ...

Finding the Perfect Placement for an Element in Relation to its Companion

Is there a way to achieve an effect similar to Position Relative in CSS using jQuery? I have developed a tooltip that I want to attach to various objects like textboxes, checkboxes, and other text elements. My code looks something like this: <input i ...

Turn off padding in material ui card

Currently, I am utilizing a material UI card and upon inspecting the card, I noticed the presence of this unique class: "MulticardContent-root". This class adds padding of 16 which I would like to remove. Strangely, it is not located within the styles co ...