HTML if image is not found

Html

Html Problem Overview


I have an image in a HTML page:

<img src="smiley.gif" alt="Smiley face" width="32" height="32" />

If the image is not found on the server it shows an ugly blank square.

I want to make it so that if an image is not found it will display nothing or some other default image that I know is definitely on the server.

How can this be done?

Html Solutions


Solution 1 - Html

The best way to solve your problem:

<img id="currentPhoto" src="SomeImage.jpg" onerror="this.onerror=null; this.src='Default.jpg'" alt="" width="100" height="120">

onerror is a good thing for you :)

Just change the image file name and try yourself.

Solution 2 - Html

Well you can try this.

  <object data="URL_to_preferred_image.png" type="image/png">
   <img src="URL_to_fallback_image.png" />
  </object>

this worked for me. let me know about you.

Solution 3 - Html

Ok but I like to use this way, so that whenever original image is not available you can load your default image that may be your favorite smiley or image saying Sorry ! Not Available, But in case if both the images are missing you can use text to display. where you can also you smiley then. have a look almost covers every case.

<img src="path_to_original_img/img.png"  alt="Sorry! Image not available at this time" 
       onError="this.onerror=null;this.src='<?=base_url()?>assets1/img/default.jpg';">

Solution 4 - Html

For the alternative, if the image doesn't exist - show nothing at all. (what I was looking for)

You can swap the function from Robby Shaw's answer in the "onerror" attribute to "this.remove()".

<img id="currentPhoto" src="SomeImage.jpg" alt='1' width="100" height="120">
<img id="currentPhoto" src="SomeImage.jpg" onerror="this.onerror=null; this.remove();" alt="2" width="100" height="120">

Solution 5 - Html

You can show an alternative text by adding alt:

<img src="my_img.png" alt="alternative text" border="0" /> 

Solution 6 - Html

The usual way to handle this scenario is by setting the alt tag to something meaningful.

If you want a default image instead, then I suggest using a server-side technology to serve up your images, called using a similar format to:

<img src="ImageHandler.aspx?Img=Blue.jpg" alt="I am a picture" />

In the ImageHandler.aspx code, catch any file-not-found errors and serve up your default.jpg instead.

Solution 7 - Html

I added a parent div around the image and used the following onerror event handler to hide the original image because in IE there was still an ugly image-not-found image shown after using the alt attribute:

<div style=" width:300px; height:150px; float:left; margin-left:5px; margin-top:50px;">
        <img src='@Url.Content("~/Images/Logo-Left.png")' onerror="this.parentElement.innerHTML = '';" />
</div>

Solution 8 - Html

Here Check below code snippet which, In this, I miss-spelled the image name. So, just because of that it is showing the alternative image as not found image ( 404.svg ).

<img id="currentPhoto" src="https://www.ccrharrow.org/Images/content/953/741209.pg" onerror="this.src='https://www.unesale.com/ProductImages/Large/notfound.png'" alt="">

Solution 9 - Html

Try using border=0 in the img tag to make the ugly square go away.

<img src="someimage.png" border="0" alt="some alternate text" />

Solution 10 - Html

I wanted to hide the space occupied by <img> tag so this worked for me

<img src = "source.jpg" alt = "" >

Solution 11 - Html

If you want an alternative image instead of a text, you can as well use php:

$file="smiley.gif";
$alt_file="alt.gif";
if(file_exist($file)){
     echo "<img src='".$file."' border="0" />";
}else if($alt_file){
     // the alternative file too might not exist not exist
     echo "<img src='".$alt_file."' border="0" />";
}else{
  echo "smily face";
}

Solution 12 - Html

simple way to handle this, just add an background image.

Solution 13 - Html

its works for me that if you dont want to use alt attribute if image break then you can use this piece of code and set accordingly.

<h1>
  <a> 
   <object data="~/img/Logo.jpg" type="image/png">
     Your Custom Text Here
   </object>
  </a>
</h1>

Solution 14 - Html

This one worked for me. using the srcset. I have just learnt about it so i dont know if browsers support it but it has worked for me. Try it and later give me your feed back.

 <img src="smiley.gif" srcset="alternatve.gif" width="32" height="32" />

Solution 15 - Html

try PHP

if (file_exists('url/img/' . $Image)) {
    $show_img_URL = "Image.jpg";
} else {
    $show_img_URL = "Image_not_found.jpg";
}

Solution 16 - Html

Solution - I removed the height and width elements of the img and then the alt text worked.

<img src="smiley.gif" alt="Smiley face" width="32" height="32" />

TO

<img src="smiley.gif" alt="Smiley face" />

Thank you all.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid19801View Question on Stackoverflow
Solution 1 - HtmlRobby ShawView Answer on Stackoverflow
Solution 2 - HtmlSudarshan KalebereView Answer on Stackoverflow
Solution 3 - HtmlSudarshan KalebereView Answer on Stackoverflow
Solution 4 - HtmlBimView Answer on Stackoverflow
Solution 5 - HtmljuankysmithView Answer on Stackoverflow
Solution 6 - HtmlWidorView Answer on Stackoverflow
Solution 7 - HtmlPatrick KoorevaarView Answer on Stackoverflow
Solution 8 - Htmlkrishna amarghadeView Answer on Stackoverflow
Solution 9 - HtmlAziz ShaikhView Answer on Stackoverflow
Solution 10 - HtmlDivyanshu JimmyView Answer on Stackoverflow
Solution 11 - Htmlstephen komuView Answer on Stackoverflow
Solution 12 - Htmlvikas dhereView Answer on Stackoverflow
Solution 13 - HtmlNasir ShafiqueView Answer on Stackoverflow
Solution 14 - Htmlstephen komuView Answer on Stackoverflow
Solution 15 - HtmlSIAMWEBSITEView Answer on Stackoverflow
Solution 16 - HtmlDavid19801View Answer on Stackoverflow