Improving CSS tooltips

But only very slightly… Jack Osborne, whom I have followed on Twitter for a while now, posted some time ago a tooltip tutorial whereby you utilise the :after CSS pseudo-element and the attr() function to populate it. His method works by giving an element a title="" attribute and a class of tooltip, and placing the content of the title attribute after the content, all through CSS.

The only technical downside I can see here is the necessity for that class. Using a simple attribute selector we can ditch that and basically say ‘if any element has a title, put that title after it in CSS’. By simply using [title] over .tooltip we can automate the process and trim some bytes, thus:

<a href="http://twitter.com/jackosborne" title="Follow Jack Osborne on Twitter">Jack Osborne</a>

[title]{
	position:relative;
}
[title]:after{
	content:attr(title);
	color:#fff;
	background:#333;
	background:rgba(51,51,51,0.75);
	padding:5px;
	position:absolute;
	left:-9999px;
	opacity:0;
	bottom:100%;
	white-space:nowrap;
	-webkit-transition:0.25s linear opacity;
}
[title]:hover:after{
	left:5px;
	opacity:1;
}

By Harry Roberts on Tuesday, November 30th, 2010 in Web Development. Tags: , , | 6 Comments »

+

6 Responses to ‘Improving CSS tooltips’


  1. Jonathan Thomas said on 1 December, 2010 at 9:01 am

    Hi,

    Great work Harry, thank you.

    Is there any way to make it work in IE 6?

    Unfortunately, at our place, we still need to support IE 6, however ridiculous that is! :(

    After looking at ‘Fangs’ (Firefox’s screen reader emulator), it doesn’t read out the title text unfortunately. We’re currently looking at making pure CSS tooltips that are completely accessible, do you have any ideas on how we can go about doing this?

    Many thanks,
    Jonathan


  2. Ivan Nikolić said on 1 December, 2010 at 9:05 am

    Shouldn’t this also show generic tooltip on :hover?


  3. Harry Roberts said on 1 December, 2010 at 9:06 am

    Jonathan:

    Well with this being entirely progressive I’d personally not bother making it work in IE6, as IE6 users will still get the browser’s title attribute text on hover. If you want the stylistic effect I’m sure you could use some Javascript, but then in my opinion that’d be overkill for such a trivial addition.

    Also, further to the above, the screenreader doesn’t want to be able to read the CSS tooltip as it will be reading the (more semantic) title attribute text anyway.

    Hope that helps.

    Ivan:

    Yes, this is one drawback of this technique; you get the CSS tooltip and the browser default title attribute on :hover. In an ideal world you’d have one or the other…


  4. Jack Osborne said on 1 December, 2010 at 11:09 am

    Harry,

    I had a good little debate going on in the comments of this article, before I decided to remove the comments from my site. So I thought I’d post some of the better ones in here for others to see — I’ll also update my article according with what I’m about to post and a link through to this article.

    Doug Aitken Pointed out that you see the tooltip twice due to using the alt attribute – http://dl.dropbox.com/u/560599/scrnshts/tooltips.png

    Frequency Decoder said:

    “There’s no way to stop the browser’s default tooltip from appearing I’m afraid (without resorting to JS but this removes all notion of accessibility). I toyed with using a custom data- attribute to hold the tooltip text but again, this just makes the tooltips 100% un-accessible (as screen readers are not supposed to read generated content) if the title attribute isn’t added to the markup.
    Quick custom data- attribute demo: http://www.frequency-decoder.com/demo/css-tooltips
    …and inevitable blah, blah: http://www.frequency-decoder.com/2010/06/21/css-tooltips-using-generated-content-and-html5-data-attributes/

    And Nicolas Gallagher who I am sure you already know, said that Frequency Decoders alternative was his preferred option:

    “Placing text in the title attribute is no guarantee that it will be read by screen-readers either. However, Filament Group’s research indicates that some screen-readers do actually read CSS generated content: http://filamentgroup.com/lab/dingbat_webfonts_accessibility_issues/.
    The article about pseudo-elements on CSS Tricks was influenced by an article I wrote which, if you haven’t read it, I hope might give you some more ideas for experiments using pseudo-elements – http://j.mp/d6UMOd. Please ping me on twitter if you publish any more pseudo-element stuff!”

    Some more food for thought.


  5. Jonathan Thomas said on 1 December, 2010 at 1:16 pm

    Harry, the problem is, as Jack states above, the screen reader doesn’t by default read the ‘title’ attribute. It’s a select option which needs to be switched on by users. As you’ll appreciate, many users won’t deem this necessary or won’t think of enabling the functionality and as a consequence, they will not be able to have any tooltip text read out to them.

    I take your IE6 point, spot on. :)


  6. Dan Arsenault said on 1 July, 2011 at 6:05 pm

    @Ivan
    You could just change the attribute to be something arbitrary like ‘tooltip’ and it will still work without showing the generic title tool tip. Not sure how this would effect screen readers.


Leave a Reply

Respond to Improving CSS tooltips

Hi there, I am Harry Roberts. I am a 21 year old web developer from the UK. I Tweet and write about web standards, typography, best practices and everything in between. You should browse and search my archives and follow me on Twitter, 7,791 people do.

via Ad Packs