html – How to load default image if src="" doesn't find it?

Question:

Is there any way to load a standard image, using just HTML , if the first src="" doesn't find the file?

<img src="ola_mundo.png" alt="Olá Mundo"> // Existe algum atributo para isso?

Answer:

Other simpler fallbacks you can try, with javascript:

<img src="imagem-nao-existe.png" onerror="this.src='fallback.png'">

An example of jsfiddle by @Randrade: https://jsfiddle.net/wts9e4xb/1/

Sem error
<br>

<img src="http://previews.123rf.com/images/otnaydur/otnaydur1103/otnaydur110300040/9112843-Gold-coins-stack-over-scater-coins-Stock-Photo-bar.jpg" alt="Olá Mundo" onerror="this.onerror=null;this.src='http://logz.io/wp-content/uploads/2016/02/stack-overflow-logo.png';"
>

<br>Com error
<br>

<img src="loading.gif" alt="Olá Mundo" onerror="this.src='http://logz.io/wp-content/uploads/2016/02/stack-overflow-logo.png';" >

With CSS (requires fixed size):

<style>
.image {
     width: 620px;
     height: 480x;
}
</style>

<div class="image" style="background: url(foto-nao-existe.png), url(fallback.png)"></div>
Scroll to Top