Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

415
Views
How to prevent a centered aligned flex item from going up as its height increases?

I'm trying to build a dropdown menu, but its div (which is position static) keeps going up as I add new items to the menu.

It's the align-items:center rule that keeps repositioning the item so it's properly vertically aligned.

If I discard this rule, it'll break the navbar, the other items won't be centered as I intended.

If I use the display property, as the code bellow shows, the button starts where I want but whenever I click on it, it goes out of its place.

const dropdown = () => {
    let display = document.getElementById("menu").style.display;
    
    document.getElementById("menu").style.display = (display === "none") ? "block":"none";
    //toggling "visibility" takes the div out of its place
 }
.nav {
    display: flex;
    justify-content: center;
    align-items: center;
    height:160px;
    border:1px solid black;
    text-align: center;    
}

.dropdownContainer {
    margin-left: auto;
}

#drop {
  display:none;
  /* visibility:hidden;*/
}
<nav class="nav">
    <div>Irrelevant elements to this question</div>
    <div class="dropdownContainer">
        <button onclick="dropdown()" class="dropdownBtn">Ronaldinho</button> 
        <div id="menu">
            <!-- the more items added here, the more up the page the whole container goes -->
            <div>One</div>
            <div>Two</div>
            <div>Three</div>
            <div>Four</div>
            <div>Five</div>
            <div>Six</div>
        </div>
    </div>
</nav>

If I change the JS code so it toggles the visibility property instead, the button already starts out of its intended position but it also doesn't change place when its clicked on.

How can I keep the button centered as it is and make the menu drop down without changing the button's place?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I Think that's what you want to achieve:

I positioned your menu underneath the button with position absolute. so the button won't change its position.

const dropdown = () => {
    let display = document.getElementById("menu").style.display;
    
    document.getElementById("menu").style.display = (display === "none") ? "block":"none";
    //toggling "visibility" takes the div out of its place
 }
.nav {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 160px;
  border: 1px solid black;
  text-align: center;
}

.dropdownContainer {
  margin-left: auto;
  position: relative;
}

#drop {
  display: none;
  /* visibility:hidden;*/
}
#menu {
  position: absolute;
  top: 100%;
}
<nav class="nav">
    <div>Irrelevant elements to this question</div>
    <div class="dropdownContainer">
        <button onclick="dropdown()" class="dropdownBtn">Ronaldinho</button> 
        <div id="menu">
            <!-- the more items added here, the more up the page the whole container goes -->
            <div>One</div>
            <div>Two</div>
            <div>Three</div>
            <div>Four</div>
            <div>Five</div>
            <div>Six</div>
        </div>
    </div>
</nav>

about 4 years ago · Juan Pablo Isaza Report

0

const dropdown = () => {
    let display = document.getElementById("menu").style.display;
    
    document.getElementById("menu").style.display = (display === "none") ? "block":"none";
    //toggling "visibility" takes the div out of its place
 }
.nav {
    display: flex;
    justify-content: center;
    align-items: center;
    height:160px;
    border:1px solid black;
    text-align: center;    
}

.dropdownContainer {
    margin-left: auto;
}

#drop {
  display:none;
  /* visibility:hidden;*/
}
#menu{
 display: block;
 position: absolute;
 height: 60px;
 overflow-y: auto;
 width: 80px;
}
<nav class="nav">
    <div>Irrelevant elements to this question</div>
    <div class="dropdownContainer">
        <button onclick="dropdown()" class="dropdownBtn">Ronaldinho</button> 
        <div id="menu">
            <!-- the more items added here, the more up the page the whole container goes -->
            <div>One</div>
            <div>Two</div>
            <div>Three</div>
            <div>Four</div>
            <div>Five</div>
            <div>Six</div>
        </div>
    </div>
</nav>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!