What is the best way to place this dropdown in a fixed position within a parent element with overflow scrolling

I am facing an issue with a dropdown menu inside a parent div. The parent div has a fixed height and is set to overflow: auto. My goal is to have the menu extend beyond the boundaries of the parent when opened, but currently it just increases the height of the parent.

You can view a minimal test case on CodePen, or refer to the code below:

HTML

<div class="parent">
    <div class="dropdown">
        <button id="btn" class="btn" type="button">Click me</button>
        <div id="menu" class="menu">I should protrude outside the parent, but instead I trigger scrolling within the parent</div>
    </div>
</div>

SCSS

.parent {
    height: 3rem;
    padding: 0.25rem;
    background-color: #f5f5f5;
    overflow: auto;
    text-align: center
}
.btn {
    font-size: 1.2rem;
}
.dropdown {
    display: inline-block;
    position: relative;
}
.menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    background-color: white;
    border: 1px solid #eee;
    padding: 0.5rem;
    &.show {
        display: block;
    }
}

(The JavaScript simply toggles the show class on the dropdown and is not relevant to this issue.)

I have tried wrapping the dropdown in another div and experimenting with various overflow values on both the dropdown and parent elements without success. I prefer not to resort to moving the menu outside of the parent and positioning it through JavaScript, as I believe there must be a CSS solution available.

If anyone has any insights or solutions to this problem, they would be greatly appreciated!

*Note that it is crucial for the dropdown to remain in the normal document flow, as multiple instances may appear one after the other within the parent div.

Answer №1

Here's a clever little trick you can try out. It may not be the conventional way of doing things, but it gets the job done.

Keep in mind that when scrolling, the menu will not move along with the page. You'll need to add code to detect scroll events on the document and adjust the menu position accordingly.

document.getElementById('btn').onclick = function openMenu() {

  var btn = document.getElementById('btn');
  
  var top = btn.offsetTop;
  var left = btn.offsetLeft;
  var height = btn.offsetHeight;
  
  
  var menu = document.getElementById('menu');
  menu.style.top = top + height +'px';
  menu.style.left = left + 'px';
  
  menu.classList.toggle('show');
};
.parent {
  height: 3rem;
  padding: 0.25rem;
  background-color: #f5f5f5;
  overflow: auto;
}

.btn {
  font-size: 1.2rem;
}

.dropdown {
  display: inline-block;
}

.menu {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  max-width: 100px;
  background-color: white;
  border: 1px solid #eee;
  padding: 0.5rem;
}
.menu.show {
  display: block;
}
<div class="parent">
  <span>The button needs to stay within the document flow to work well with other elements in the parent.</span>
  <div class="dropdown">
    <button id="btn" class="btn" type="button">Click me</button>
    <div id="menu" class="menu">I'm supposed to stick out of the parent, but I end up making the parent scroll instead.</div>
  </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

The Angular Observable continues to show an array instead of a single string value

The project I am working on is a bit disorganized, so I will try to explain it as simply as possible. For context, the technologies being used include Angular, Spring, and Maven. However, I believe the only relevant part is Angular. My goal is to make a c ...

Display various divs simultaneously based on the quantity of items in the dropdown menu

In my project, there is a dynamic select list that retrieves items from the database. Users have the ability to add or delete items from this list. Below is some code related to this functionality: <div class="form-group col-md-3"> <la ...

Using the loop function within HTML in JavaScript with React

I'm trying to loop over my SliderComponent function with different inputs within the HTML to generate multiple components, but I can't seem to get it to work. I came across a solution online that involves building a string and returning it as HTM ...

Ramjet: Unveiling the Magic of Making Elements Appear and Disappear

Currently, I am attempting to implement the code for ramjet from . However, I am facing an issue where element a does not disappear when transitioning into b. Additionally, I am encountering an error message "--Uncaught TypeError: Cannot read property &apo ...

Showing JSX/HTML content depending on the props received

Can you identify the name of this type of expression and do you know in what scenarios it should be applied? {props.type === "big" && <h2>{props.title}</h2>} ...

"Step-by-step guide for incorporating a right-to-left (RTL) Bootstrap

Is there a way to make my bootstrap navbar right-to-left (RTL)? I want the logo to be on the right and the links to flow from right to left. I've tried various tricks but none of them seem to work. Here's the code I currently have: <nav class ...

Tips on personalizing the checkbox by incorporating a custom background image

Looking to spruce up the standard checkbox? We're open to any method - whether it's a background image, CSS3, or some jQuery magic. Check out this example for inspiration! ...

How do I incorporate scrolling into Material-UI Tabs?

I am currently incorporating Material-ui Tablist into my AppBar component. However, I am facing an issue with the responsiveness of the tabs. When there are too many tabs, some of them become hidden on smaller screens. For more information on the componen ...

MVC Template with a Defective BootStrap

Struggling with creating an MVC master template along with sub-pages using Bootstrap. Sadly, I seem to have messed it up and can't quite figure out where the problem lies. You can check out my test website at Do you see how the different sections a ...

how to quietly change the focus of an element using jquery and css?

Whenever I modify the CSS of a scrolled-past element, the view abruptly jumps to that particular div (by scrolling up) upon applying CSS changes using .css(...). Something as simple as adjusting the background-color can trigger this effect. Is there a wor ...

Unable to initialize styles in a newly created Angular CLI project

Just finished setting up a new project with the angular cli. Encountering an issue when trying to link the styles.css file in index.html <link rel="stylesheet" type="text/css" href="styles.css"> Getting the following error in Chrome's dev too ...

Is there a way to access an SD card by clicking on an HTML link using JavaScript?

UPDATE: I am seeking a way to embed an HTML file with JavaScript or jQuery that can directly access the contents of the SD card while being opened in a browser. Currently, I have posted code for accessing it through an activity, but I want to be able to d ...

Can you modify a single attribute for multiple child elements under a parent tag in Visual Studio Code simultaneously?

Imagine a scenario where you are faced with 30-40 options and need to update the name attribute from blank to something more appropriate. Is there a method in visual studio code that allows you to do this in one go? It would be a fantastic feature to have. ...

The Final Div Fluttering in the Midst of a jQuery Hover Effect

Check out my code snippet here: http://jsfiddle.net/ZspZT/ The issue I'm facing is that the fourth div block in my example is flickering quite noticeably, especially when hovering over it. The problem also occurs occasionally with the other divs. Ap ...

File-loader mistakenly generates 2 images and associates them with the incorrect one

I have encountered an issue with file-loader where it is creating two separate images in my build folder. One image is named correctly and saved in the correct path, while the other one is named [hash].png (default) and is stored directly in the build dire ...

The use of e.preventDefault in Ajax/jQuery is stopping the page from refreshing and preventing the form

Currently, I am experimenting with using ajax/jQuery/php/html to submit my form without reloading the page. If I include e.preventDefault(), the page remains static and the form does not get submitted. When I remove e.preventDefault(), the form gets submi ...

Customize your Bootstrap design by selecting a background color that doesn't

Why is the dropdown background black when used in a bg-dark navbar but white when used outside of the navbar? I want to use a white dropdown in a dark navbar. Here is my complete code: MyCode Here is my navbar & dropdown: <nav class="na ...

What is the process for requesting a css file?

My CSS file is pretty lengthy, with over 3000 lines (yes, it's a tad excessive). I'm looking to organize them in alphabetical order so I can easily locate properties. Using Ctrl+F simply tires me out because of the abundance of similar selectors. ...

Understanding the user's preference for text alignment and managing how the text is displayed in a text area (or text field) within a ReactJS application

My latest project is an app that features multiple text areas. Personally, I use the app with two languages - English and another language that is aligned to the right. However, the default setting has the text-areas aligning to the left. To change this ...

Assign a value to the cookie based on the input from the form

I recently asked a similar question, but it seems like I missed providing some context, which is why I couldn't get it to work. My goal is to set a cookie value of a form entry when clicking on it (using the carhartl jquery plugin), but nothing happen ...