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

What is the fastest way to emulate CSS3 box radius for IE in terms of rendering speed?

There are numerous options available in JavaScript, .htc, and jQuery to create rounded corners for IE 6,7,8. (Not compatible with IE 8) http://code.google.com/p/jquerycurvycorners/ http://code.google.com/p/curvycorners/ Which solution offers the faste ...

Utilize custom scrollbar design exclusively for Windows operating system

My goal is to customize the scrollbar style of a div specifically for Windows systems. While most mobile devices have invisible scrollbars that I want to keep hidden, the aesthetics of Mac OS scrollbars are acceptable to me. What I really need is to make t ...

HTML5 footer element is the perfect way to add a

I'm currently working on creating a footer that stretches across the entire width of the screen. Originally, I had it nested within the body tag which was centered with a width of 825px. To remove this width constraint, I moved the footer under the ht ...

To avoid truncation issues, consider inserting a plus sign following the ellipsis in the text-overflow property

Hey there! I have a table that contains some text and I'm looking to shorten the text after it reaches a certain length. I was able to achieve this using the text-overflow property. .overflow { width: 70px; overflow: hidden; text-overflow: elli ...

Text within cells is not wrapping onto a new line in an HTML table

Let me show you how it currently looks: https://i.sstatic.net/OeHRg.png The maximum width of the cell is working properly, but the text is not wrapping to a new line as expected. Instead, it overflows out of the cell. Here is the CSS applied to the tabl ...

What is the best way to align content in the center when its parent element has a position property

JSBIN Is it possible to center the number row without using JavaScript to calculate its position, considering its parent has a fixed attribute? I've tried using CSS properties like margin and text-align with no success. Any suggestions on achieving t ...

Image loading in NextJS is limited to only Firefox browsers

Images are loading only in Firefox and not anywhere else. Here is the import statement: import gradient from '../public/mountains/gradient.svg' And here is the Image component: <div id='gradient' className={`${styles.bgimage} ${sty ...

Having difficulty configuring Compass in WebStorm

Trying to integrate Compass into an existing WebStorm project has been challenging for me as the CSS files are not linking properly. Despite my efforts to modify the relative links, I have not yet managed to resolve the issue. The folders and config.rb we ...

"Struggling with vertical alignment in my scenario, any tips on how to achieve

I'm trying to vertically align my texts while ensuring that the green background div covers the entire height of the red color div. Right now, the green div only covers 90% of the red div and I'm not sure why. Can someone please explain what&apos ...

The div elements are constantly shifting positions when viewed in Internet Explorer

After completing my website and testing it across different browsers, I noticed an issue with IE. The contact div and navigation shift around the page in this browser specifically. Can anyone help me out with some code to fix this problem? Link to live ex ...

What is the best way for a background-image to determine its height in CSS?

I am currently working on a website project and I would like to include my title using a background-image because it uses a unique font. In my design class, we were taught that this is the best approach. However, I am struggling with setting the correct di ...

CSS: Dynamically adjusting height based on parent element's current height

I'm seeking clarification on the topic at hand. Why am I unable to simply use the following code: <div style="min-height: 100vh"> <div style="height: 100%"> </div> </div> When I implement this code, ...

What causes the unset width or height to impact object-fit, and is there a workaround for this issue?

Query Beginnings Encountering unexpected behavior while using the value unset with the rule object-fit led me to question if there might be a rationale or workaround for this peculiar issue. Demonstration .block { &__img { width: 100%; hei ...

CSS guidelines for handling single line breaks with the white-space property set to pre-wrap

Is it considered acceptable to include solitary carriage return characters in an HTML block styled with white-space: pre-wrap? I am working with an application that receives SOAP messages and logs them to a file. The logging process involves removing new- ...

Robotic Arm in Motion

GOAL: The aim of the code below is to create a robotic arm that consists of three layers (upper, lower, and middle), all connected to the base. There are four sliders provided to independently move each part except for the base which moves the entire arm. ...

unwelcome tab spacing in CSS

I am facing an issue with unwanted spacing in my lists. I have a code that generates three-column lists, each containing about eight rows. However, the first list item of the last row is causing an unwanted space. It seems to shift entirely to the next col ...

Issue with background image repeating incorrectly when resizing

I've recently added an image as the background for my website using the following style: #whole_wrapper{ background-image: url(../images/wrapper_bg.jpg); background-repeat: repeat-x; width: 100%; float: left; height: 116px; } ...

Is it possible to adjust the width of a parent element based on the number of child

In this particular example, I have structured a header with a logo-container positioned on the left side, a menu in the center, and a button on the right. The menu consists of 5 top-level items and 2 sub-menus. <div class="container"> <div cla ...

Using Vue to alter data through mutations

Greetings! I am currently in the process of developing a website for storing recipes, but as this is my first project, I am facing a challenge with modifying user input data. My goal is to create a system where each new recipe added by a user generates a u ...

Deactivate button using Javascript

Can anyone assist me with this issue I am having? I currently have a button set up as follows: <input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/> I need to disable the button using jQuery, standard javascript ...