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
    6 Things You Didn&#8217;t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    Chris Coyier&#8217;s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    Using Opacity to Show Focus with jQuery

    A few days back I debuted a sweet article that made use of MooTools JavaScript and opacity to show focus on a specified element. Here's how to accomplish that feat using jQuery. The jQuery JavaScript There you have it. Opacity is a very simple but effective...

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!