An abbreviation written in HTML: <abbr title="abbreviation">ABBR</abbr>
The humble <abbr> tag in action. • Open image

If you are just here to see my suggestion for a more accessible abbr implementation, skip to another solution. (You will not collect $200 when passing Go.)

An abbreviated history of abbreviations

In Hyper-Text Mark-up Language, if you wrap some gobbledygook in a cute <abbr> tag, you tell the world (or at least your computer) that said gobbledygook is an abbreviation (handily, the abbreviation of abbreviation is abbr).

HTML
<abbr>NASA</abbr>

So, now you intend your poor user to jump over to Google and figure out what the abbreviation means?

Or you could help them by adding a title to your abbreviation to tell them you didn’t mean the NahverkehrsService Sachsen-Anhalt:

HTML
<abbr title="National Aeronautics and Space Administration">NASA</abbr>

Job done! When your users hover over it, a little popover appears with the expanded label, and screen readers will read it out.

Except it hardly ever does and most of them don’t…

As a frontend engineer you would reasonably assume that using bona fide, consortium-approved tags would guarantee accessibility, usability, and general completeness — and that you don't need to design some hoops, build them, and jump through them.

But you would — unreasonably — be wrong!

Let’s count the ways that this is bad:

  • Most screen readers bluntly ignore title attributes and — with default settings — do not expand abbreviations.
  • The title attribute only appears on hover, so is completely inexistant to keyboard users. (Touch-screen users occasionally have success with tapping on it to reveal the title — but only in favourable winds and with divine patronage.)
  • If you live in one of those parallel realities, where it is possible to conjure up the title, you know just how horrible that experience is. So bad, that you would be happier if it didn’t appear (and assuming it meant NahverkehrsService Sachsen-Anhalt would be perfectly tolerable).

The solution

So, how can we make it less bad?

We could throw a pageful of JavaScript and handful of ARIA-labels at it (ARIA is itself an abbreviation of Accessible Rich Internet Applications), some custom CSS (another abbreviation, this time for Cascading Style Sheets). JavaScript is not an abbreviation but has nothing to do with Java — neither the island, the programming language, or the coffee.

Or we could just write it out:

He plans regional bus routes in Saxony for the NahverkehrsService Sachsen-Anhalt (NASA).

Now you have successfully introduced the concept and its abbreviation and can just refer to it by its abbreviation from now on.

I can let you in on a secret: they have been doing this in print articles since print articles and style manuals were a thing.

Another solution

The above is sound advice. Take it onboard. Tell your neighbour.

But sometimes — just every so often — writing out the abbreviation is not helpful. For example, when the abbreviation is more common than its expansion (NASA (the one you are really thinking of), laser (yes, it is an abbreviation!), PDF, ABBA, etc. (also an abbreviation)).

Sometimes… But having the expansion to hand is… er… handy, because you can never presume that your audience has the same cultural background as yourself (and we all some of us know that to presume makes a pre of u and me!).

The HTML

So, for this site — where I sometimes spend hours thinking about how to best add a specific acronym, initialism, contraction, or numeronym — I have come up with this pattern:

HTML
<span class="has-tooltip"> <button aria-describedby="abbrID"> <abbr>NASA</abbr> </button> <span id="abbrID" role="tooltip">NahverkehrsService Sachsen-Anhalt</span> </span>

So, what does this do?

  • the <span> just holds the whole thing together in a tight unit. The has-tooltip class is to hang some CSS on (CSS is an initialism).
  • The <button> allows the concoction to receive focus for people with a preference or need for a mouse-free house and that the thing with an ID of "abbrID" will tell you more about the content (ID is a contraction).
  • Then we finally have the <abbr> tag — because NASA is an acronym.
  • The <span> with the id of "abbrID" holds the expansion — like we promised. Additionally, it has role="tooltip" that tells assistive technology that the element is a… 🥁!… tooltip — so, now we all know.

The CSS

Don‘t want the explanation? Go directly to all the HTML and CSS.

Let us sprinkle some cascading magic unto our HTML.

First, we remove the styling for the button, because it doesn't need to look like a button. We don’t want our users to click a button saying "NASA" in the hope they go to space.

CSS
.has-tooltip button { appearance: none; background: none; border: none; }

Secondly, we want some default styling for very old or weird browsers that don’t support the clever things we will do later.

CSS (cont.)
[role=tooltip]::before { content: " ("; } [role=tooltip]::after { content: ")"; }

This will render our abbreviation as: HTML (Hyper-Text Mark-up Language).

But that is not what we want to do. We have made the collective decision — and we didn’t make it lightly — that the expansion is not really necessary, and should only be shown to people who insist on expanding it.

So, if your users are proud owners of a fine input device capable of hovering (such as a mouse) or a keyboard capable of tabbing, then we will go the whole hog!

The rest of our styles will be sitting inside this media query:

CSS (cont.)
@media (pointer: fine), (hover: hover) { ⋮ }

Nothing magical here. We are just asking if they are using a precise mouse (as opposed to a fat, fumbling finger) and if they can somehow hover. If they can, we can safely assume they can also tab.

First things first: let’s remove those brackets we just added:

CSS (cont.)
@media (pointer: fine), (hover: hover) { .has-tooltip [role=tooltip]::before, .has-tooltip [role=tooltip]::after { content: none; } }

Done!

Next, we’ll sprinkle some modern magic with anchor positioning 🪄⊹₊⟡⋆:

CSS (cont.)
@media (pointer: fine), (hover: hover) { ⋮ .has-tooltip { anchor-scope: --tooltip-anchor; } .has-tooltip button { anchor-name: --tooltip-anchor; } .has-tooltip [role=tooltip] { inline-size: fit-content; position: fixed; position-anchor: --tooltip-anchor; position-area: none; position-try: flip-block; z-index: 1000; } }

I am not going to explain any of this here. This is about abbreviations, not anchor positioning. Suffice to say, that this keeps the tooltip anchored to the <abbr> but moves it above or below to always keep it visible in the viewport. There is a link in further reading for the obsessive.

Our tooltip is now hidden (I told you it was magic!). So, now we just need to conjure it up when someone focuses our button or hovers over it. Et voilà:

CSS (cont.)
@media (pointer: fine), (hover: hover) { ⋮ .has-tooltip:has(button:hover) [role=tooltip], .has-tooltip:focus-within [role=tooltip] { position-area: block-start; } }

If you haven’t already, do read up on anchor positioning. It removes a tonne of JavaScript that is representational rather than functional…

Lastly, we just need to indicate if something happens when you interact with it. That, by convention, is a dashed underline:

CSS (cont.)
@media (pointer: fine), (hover: hover) { ⋮ .has-tooltip:has([role=tooltip]) abbr, abbr[title] { text-decoration: underline dashed; } }

I have added abbreviations using the title attribute — just in case you forget yourself…

Now, we just need to include our presentational styles for the tooltip (not included in box) and we’re done.

And there you have it: an accessible abbr component, that you shouldn't really use, because you should just keep it simple and write it out the first time. But just for those rare occurrences…

Here’s a test: Tedious Example Saves Time

Caveats

  • Screen readers will still read out the whole thing and people tabbing through your site will stumble over each occurrence. So, don’t be a hole about it and add it every single time you use the abbreviation. Like the Solution above, only do it the first time you introduce the concept.
  • Always consider carefully if you really, really, really need it or you are better served with just expanding the abbreviation on introduction or omitting the expansion completely, because everyone knows what NASA you are referring to (unless they live in Saxony-Anhalt and may get confused.)

All the HTML and CSS

HTML
<span class="has-tooltip"> <button aria-describedby="abbrID"> <abbr>NASA</abbr> </button> <span id="abbrID" role="tooltip">NahverkehrsService Sachsen-Anhalt</span> </span>
CSS
.has-tooltip button { appearance: none; background: none; border: 0; padding: 0; } [role=tooltip]::before { content: " ("; } [role=tooltip]::after { content: ")"; } @media (pointer: fine), (hover: hover) { .has-tooltip [role=tooltip]::before, .has-tooltip [role=tooltip]::after { content: none; } .has-tooltip { anchor-scope: --tooltip-anchor; } .has-tooltip button { anchor-name: --tooltip-anchor; } .has-tooltip [role=tooltip] { inline-size: fit-content; position: fixed; position-anchor: --tooltip-anchor; position-area: none; position-try: flip-block; z-index: 1000; /* don’t forget all your lovely tooltip styling */ } .has-tooltip:has(button:hover) [role=tooltip], .has-tooltip:focus-within [role=tooltip] { position-area: block-start; } .has-tooltip:has([role=tooltip]) abbr, abbr[title] { text-decoration: underline dashed; } }

Further reading