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
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    JavaScript Canvas Image Conversion

    At last week's Mozilla WebDev Offsite, we all spent half of the last day hacking on our future Mozilla Marketplace app. One mobile app that recently got a lot of attention was Instagram, which sold to Facebook for the bat shit crazy price of one...

  • By
    Using jQuery and MooTools Together

    There's yet another reason to master more than one JavaScript library: you can use some of them together! Since MooTools is prototype-based and jQuery is not, jQuery and MooTools may be used together on the same page. The XHTML and JavaScript jQuery is namespaced so the...

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!