Accessibility Tip: Empty alt Attributes

By  on  

As mostly a self-taught developer, my impressions of best practices were self-formed, and over the years I've realized many of those impressions were incorrect or the standards associated with them had quickly changed.  One small but important practice is the usage of alt attributes on img tags, and I had thought you should set an alt attributes on all images within the page.  Little did I know that isn't the case.

<!-- BAD: image URL will be read out -->
<img src="/wp-content/themes/punky/images/logo.png">

<!-- BAD: adding useless cruft alt -->
<img src="/wp-content/themes/punky/images/logo.png" alt="Logo">

<!-- GOOD: empty alt is silent -->
<img src="/wp-content/themes/punky/images/logo.png" alt="">

Images with only visual value should have an empty alt attribute set on them.  Omitting the alt attribute makes most screen readers read out the entire image URL and providing an alt attribute when the image is for visual purposes only is just as useless.

You can't blame young developers for not knowing this or any other accessibility related topic -- accessibility (and usability for that matter) aren't topics taught at most universities.  Let's be honest -- most developers are excited their stuff works; accessibility and usability are just added bonuses.  Well, now you have no excuse to not use alt attributes correctly!

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    5 Awesome New Mozilla Technologies You&#8217;ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

Incredible Demos

  • By
    MooTools onLoad SmoothScrolling

    SmoothScroll is a fantastic MooTools plugin but smooth scrolling only occurs when the anchor is on the same page. Making SmoothScroll work across pages is as easy as a few extra line of MooTools and a querystring variable. The MooTools / PHP Of course, this is a...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Discussion

  1. Of course this only applies to decorative images, really good guide to alt text on Webaim – http://webaim.org/techniques/alttext/

  2. I’m curious about SEO. What if I want my logo to index in Google as “My Company Logo”? I’d imagine that I’d like an alt tag.

    
    

    And I wouldn’t care much about a screen reader announcing the alt text.

    Of course this use case would apply only to images you’d like indexed, which wouldn’t include many decorative images. In that case, an empty alt seems correct.

  3. Wilkins

    It’s actually recommended to use the aria role “presentation”, no need for an empty alt tag. You actually don’t even need the alt tag if you’re using a presentation role.
    http://www.w3.org/TR/wai-aria/roles

  4. That’s interesting, I’ve always thought it was good practice to add alt tags to images. Especially with the W3 validator listing images with missing alt tags as errors, although it does specify under certain conditions.

    I just tested it with an empty alt tag, and it appears to accept that. Thanks I’ll start using this from now on.

  5. Good trick. But it may not be a good practice to put decorative images in HTML at all. Why can’t we use CSS here?

  6. 
    

    Just sayin’ ^^

  7. Kyra

    so first you say “little did i know that this [the fact you should use alt everywhere] isn’t the case”, but then you proceed to show us why it indeed is the case (to prevent url reading), might wanna clarify that.

  8. Regina Battle

    “…accessibility and usability are just added bonuses.”

    Wrong. Sites should be inclusive — accessible and easy to use.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!