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

222
Vistas
Get window height to decimal point

I would like to get the height of my window. Hence, I use window.innerHeight.

However, this return an integer and I would like a precise value (float). I though to use document.body.getBoundingClientRect().height but this means to add some css to fill entirely the window. Such like that:

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

How can I get the the height of the window to decimal point?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can set the documentElement and body's height to 100% programmatically, get the height, then remove the inline style so as not to make any lasting changes:

function getPreciseHeight() {
  document.documentElement.style.height = "100%";
  document.body.style.height = "100%";

  const height = document.body.getBoundingClientRect().height;

  document.documentElement.style.removeProperty('height');
  document.body.style.removeProperty('height');
  return height;
}

console.log("Normal:", document.body.getBoundingClientRect().height)



console.log("Fixed:", getPreciseHeight())
Hello World!

about 4 years ago · Juan Pablo Isaza Denunciar

0

We can use Window.getComputedStyle() to get precise floating point dimensions of elements after render. It is also considerable faster than using getBoundingClientRect(). I am not sure I understood the reason for setting the height of the body though..

// Precise dimensions of any element 
function getTrueFloatSize(element) {
    x = parseFloat(window.getComputedStyle(element).height)
    y = parseFloat(window.getComputedStyle(element).width)
    return [x, y];
}

console.log(getTrueFloatSize(document.body))
body {
 width: 45.5px;
 height: 267.5px;
}

about 4 years ago · Juan Pablo Isaza Denunciar
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