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

110
Vistas
window.getCompuedStyle doesn't work as expected?

I write following function to return the width and height of a text:

function size_calculation(word,fontSize) {
  const div = document.body.appendChild(document.createElement('div'));
  div.textContent = word;
  div.style.cssText = `
  font-size:${fontSize}px;
  width:auto;
  position:absolute;
  visibility:hidden;
  `
  const computed_styles = window.getComputedStyle(div);
//return the expected height
console.log(computed_styles.getPropertyValue('height'))
 
  div.remove();
 
//return nothing 
console.log(computed_styles,computed_styles.getPropertyValue('height'))
 
  return ({
    height: computed_styles["height"],
    width:computed_styles["width"]
  })
}
console.log(size_calculation("hi",15))

I am quite confused why after I remove the div, all styles' property in the computed_styles will become "" nothing, but before I remove the div, all styles' property in the computed_styles has something different than "".

In my understanding, the value for variable will not be changed and is static unless we write any update or reassign statement once declared (AM I Wrong?)

Why the computed_styles will be automatically update?

Thank you!

P.S: not asking for solution (solution quite easy), but curious why.

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

0

See MDN

Return value: A live CSSStyleDeclaration object, which updates automatically when the element's styles are changed.

Note the word live.

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