Adjustable positioning and size of dropdown menu (triggered by clicking)

Currently developing the following webpage: https://jsfiddle.net/t9f2ca4n/212/

Check the raw code at the bottom of the page.

The issue at hand involves adding two buttons for each line item (Item I or Item II) to display setup details under "Category" and "Setup". Clicking these buttons should reveal a dropdown box with the respective information. However, the placement of the dropdown items after selecting "category" or "setup" is not aligning correctly, despite several attempts to make it stable and dynamic.

While the initial position of the pop-up box on the left side looks good, it should ideally end at the equivalent place on the right side without extending all the way to the edge. I've also struggled to ensure that the dropdown box always starts at the bottom of its selected item row, as demonstrated in the "ideal" behavior image below.

While the placement looks fine on "jsfiddle" due to the limited page size, opening the page separately reveals the issue. The current set-up is flawed as it doesn't account for a larger page where the dropdown placement is incorrect, and the width is also problematic.

Current placement issue (clicking the highlighted yellow button) - NOT JSFIDDLE: https://i.sstatic.net/J5FOrx2C.png

The box appears too low and extends too far to the right, as illustrated in the image.

How it appears on JSFiddle: Given the specific page size, the dropdown placement appears correct, but only superficially. We know that a larger page would not work as intended (refer to the previous image). The width of the dropdown is also not dynamically adjusting for the placement, making it less than ideal.

(the red X's indicate what should not be present) https://i.sstatic.net/65qDuYKB.png

Desired Ideal Behavior: https://i.sstatic.net/wj0pCQmY.png

Attempts so far: I have experimented with different display types (e.g., relative, fixed), bottom placement parameters, and maximizing available width. However, none of these solutions provided the desired dynamic and stable drop-down positioning and width.

The primary issue lies in the dropdown location. Any insights on improving the current approach for consistent placement and width adjustments irrespective of page dimensions would be greatly appreciated. It's evident from the existing trials that multiple strategies have been explored, and I am hopeful for some guidance.

Thank you for any suggestions.

Regards, F.

Code:


                /* JavaScript functions for toggling dropdown visibility */
                // Function to toggle the first dropdown
                function myFunction() {
                  document.getElementById("myDropdown").classList.toggle("show");
                }
                // Function to toggle the second dropdown
                function myFunction_two() {
                  document.getElementById("myDropdown_two").classList.toggle("show");
                }
                // Additional toggle functions for other dropdowns...
            

                /* CSS styling for dropdown and buttons */
                /* CSS code snippet */
            

                <!-- HTML structure for the page -->
                <body id="wrapper_id">
                  <div class="wrapper">
                    <!-- HTML elements for different sections and buttons -->
                  </div>
                  <footer></footer>
                

Answer №1

Having trouble with dropdown positioning? I've made a simple adjustment that should help. If you need more assistance, feel free to provide additional details.

What was changed? To address the dropdown positioning, I added the CSS property position: relative to the parent container of the dropdowns (specifically, the item_wrapper class). This ensures that all positions are relative to this container. Additionally, to make the dropdown flow downwards from the top, I applied top: 100%.

<!DOCTYPE html>
<html>

  <head>
    <style>
      /* CSS styles for elements */
    </style>
  </head>

  <body id="wrapper_id">
    
    <div class="wrapper">
      <div id="big_rcorners1" style="grid-area: tll;">Group I</div>
      <div id="big_rcorners1" style="grid-area: trr;">Group I - Changes</div>
      <br>
      
      <div id="lrcorners1" class="item_wrapper" style="grid-area: lll;">
        <button class="good_button_new" style="grid-area: block_g;" onclick="myFunction()">SETUP</button>
        <div id="myDropdown" class="dropdown-content" style="top: 100%">
          <a>TEST DROP DOWN</a></div>
        <button class="good_button_new" style="grid-area: block_f;" onclick="myFunction_two()">CATEGORY</button>
        <div id="myDropdown_two" class="dropdown-content" style="top: 100%">
          <a>CATEGORY DROP DOWN</a></div>
        <p style="grid-area: block_e; text-align: middle center; vertical-align: middle;">SAMPLE_TXT</p>
        /* Additional elements and content */
      </div>
      
      /* More content and elements */
    </div>

  <footer></footer>
  
  <script>
    /* JavaScript functions for dropdown toggling */
    // Event listeners and functions
    </script>
  
  </body>

</html>

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

Tips on keeping an HTML element from shifting when resizing the browser window (how to lock an element in a specific position on a webpage)

As a newcomer to front-end web development, I recently attempted creating a basic responsive page. While working on this project, I encountered an issue with the candle holder image. This image is size-responsive, meaning it adjusts in size as the browser ...

Puppeteer exhibiting unexpected behavior compared to the Developer Console

My goal is to extract the title of the page using Puppeteer from the following URL: Here's the code snippet I am working with: (async () => { const browser = await puppet.launch({ headless: true }); const page = a ...

Deciphering unconventional JSON formats

Can anyone identify the format of this JSON (if it even is JSON!) from the code snippet below? I've extracted this data from a website's HTML and now I'm struggling to parse it in C# using a JSON parser. The data requires significant preproc ...

Adaptive Website displayed within an iframe

Currently, I have a responsive website that can be viewed here. The main objective is to embed this site into an iframe while maintaining its responsiveness. I attempted embedding my site in JSFiddle for testing purposes, which you can see here. However ...

What could be causing the flexbox code to not take effect and the text to refuse wrapping?

I've been utilizing flexbox to style my content, and it worked seamlessly on the first three divs. However, when I attempted to apply the same styling to the fourth one, it didn't quite work out as planned. Despite trying options like flex-wrap t ...

Each page is restricted to containing only a single server-side Form tag

I inserted a page with HTML code into a master page and encountered this error when I tried to launch the page. I'm unsure of what I did wrong! <%@ Page Title="" Language="C#" MasterPageFile="~/YourGuruMaster.master" AutoEventWireup="true" Cod ...

Tips for building dynamic web interfaces that allow for interactive communication in both directions

I'm struggling to find the right keywords to search for, so I'll explain what I'm attempting to accomplish using a basic example and see if anyone can suggest some potential techniques or technologies. Imagine a scenario where one or more i ...

The file upload process did not return a successful response for Ajax

Recently delved into the world of Ajax and encountered a perplexing issue. Here is the dilemma: I successfully upload a .csv file to the server. However, after the upload "success" in the ajax call fails to trigger a response. Neither does complete or er ...

nth-child selector causing tbody element to break

I have a code snippet that should alternate row colors between yellow and red. It works perfectly without using the <tbody> tags. However, when I include the <tbody> tags in my code, the layout breaks. It seems to restart with yellow at every n ...

Leverage angular-translate to establish placeholder text upon blurring

Just starting out with Angular and facing the challenge of implementing localization in my project. I have a lot of input fields that need their placeholders translated. In my HTML, I'm trying to achieve this: <input type="email" placeholder="{{ & ...

Is Bootstrap CSS cached by Chrome?

As I began a new project, I decided to tweak the color settings in bootstrap. During debugging in Visual Studio, everything appeared as intended. However, after publishing the site for the first time on a server, I noticed that the color change I made in ...

Safari does not stop the scrolling of the <body style="overflow-y: hidden"> tag

Welcome to this simple HTML page <body style="overflow-y: hidden"> ... </body> This page is designed to prevent scrolling by using the CSS property overflow-y: hidden. While this functionality works as intended on most browsers, it does ...

React/MUI Popover not aligning properly due to anchorPosition issue

I'm currently implementing a React/MUI Popover within a List element from react-window. However, I'm facing an issue with positioning the Popover correctly, as it always ends up in the top left corner of the window. This occurs because the compon ...

Conceal a row depending on a cell's value being equal to zero

Having an issue with hiding rows that have a zero value in the middle cells. The values in the middle cells are dynamically generated from a Google Spreadsheet. The code below successfully hides rows with zero values that I manually input, but it doesn&apo ...

Display HTML element within the <samp> tag using PHP

I'm using Bootstrap for my form control, but my input and button are not on the same line. <div class="form-group"> <label class="col-lg-3 control-label" id="title-meaning">Meaning:</label> <div c ...

Is there a way to trigger a click event on an href using asp code behind?

Is there a way to programmatically trigger a click event on the href "shipping_question_ID_product_received_as_ordered_link" from the code behind of an ASP form? This href triggers a complex function in jquery when clicked by the user. I need to simulate ...

Testing the material-ui toggle component by simulating a click event

Trying to test functionality based on the material-ui toggle component with jest and enzyme has been challenging for me. Even though my generic clickIt function usually works well with other material-ui components, it doesn't seem to trigger the stat ...

Can you identify the issue with the phase control in my sine wave program?

I have recently developed an interactive sine wave drawing web application. Below is the code snippet for reference: const canvas = document.getElementById("canvas"), amplitude_slider = document.getElementById("amplitude_control"), wavelength_slider ...

Are inline styles being utilized in a Styled component?

I'm currently utilizing styled-components within a React project and I am having trouble finding documentation regarding whether or not a styled component can accept inline styles on the "style" property. For example: The Text component has been styl ...

Embed one module within another module and utilize the controller from the embedded module

I am attempting to create a module and inject it into the main module. Then, I want to inject the controller into the injected module but am facing issues. In my index.html file: <html ng-app="MenuApp"> <head> </head> <body> <d ...