0

On my web page I have an anchor (code below), which I visually display as an image (logotype) completely hiding the text. I'd like the anchor to be:

  • accessible - read properly by screen readers,
  • display a tooltip when hovered by mouse (the tooltip explains the image logotype, which may not be understood by everyone).

I use the following method:

HTML

<a href="http://some-institution.com"
   title="Some Institution"
   class="replace-text-with-image">Some Institution</a>

CSS

.replace-text-with-image {
    /* Hide the text. */
    text-indent: 100%;
    white-space: nowrap;
    overflow: hidden;
    /* Display the image. */
    width: <image-width>;
    height: <image-height>;
    background-image: url(<url-to-image>);
    background-size: <image-width> <image-height>;
    background-repeat: none;
}

The problems with the method above:

  • A guy reported to me that on encountering the link his screen reader reads both the link's contents ("Some Institution") and then the link's title ("Some Institution" again), resulting in a "stutter".
  • I read (in this article) that using title for accessibility is wrong and aria-label should be used instead.

So I tried re-factoring to use both aria-label and title:

<a href="http://some-institution.com"
   title="Some Institution"
   aria-label="Some Institution"
   class="replace-text-with-image">Some Institution</a>

Now NVDA (the screen reader I'm testing on) reads both title and aria-label attributes resulting in a stutter again.

Other possible solutions which won't work:

  • When I remove the title attribute and leave only aria-label it's read fine, but I can't remove title attribute, as that's an explanation for the visual users that I want to have.
  • I could also stay with just title attribute completely removing anchor's contents, but I feel having an empty anchor is not a particularly good practice, isn't it?

What's a solution for that problem?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Robert Kusznier
  • 6,471
  • 9
  • 50
  • 71

2 Answers2

0

First off, I would really encourage you to use the simplest possible markup, which would be an anchor element wrapped around an image element, like this: https://jsfiddle.net/pLuqqh8w/

You should be using the title attribute of your anchor element to describe the intent of the link (i.e. where it goes), and use the alt attribute to describe the intent of your image (i.e. what it is). This is how the technology is designed to work. The alt and title attributes should not be same thing.

That being said, if you REALLY want to have a tooltip that isn't announced by screen readers, here's how I would recommend doing it: https://jsfiddle.net/neht6v01/

EDIT: Using the title attribute isn't wrong per-se, it's just not something that should be relied upon because it has very spotty support among screen-reader/browser combinations.

Josh
  • 3,872
  • 1
  • 12
  • 13
  • Probably I wasn't clear: it's not that I want a tooltip **that isn't announced by screen readers**. Actually it's the opposite - I want the tooltip to be identical to what's announced by screen readers. My problem was that `title` would give me a tooltip but not exactly accessibility, `aria-label` would give me accessibility without tooltip, while providing both makes NVDA read both of them which results in a stutter. – Robert Kusznier Dec 01 '17 at 09:34
  • You recommend using the simplest possible markup. I used text, while you use text. Isn't text the simpler one? I avoided using consciously, because I believe that all images which are pure decorations - and that image is, it's just a visual representation of a regular anchor with a text name - should not be present in the markup HTML but only in CSS – Robert Kusznier Dec 01 '17 at 09:39
  • At the end of the day, the "stutter" that you're worried about isn't a big deal. As a web developer, you can't control exactly how assistive technology is going to behave. Every screen reader/browser combination will render content differently. Your role as a web developer is to follow the standards and use the web technologies as they were designed, according to the specification. This is why I said your use of a background image is overly complicated. Background images weren't designed to be used this way. They are intended for wallpaper style backgrounds. – Josh Dec 01 '17 at 19:01
  • But if you're really set on doing this the way that you've explained, I'd recommend looking into using CSS tooltips. That may get you where you need to be. https://www.w3schools.com/css/css_tooltip.asp – Josh Dec 01 '17 at 19:02
0

You should think about which ARIA role you have to give.

The a[href] is a link. Can it be also an image? No. So, create an image.

<a href="http://some-institution.com" aria-label="Some institution">
   <span class="replace-text-with-image" role="img" title="Some institution">Some institution</span>
</a>

This way, the screenreader will announce the aria-label. And the title will be available to mouse users.

title is not bad for accessibility but can be useless for screenreaders. So you should not rely only on the title attribute but, as you perfectly pointed out, aria-label is useless for non-screenreader users.

Adam
  • 17,838
  • 32
  • 54
  • But this link is not an image - it's a regular link with a text name and that's how I'd like screen reader to interpret it. Only for visual readers I'd like the link to be displayed as an image, but only as a pure decoration. If that would be a meaningful image (not pure decoration), I'd use tag inside of an anchor. – Robert Kusznier Dec 01 '17 at 09:43
  • @Robert My answer relies on adding a `span` inside the `a`. It would also work replacing `role=img` with `role=presentation` (or no role at all) – Adam Dec 01 '17 at 14:09
  • (but technically an image with an alternative text is not pure decoration) – Adam Dec 01 '17 at 14:10
  • But I don't have an image with alternative text. I have a link, which I want to decorate with an image, but only for visual users. But yes, your answer should work nonetheless. I'll test it soon. – Robert Kusznier Dec 01 '17 at 14:49