73

The image with invalid source displays an alternate text in Firefox but not in chrome unless the width of an image is adjusted.

  <img height="90" width="90"
    src="http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"
    alt="Image Not Found"/>

How to display the alt text for an image?

5
  • Which version of chrome are you using? Because, according to code.google.com/p/chromium/issues/detail?id=16734 this should have been fixed. Commented Mar 22, 2011 at 3:58
  • This still seems to be a problem in Chrome 29 (2+ years on)! Unless the width of the element is wide enough to show the alt text in its entirety, on one line, then the alt text does not show!?
    – MrWhite
    Commented Sep 2, 2013 at 10:20
  • ...and the element needs to have some height. At least 44px in my tests.
    – MrWhite
    Commented Sep 2, 2013 at 11:00
  • 17
    You shouldn't use the alt tag to say "image not found" because it is used by screen readers for blind and partially-sighted people. See this for tips.
    – Hugo
    Commented Jan 16, 2014 at 9:23
  • I also got the same issue in Firefox 45.8.0 - and title="hover text here" solves it nicely.
    – Rose
    Commented Apr 25, 2017 at 18:33

11 Answers 11

90

If I'm correct, this is a bug in webkit (according to this). I'm not sure if there is much you can do, sorry for the weak answer.

There is, however, a work around which you can use. If you add the title attribute to your image (e.g. title="Image Not Found") it'll work.

2
  • @Prisoner, <img src="pic.png" />, not <img href="" />. Width and height make no difference. Also, src shouldn't be empty or omitted, in practice, it never is (and that's the only reason why your example worked). Commented Jun 5, 2014 at 5:07
  • The image height and width shouldn't make a difference. (But some browsers are weird.) A better example than the one in the comment above: jsfiddle.net/o8syw0bp Commented Jan 23, 2019 at 15:57
24

You can use the title attribute.

<img height="90" width="90"
src="http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"
alt="Google Images" title="Google Images" />
5

Yes it's an issue in webkit and also reported in chromium: http://code.google.com/p/chromium/issues/detail?id=773 It's there since 2008... and still not fixed!!

I'm using a piece of javacsript and jQuery to make my way around this.

function showAlt(){$(this).replaceWith(this.alt)};
function addShowAlt(selector){$(selector).error(showAlt).attr("src", $(selector).src)};
addShowAlt("img");

If you only want one some images:

addShowAlt("#myImgID");
3
  • I don't know why I'm still having this problem in Chrome, you'd think that this would be a major thing to fix, but your code worked (once wrapped in a domready block. So thanks.
    – Kzqai
    Commented Oct 17, 2012 at 16:47
  • can you provide your solution in plunker or jfiddle please. Commented Jan 16, 2015 at 15:41
  • This bug appears to be still existing in 2016! Can your javascript solution be "turned" into a Chrome extension or a boomarklet for instant-activation?
    – jumpjack
    Commented Feb 24, 2016 at 8:45
4

Use title attribute instead of alt

<img
  height="90"
  width="90"
  src="http://www.google.com/intl/en_ALL/images/logos/images_logo_lg12.gif"
  title="Image Not Found"
/>
4
  • please explain it why Use title attribute instead of alt
    – sandeep
    Commented Mar 22, 2011 at 4:10
  • 2
    we have to use both title and alt for browser compatibility as different browsers consider different attribute in this case title/alt
    – Harsh Baid
    Commented Mar 22, 2011 at 4:30
  • @Harsh yeah u r right.actually i was testing on Google chrome so i post code according to that.but we have to use both for multiple browser.
    – Sukhjeevan
    Commented Mar 22, 2011 at 4:38
  • I also go through an article.I think it would be best to understand the difference between alt and title attribute.DIFFERENCE BETWEEN ALT AND TITLE ATTRIBUTE
    – Sukhjeevan
    Commented Mar 22, 2011 at 4:43
4

Here is a simple workaround in jQuery. You can implement it as a user script to apply it to every page you view.

$(function () {
  $('img').live('mouseover', function () {
    var img = $(this); // cache query
    if (img.title) {
      return;
    }
    img.attr('title', img.attr('alt'));
  });
});

I have also implemented this as a Chrome extension called alt. Because it uses jQuery.live, it works with dynamically loaded content, too. I have retired this extension and removed it from the Chrome store.

1

Various browsers (mis)handle this in various ways. Using title (an old IE 'standard') isn't particularly appropriate, since the title attribute is a mouseover effect. The jQuery solution above (Alexis) seems on the right track, but I don't think the 'error' occurs at a point where it could be caught. I've had success by replacing at the src with itself, and then catching the error:

$('img').each(function()
{
    $(this).error(function()
    {
        $(this).replaceWith(this.alt);
    }).attr('src',$(this).prop('src'));
});

This, as in the Alexis contribution, has the benefit of removing the missing img image.

1

Internet Explorer 7 (and earlier) displays the value of the alt attribute as a tooltip, when mousing over the image. This is NOT the correct behavior, according to the HTML specification. The title attribute should be used instead. Source: http://www.w3schools.com/tags/att_img_alt.asp

1

To display the Alt text of missing images, we have to add a style like this. I think, there is no need to add extra javascript for this.

.Your_Image_Class_Name {
  font-size: 14px;
}

It's work for me. Enjoy!!!!

0

You can put title attribute to tag.I hope it will work.

<img src="smiley.gif" title="Smiley face" width="42" height="42">
0

This can be entered in the javascript console to replace empty titles with alt text (if available) for images on a single page:

[...document.getElementsByTagName('img')].forEach((x) => {
  if(x.getAttribute('alt') && !x.getAttribute('title')){
    x.setAttribute('title',x.getAttribute('alt'));}})

It doesn't require JQuery.

-2

I use this, it works with php...

<span><?php
if (file_exists("image/".$data['img_name'])) {
  ?>
  <img src="image/<?php echo $data['img_name']; ?>" width="100" height="100">
  <?php
}else{
  echo "Image Not Found";
}>?
</span>

Essentially what the code is doing, is checking for the File. The $data variable will be used with our array then actually make the desired change. If it isn't found, it will throw an Exception.

Not the answer you're looking for? Browse other questions tagged or ask your own question.