12

Take the following code:

<abbr title="World Health Organization">WHO</abbr>

Can we style an abbr tag's title? So that instead of a custom tooltip we can use title?

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
Joemon
  • 775
  • 3
  • 9
  • 18
  • FYI on Firefox the abbr tag has a dotted underline to indicate there's something worth hovering over, but not on Chrome or IE. But they can be styled to have it like so: http://html5doctor.com/the-abbr-element/ – Matthew Lock May 25 '16 at 03:06

4 Answers4

28

Actually, Alex Mcp’s answer is incorrect. It is entirely possible to do this with CSS for modern browsers. However, a fallback for older browsers with JavaScript may be used.

abbr {
    position: relative;
}

abbr:hover::after {
    position: absolute;
    bottom: 100%;
    left: 100%;
    display: block;
    padding: 1em;
    background: yellow;
    content: attr(title);
}

This will add an absolutely positioned pseudo element top right of the abbr tag using the attribute content within the title when the abbr tag is hovered over.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
DigiKev
  • 1,071
  • 3
  • 11
  • 19
  • I see the yellow background but `content: attr(title);` isn't working. There's no text. – Pacerier Jul 13 '12 at 00:15
  • It actually does work if you give the element that contains the abbr `position: relative;` –  Aug 30 '13 at 03:37
  • 4
    am I the only one this doesn't work for? even @SoonDead's fiddle doesn't work. if you leave the cursor on the abbr long enough you will see the old tooltip show up as well – George Feb 23 '15 at 20:50
  • @George yes that is true. you can [use javascript](http://jsfiddle.net/sSMKx/33/) to replace the tag and mimic functionality. – vinczemarton Feb 26 '15 at 07:18
  • 1
    Tried this today with Yandex and found the answer from @alex-mcp to be correct despite the difference in votes given this approach does not actually style the browser's pop-up tooltip, it emulates it. In my case I **saw two tooltips at the same time** using this approach: one faux and the other from the browser. – vhs Jan 31 '19 at 11:26
10

If you mean style the actual text that pops up, no you can't style that with CSS; it's browser-specific. Javascript-based tooltips would be the way I would handle it, since it allows to have more control over this behavior.

e-sushi
  • 13,786
  • 10
  • 38
  • 57
Alex Mcp
  • 19,037
  • 12
  • 60
  • 93
  • https://inclusive-components.design/tooltips-toggletips/ seems to be on a better track… – vhs Jan 31 '19 at 11:22
4

I was looking for something similar and came up with the following alternative (tested on Firefox, Safari and Chrome on Mac, works with IE 7-10 when I clicked it), based on this link:

HTML:

<abbr title="My Custom Abbreviation" name="">Abbreviation</abbr>

jQuery:

$('[title]').each( function() {
    var mytitle = $(this);
    mytitle.data('title',mytitle.attr('title')); // get title attribute value
mytitle.attr('name', mytitle.attr('title')); // add title attribute value to NAME attribute
    mytitle.removeAttr('title'); // remove the title attribute, removing browser tooltip
});

CSS:

abbr {
    position: relative;
}
abbr:hover::after {
    position: absolute;
    bottom: 100%;
    left: 100%;
    display: block;
    padding: 1em;
    background: yellow;
    content: attr(name);
}
Community
  • 1
  • 1
davewoodhall
  • 998
  • 3
  • 18
  • 43
  • add `width:max-content;` to the pseudo element. This will give one line tooltip. Also we can use `data-title` instead of name attribute. – the Hutt Oct 14 '21 at 07:31
1

You can style the way the shortened version appears, so in this case 'WHO'. But not the way the pop up box appears, as that is controlled by the browser/OS.

You can get around this limitation by applying some JQuery stuff to any abbr tag programatically - this would display a div, of which the contents would read the same as the title on the tag that called it.

Amadiere
  • 11,313
  • 6
  • 41
  • 47