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

168
Visualizações
Getting wrong width value [2px extra] from getBoundingClientRect()

This is being weird. I am using getBoundingClientRect() to get the values to position my blue coloured div so that it can surround an element on mouseover.

It works great and is getting all the values correctly. See this for reference:

enter image description here

As you can see, the blue div here surrounded the password element on hover as it should! (I have used Bootstrap demo form from w3schools for this.)

However, the problem arises when I create a form page myself without using Bootstrap or any other framework.

It gets the top and left correctly, but width and height 2px more! See the extra spacing here:

enter image description here

I don't know what is causing this to output those 2px extra. Why it works perfectly with Bootstrap or W3.CSS like frameworks but spit out 2px more without any frameworks? What's the workaround for this?

Here's the code snippet:

document.addEventListener("mouseover", (e) => {
  elem = e.target;
  var rect = elem.getBoundingClientRect();

  var x = rect.left;
  var y = rect.top;
  var w = rect.width;
  var h = rect.height;

  div.style.width = w + "px";
  div.style.height = h + "px";
  div.style.left = x + window.scrollX + "px";
  div.style.top = y + window.scrollY + "px";
});
about 4 years ago · Juan Pablo Isaza
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