6

I have some images on my page.

By default if a given image is not available, then the broken image indicator is shown on Chrome and IE.

I want nothing to be shown but the alternative text in this case. Is there any way to handle it using CSS.

Besi
  • 22,579
  • 24
  • 131
  • 223
shi69
  • 93
  • 1
  • 2
  • 7
  • http://stackoverflow.com/questions/3235913/how-to-silently-hide-image-not-found-icon-when-src-source-image-is-not-found – Xareyo Aug 28 '13 at 10:08

4 Answers4

13

using javascript

<img src="broken.png" onerror="this.style.display='none'"/>

edit: added small snipet that will handle all images.

$("img").error(function(){$(this).hide();});

example: http://jsfiddle.net/Va2Wd/

Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26
  • 3
    This is ok for an example, but if the OP is doing this for a lot of images, the JS should _not_ be inline – Bojangles Aug 28 '13 at 10:02
4

Try setting alt=" " as an empty string, If the image isn't found, there will just be an empty space.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
Vladimir
  • 377
  • 1
  • 6
  • 2
    Chrome doesn't hide icons of broken images though, where OP wants to hide those icons – Mr. Alien Aug 28 '13 at 10:09
  • 1
    Thank you for your reply. On IE also broken image appears and i want it to do it using CSS not JS, if any option is available please help. – shi69 Aug 29 '13 at 06:02
1

You can use the onerror event in JavaScript to act when an image fails to load:

var img = document.getElementById("myImg");
img.onerror = function () { 
    this.style.display = "none";
}

or use

var images=document.getElementsByTagName("img");
for(i=0;img[i]!=null;i++)
{
img[i].style.display = "none";
}
Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
1

Instead of using JavaScript or CSS, you can use the object tag. It's cleaner and easier code. Add alternative text between the tags like this:

<object data="img/failedToLoad.png" type="image/png">Alternative Text</object>

http://www.w3schools.com/tags/tag_object.asp

ThomasAFink
  • 1,257
  • 14
  • 25