Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
How to center div below other element, not parent div?

Here's an image showing what I'm trying to achieve:

enter image description here

As you can see, I'm trying to get the profile options dropdown to be centered below the profile button. The current way I have it set up is to have the dropdown menu have position: absolute, and then use a margin-left: -3% to actually "center" it, but I feel like there should be a more reliable way to actually center it that isn't dependent on a hardcoded percentage value. Below is the relevant code:

index.php:

<div id="navTools">
    <button id="navProfile"><img src="./imgs/profile.svg" alt="Profile"/></button>
    <div id="profileDropdown">
        <a href="#example">Account Information</a>
        <a href="#example">Orders & Returns</a>
        <a href="./logout.php">Logout</a>
    </div>
    <a id="navCart" href="cart.php"><img src="./imgs/cart.svg" alt="Cart"/></a>
</div>

styles.css

#profileDropdown {
  display: none;
  background: #1d1d1d;
  position: fixed;
  margin-left: -3%;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Wrap profile button and profile dropdown in a div. Give that div position: relative and set position: absolute for the dropdown. If you want it to appear at the bottom, set bottom: 0. If you want to center it vertically, set left: 50%, transform: translateX(-50%).

<div id="navTools">
<div class="dropdown-wrapper">
    <button id="navProfile"><img src="./imgs/profile.svg" alt="Profile"/></button>
    <div id="profileDropdown">
        <a href="#example">Account Information</a>
        <a href="#example">Orders & Returns</a>
        <a href="./logout.php">Logout</a>
    </div>
</div>
<a id="navCart" href="cart.php"><img src="./imgs/cart.svg" alt="Cart"/></a>
.dropdown-wrapper {
    position: relative;
}

#profileDropdown {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda