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

231
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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