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

227
Vistas
How to change height of box together with its width?

I want to publish on my page section with articles in rows if form of boxes. I use bootstrap grid system to make it responsive. I set width of boxes to be 100%. And I want height to be as the same as width. For this I wrote an javascript code to execute this task:

document.addEventListener("DOMContentLoaded", function(event) { 
    let width = document.querySelectorAll(".post-boxes .box");
    width.forEach(item =>{
    item.style.height = `${item.offsetWidth}px`
    });
  });

But the problem is it works only when DOM is loaded for the first time, and if screen size is change, height remain the same.

This is my photo of boxes:

IMG

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

0

One solution is to run the function again when the screen is resized. You can use the resize event for this. Here is an example:

function box(){
    let width = document.querySelectorAll(".post-boxes .box");
    width.forEach(item =>{
    item.style.height = `${item.offsetWidth}px` })
}

document.addEventListener("DOMContentLoaded", () => box() );
window.addEventListener("resize", () => box() );
about 4 years ago · Juan Pablo Isaza Denunciar

0

DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

If you want to recalculate the height then add event handler for onresize

window.addEventListener('resize', <your-handler>);
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