Alt attribute for ASCII art in HTML?
Asked Answered
R

2

9

This is a very (very) specific question, but would it be appropriate to use the alt attribute for a pre tag when using that to show ASCII art? It's essentially like an image (which wouldn't be intelligible using a screen reader), so using alt makes sense.

<pre alt="A cute little blob creature">༼ つ ◕_◕ ༽つ</pre>

Would is be read by a screen reader? Would this be appropriate or good form?

(I could imagine other situations where this might arise, like using a typographical object to denote a particular action, such as a "home" link.)

Redress answered 26/11, 2015 at 2:27 Comment(1)
I like the idea, semantically, but does pre even have an alt attribute? If it's non-standard then its applicability to screen readers and the like may be moot.Swatch
F
9

The aria-label attribute exists for this purpose.

<pre aria-label="A cute little blob creature">༼ つ ◕_◕ ༽つ</pre>

For more information, check out this MDN article on it.

Favus answered 26/11, 2015 at 2:32 Comment(4)
Thanks! I wasn't able to see anything about browser support – will this be accessible for most screen-reader users?Redress
Ah, found some info: developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/…Redress
As you've discovered, browser support doesn't matter. It's the accessibility software (screen-reader etc.) that needs to support it ;)Favus
the MDN article doesn't exist anymore. On the internet archive website, at the time the answer was written, this screenshot was taken : web.archive.org/web/20150910173438/https://…, the last screenshot of it is from jamuary 2022 and it doesn't differ, and then for few months it redirects to this MDN article : developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/…, and now it doesn't redirect anymoreOtology
I
2

You have different options to consider as screen readers are just one step in the big journey of accessibility.

  1. The title attribute

The alt attribute is specific to img and area tags. For other element, you can use the title attribute which will give the use access to a tooltip in many browser implementations:

See HTML5 doc:

The title attribute represents advisory information for the element, such as would be appropriate for a tooltip. On a link, this could be the title or a description of the target resource; on an image, it could be the image credit or a description of the image; on a paragraph, it could be a footnote or commentary on the text; on a citation, it could be further information about the source; on interactive content, it could be a label for, or instructions for, use of the element; and so forth. The value is text.

Warning! Relying on the title attribute is currently discouraged as many user agents do not expose the attribute in an accessible manner as required by this specification (e.g. requiring a pointing device such as a mouse to cause a tooltip to appear, which excludes keyboard-only users and touch-only users, such as anyone with a modern phone or tablet).

The title attribute might be used with some conditions: using it on a targetable element (using tabindex=0) so that it can be accessed with keyboard, using javascript to show the tooltip when the browser doesn't, and giving visual clue that we can access the definition by focusing the element (underlying, question mark, ...).

  1. The aria-label attribute

Many screen readers do not read by default the value of the title attribute, so you are encouraged to use the aria-label which is somehow specific to screen-readers. As people not using screen reader won't benefit of an aria-label, you have to use it in conjunction with the title attribute.

Note that you can use two different text as the title can be used as a description when aria-label is more a text replacement.

<pre aria-label="Welcome!" title="A cute little blob creature">༼ つ ◕_◕ ༽つ</pre>
  1. Ignoring it.

You can also consider that this is meaningless to a screen reader, and consider it as a purely decorative text using the aria-hidden attribute.

<pre aria-hidden="true">༼ つ ◕_◕ ༽つ</pre>
Idou answered 26/11, 2015 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.