Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda