Aligning a collapsible feature while ensuring that the content descends

My dilemma involves a centered menu with a dropdown feature. The issue arises from the absolute positioning, causing the entire menu to shift upward when the dropdown is activated due to the increased height.

I am struggling to find an effective solution for this. Ideally, I want the menu to remain perfectly centered even when a dropdown is opened, without any movement in its position.

I am exploring CSS-only methods to address this problem. If no suitable solution is found, I may resort to using JavaScript to adjust the menu position upon loading.

var dropdown = document.getElementById("dropdown");

var show = false;

function showDropdown() {
  var dropdownList = document.getElementById("dropdownList");
  if (show) {
    dropdownList.classList.remove("show");
    show = false;
  } else {
    dropdownList.classList.add("show");
    show = true;
  }
}

dropdown.addEventListener("click", showDropdown);
.parent {
  height: 100vh;
  width: 100vw;
  background-color: #aaa;
}

.list {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.dropdown > ul {
  display: none;
}

.dropdown > ul.show {
  display: block;
}
<div class="parent">
  <ul class="list">
    <li class="dropdown"><a href="#" id="dropdown">Item One</a>
      <ul id="dropdownList">
        <li>Dropdown 1</li>
        <li>Dropdown 2</li>
      </ul>
    </li>
    <li>Item Two</li>
    <li>Item Three</li>
  </ul>
</div>

Answer №1

It seems that the issue lies in how your content is vertically aligned using the translate and top properties. I have made adjustments by changing this to use margin-top, but please note that it may not be a perfect solution.

Your previous method of automatic vertical centering will unfortunately cause your content to move upward when it expands, as that is the nature of vertical alignment.

In order to achieve the desired alignment, you may need to manually determine the positioning on the page.

var dropdown = document.getElementById("dropdown");

var show = false;

function showDropdown() {
  var dropdownList = document.getElementById("dropdownList");
  if (show) {
    dropdownList.classList.remove("show");
    show = false;
  } else {
    dropdownList.classList.add("show");
    show = true;
  }
}

dropdown.addEventListener("click", showDropdown);
.parent {
  height: 100vh;
  width: 100vw;
  background-color: #aaa;
}

.list {
  position: absolute;
  margin-top: 40vh;
}

.dropdown > ul {
  display: none;
}

.dropdown > ul.show {
  display: block;
}
<div class="parent">
  <ul class="list">
    <li class="dropdown"><a href="#" id="dropdown">Item One</a>
      <ul id="dropdownList">
        <li>Dropdown 1</li>
        <li>Dropdown 2</li>
      </ul>
    </li>
    <li>Item Two</li>
    <li>Item Three</li>
  </ul>
</div>https://stackoverflow.com/posts/66536030/edit#

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

Changing the size of a div using jQuery

I'm attempting to adjust the size of a div element. You can view my progress in this code fiddle: https://jsfiddle.net/bj3m24ks/31/ Kindly refrain from sharing links to other tutorials as I have already explored them all. I attempted to utilize the $ ...

When arriving on a page via an HTML anchor tag, the CSS style does not appear. How can I "reinstate" it?

I have set up a website with an index.html page that links to a contact.html page using anchor tags. On the contact.html page, there are anchor tags that should navigate the user back to specific sections of the index.html page. The navigation works fine, ...

What steps can I take to properly position my child element within its parent container?

Is there a way to adjust the width of a child element so it fits snugly inside its parent div? The width should be contained within the padding area. The top and left placement is correct, but the right side needs adjustment. Check out this link for refe ...

Using Jquery to toggle classes between "display block" and "display none

Hello friends, I'm in need of some help with my code. $(document).ready(function() { var circle = $('.circle'); $(".send a").click(function(e) { e.preventDefault(); $('.wrap').css('display', 'bl ...

I encountered an issue while attempting to link my project to a database. An error popped up stating that the name 'textbox' does not exist in the current context

I encountered an error when trying to connect my project to a live database. The error message stated: the name ‘textbox’ does not exist in the current context. How can I resolve this issue? Below is the aspx.cs code that I used for the connection, wh ...

How can you create a jQuery fade in effect for a single <li> element

I'm in the process of developing a task management app that generates a new li element every time a user adds an item. However, I am facing an issue where fadeIn() is activating for every li on the page whenever a new item is added. Does anyone have s ...

The tab content refuses to show up in its designated fixed location

I've been working on creating a responsive tab system that functions as an accordion on smaller mobile devices, inspired by this example. I've made great progress and I'm almost where I want to be, but for some reason, the active tab content ...

What are the best techniques for positioning text next to images in HTML and CSS?

I have attempted to position the text in close proximity to the images as shown in the picture below (to allow for spacing in the middle). As depicted in the image, the "AVENUE FOR GROWTH" and "NETWORKING OPPORTUNITIES" texts are near Man's hand and w ...

The `margin:auto;` property does not function properly with inline-block elements

I recently adjusted my "container" div by changing it to an inline-block. Previously, I had applied margin:auto; to center it, which worked well with a specified width. Original Code (functional) #container { border: 1px solid black; height: 2 ...

Help needed with CSS menu dropdown problem

Hello everyone, I am currently working on creating a menu for the top navigation of my website. My goal is to have a list of items appear below the menu when hovering over it with the mouse. Right now, I am testing this on a local HTML file before impleme ...

Having trouble with CSS messing up your gridview button display?

Oddly enough, whenever I place buttons inside a gridview, they end up looking very small (as shown in the image below), even though the gridview itself has no CSS applied to it. Is it possible that some other CSS is affecting the buttons? I have too much ...

What new skills should I acquire following my mastery of HTML and CSS?

I have a vision of creating a website similar to funcage.com, a site filled with funny pictures where users can register, upload their own content, vote, and leave comments. After dedicating myself to learning HTML & CSS for over a year and completing the ...

A guide to incorporating external CSS files in Angular 5 components using styleUrls

I have a unique setup where my Angular 5 application resides in a subdirectory within another parent application. The parent application consists of JSP and other AngularJS applications among other things. My goal is to include a CSS file from the parent ...

Stop the image transformation/transition when the back face is not visible

Experience the magic of a box that flips with just a click. Inside, an image awaits to zoom in upon hovering. Check out this sample. The only glitch is that the zoom transition on the hidden image appears for a brief moment when I move my mouse out or ba ...

Using ASP .NET controls in conjunction with Bootstrap

Is there a way to easily apply bootstrap classes to all asp .net controls in my application at once? Here is an example of how I formatted my menu control using bootstrap. I am looking for a solution where I can apply the bootstrap theme commonly to all m ...

What could be causing ReactNative Dimensions component to display lower resolutions?

Currently, I am developing an application using React Native v0.45.1 and testing it on my S6 device. Despite setting a background image that matches the resolution of my phone, it appears excessively large on the screen, cutting off most of the content. T ...

The specified font is not loading despite being correctly defined

I have a font stored locally in resources/fonts/ and I'm correctly linking to it, but for some reason, it's not loading. Here is the <style> section in my html: <style> @font-face{ font-family:"franklin_gothic_condensed"; ...

No need for jQuery with this scrolling image gallery

I recently created a horizontal scrolling image gallery with thumbnails underneath. The goal is to click on a thumbnail and have the corresponding image scroll into view. Here's the HTML setup: <div class="images_container"> <img id="imag ...

Issue with the MUI Autocomplete display in my form

Recently, I started using Material UI and Tailwind CSS for my project. However, I encountered an issue with the Autocomplete component where the input overlaps with the following DatePicker when there is multiple data in the Autocomplete. I have attached a ...

Issue arises when applying both overflow-x:scroll and justify-content:center

Encountering a problem with using overflow-x: scroll and justify-content: center on a flex parent container. Here is my code snippet: The issue: the first flex child item is not visible as it is cropped on the left side. Please refer to the screenshot and ...