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


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?


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:

Sem error

<img src="" alt="Olá Mundo" onerror="this.onerror=null;this.src='';"

<br>Com error

<img src="loading.gif" alt="Olá Mundo" onerror="this.src='';" >

With CSS (requires fixed size):

.image {
     width: 620px;
     height: 480x;

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