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

157
Visualizações
Center an absolute element below a div?

I'm trying to center a pargraph below a div (could be a square image for the matter). I understand that the easiest way to do it is to contain both the div and the text below it in a single container and use text-align, but in this instance I have a limitation where I cannot let the small image be contained in a container wider than that image.

Without centering the text it looks like this:

enter image description here

My code:

HTML:

<div class="block"></div>
<p class="label">This text is a bit long</p>

CSS:

body {
  padding: 5rem;
}
.block {
  background-color: blue;
  width: 80px;
  height: 80px
}

.label {
  
}

Codepen: https://codepen.io/omerh3/pen/oNqVjvV

The reason why I cannot let the image be in a container is that I'm using ReactFlow where the handles should connect to the sides of the image without a gap. If I put the image and the text inside a div, the div will take the width of the text and thus it will naturally be wider than the image.

I tried centering the text below the image with absolute positioning, but with different paragraphs sizes, it won't be persistent in the center. Is there a away to achieve this without inserting the image/square and the text inside one div?

One last thing: the width of the image if constant, for example 100px

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

0

Do you mean something like this??

body {
  padding: 5rem;
}
.block {
  background-color: blue;
  width: 80px;
  height: 80px;
  margin:0 auto;
  
}

p.label {text-align: center}
<div class="block"></div>
<p class="label">This text is a bit long</p>

about 4 years ago · Juan Pablo Isaza Relatório

0

one way to do this is to get the coordinates of your two elements and then add margin-left: to adjust the position of the span

let divOffsets = document.getElementById('a').getBoundingClientRect();
let divRight = divOffsets.right;
let divLeft = divOffsets.left;
console.log(divLeft,divRight)

let spanOffsets = document.getElementById('b').getBoundingClientRect();
let spanRight = spanOffsets.right;
let spanLeft = spanOffsets.left;
console.log(spanLeft,spanRight)

let divCenter = divLeft + divRight / 2
console.log(divCenter)

let offset = divCenter - (spanLeft + spanRight / 2) 
offset = offset + "px"

document.getElementById('b').style.marginLeft = offset;
body {
  padding: 5rem;
  border:solid 1px red;
  position:relative;
}
.block {
  background-color: blue;
  width: 80px;
  height: 80px;
  
}

span{
position:absolute;
}
<div id = 'a'class="block"></div>
<span id = 'b' >1234</span>

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