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

157
Visualizações
why isn't clientHeight 150px?

let height = document.getElementById('d').clientHeight
console.log(height);
img{
height:100%;}
<div id = 'd'>
<img src="https://via.placeholder.com/100x150">
</div>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The image hasn't loaded when you are console logging. You can create an event listener that waits for the image to be loaded and then it will show the proper height.

function loaded() {
    let height = document.getElementById('d').clientHeight;
    console.log(height);
}

const img = document.getElementById('img');
if (img.complete) {
  loaded()
} else {
  img.addEventListener('load', loaded)
}
img{
height:100%;}
<div id = 'd'>
<img id='img' src="https://via.placeholder.com/100x150">
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Because the image hasn't loaded when your code is executing:

setTimeout(() => {
    let height = document.getElementById('d').clientHeight
    console.log(height);
}, 1000)
<div id = 'd'>
<img src="https://via.placeholder.com/100x150">
</div>

If your code depends on knowing the height of the image in order to run correctly, you need to check whether the image is loaded, and wait until it has if it hasn't yet.

about 4 years ago · Juan Pablo Isaza Relatório

0

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded, which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

addEventListener('load',(event)=>{
    let height = document.getElementById('d').clientHeight
    console.log(height);
});
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