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

231
Visualizações
Center Child to Parent When Child is Absolute Positioned

Is there a way to position an absolute positioned element centered to its' parent who is relative positioned?

I was thinking if somehow I can calculate the width of the parent, and based on that, center the child? But not sure where to start whether with JavaScript or css.

Here's the codepen for reference

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

.tooltip {
  /* 
    position the top-left corner of the element 
    in the center of the parent 
  */
  position: absolute;
  top: 50%;
  left: 50%;
  /*
    move the (positioned) element up and left 
    by half its own width/height
  */
  transform: translate(-50%, -50%);
}


.container,
.tooltip{
  border: 2px solid black;
  padding: 10px;
}

.container {
  position: relative;
  width: 80vw;
  height: 50vh;
}
<div class="container">
  <div class="tooltip">Tooltip</div>
</div>

top and left percentages are percentages of that dimension on the of the parentElement.

Whereas the percentages in translate are relative to the element itself.

about 4 years ago · Juan Pablo Isaza Relatório

0

This should center your tooltip

const tooltip = document.getElementById('tooltip');
const parentWidth = tooltip.parentElement.offsetWidth;

tooltip.style.left = (parentWidth - tooltip.offsetWidth) / 2  + 'px';
about 4 years ago · Juan Pablo Isaza Relatório

0

Edit: I think Thomas Answer contains the better way

Well it depends on your code if you want to do this css only, Here are several cases I came up with after doing my research:

1. If your div has a set size (width & height), According to this page you can take this steps:

  • Add left: 50% to the element that you want to center. You will notice that this aligns the left edge of the child element with the 50% line of the parent.
  • Add a negative left margin that is equal to half the width of the element. This moves us back onto the halfway mark.
  • Next, we’ll do a similar process for the vertical axis. Add top: 50% to the child
  • And then add a negative top margin equal to half its height.

In your case it looks like this:

#container {
  border: 1px solid gray;
}

#wrapper {
  display: inline-block;
  position: relative;
  height: fit-content;
  border: 1px solid red;
  padding: 8px;
}

#tooltip {
  width: 100px; /* could be set by % aswell */
  height: 10px;
  border:1px solid green;
  position:absolute;
  left:50%;
  top:50%;
  margin-left: -50px;
  margin-top: -5px;
  bottom: -24px;
}
<div id="wrapper">
  Trigger Content, Trigger Content, Trigger Content,Trigger Content,
  <div id="tooltip">Center</div>
</div>

2. If it doesn't contain a set value but you want it to be centered anyway: Not sure if it's a good way but you can use an absolute element inside wrapper before any other content, covering its parent and having its display value as flex and containing justify-content: center as well as align-items: center; e.g. on your code:

#container {
  border: 1px solid gray;
}

.full-flex{
  width: 100%;
  display: flex;
  position: absolute;
  justify-content: center;
  align-items: center;
}

#wrapper {
  display: inline-block;
  position: relative;
  height: fit-content;
  border: 1px solid red;
  /* padding: 8px; */  /* I removed the padding so you can get a better understanding of this */
}

#tooltip {
  border:1px solid green;
}
<div id="wrapper">
    <div class="full-flex">
      <div id="tooltip">Wanna be centered</div>
    </div>
  Trigger Content, Trigger Content, Trigger Content,Trigger Content,
  
</div>

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