1. 01. Incoming
  2. 02. Hifi
  3. 03. Lofi
  4. 04. The Lab
  5. 05. Tags

frequency decoder

CSS Tooltips using generated content and HTML5 "data-" attributes

Posted Monday June 21, 2010

frequency decoder

This idea was directly inspired by Nicolas Gallagher’s article Multiple Backgrounds and Borders with CSS 2.1 – which is a must-read for any self respecting web designer out there in the ether (it has to be one of the best CSS techniques I’ve seen in a long time and it’s a technique that’s here to stay, so go read up on it).

Don’t forget, all of the disclaimers detailed by Nicholas in his original article apply here as well (z-index issues, browser support etc).

Quick Notes on the implementation

  • No Javascript or extra HTML elements required
  • The tooltip is composed of two elements created by using :before & :after generated content
  • The tooltip arrow is created by using the borders of the :before generated content layer
  • The tooltip body is created by using the :after generated content layer
  • The content declaration content:attr(data-tooltip); is used to fill the :after generated content layer with the text of the data-tooltip attribute
  • Only nodes having the data-tooltip attribute are converted to a tooltip by using a sneaky CSS selector *[data-tooltip] (Note: the star selector is a performance killer and it would be better to replace it completely with a known set of elements e.g. span[data-tooltip], a[data-tooltip])
  • None of the CSS transitions currently present in the demo source work as no browser currently supports using transitions on generated content layers
  • It was written in my lunch hour and I’m sure you will find ways to improve on the implementation

Accessibility

Zero. None. Nada. As generated content is ignored by screen-readers & alternative browsing devices, the tooltip content is not accessible. So, to cut to the chase, don’t use the tooltips if you care about the accessibility of your web content (and you do care – don’t you?). It was an interesting lunchtime experiment, no more, no less.

Misuse of the HTML5 Custom Data Attribute

The presence/absence of a particular data attribute should not be used as a CSS hook for any styling. Doing so would suggest that the data you are storing is of immediate importance to the user and should be marked up in a more semantic and accessible manner.

Via html5doctor.

Demo

View the tooltip demo. Theoretically suitable for consumption in IE8+, Chrome 4+, Safari 4+, Firefox 3.6+ and Opera 10+. Tested in Firefox & Safari.

Tags: css, data attributes, generated content, html5

Previous Comments ~

Kroc Camen · http://camendesign.com
#1 · Tuesday August 31, 2010

Completely over-engineered.

Use the dfn / abbr element with a title and style it:

See http://camendesign.com/code/uth7_floaty-abbrs
and http://camendesign.com/code/abbr_redux for details on semantic use of dfn and abbr.

frequency decoder
#2 · Sunday September 12, 2010

Hi Kroc,

You’re expanding abbreviations, I’m showing a tooltip – different things entirely. Over-engineered or not, I wasn’t striving for any sort of perfection… maybe you didn’t see the “this was a lunchtime experiment” bit.

Gabor Magyar
#3 · Thursday October 28, 2010

Hi!
Sorry! This is offtopic!

I have some question for the datepicker, but I don’t found the e-mail address. What is that?

Thierry Koblentz · http://www.css-101.org
#4 · Wednesday March 30, 2011

Accessibility: Zero. None. Nada. As generated content is ignored by screen-readers & alternative browsing devices, the tooltip content is not accessible

I believe VoiceOver reads generated content; ARIA mandates it

Johan · http://johandam.com
#5 · Tuesday May 31, 2011

Bloody amazing, might needs some cleaning up but other then that its absolutely amazing

Hotel Jijona · http://www.hoteljijona.com
#6 · Friday January 20, 2012

Is there any way to make this work on an image map?
Like <area shape=“rect” coords=“641,165,669,185” href=“index-en.html” target=”_parent” title=“Hotel Jijona in English”/>, where the “title” would be the tooltip?

A penny for your thoughts…

Remember: Off-topic or dumb-ass comments will, of course, be deleted. Spammers shall have the scary flying-monkeys from “The Wizard of Oz” dispatched to their abode. Please remember to RTFM before asking questions pertaining to any of the Lab experiments.

Reporting a bug? The error message and browser and operating system details would be much appreciated. A URL would be even better. Thanks.

Posting a code block? Please use the Pastebin service to stop textile from mangling your nicely formatted code.

Popular Frequencies

  • Unobtrusive Table Sort Script (revi…
    Saturday September 16, 2006
  • Unobtrusive Date-Picker Widget Upda…
    Monday October 02, 2006
  • Unobtrusive Date-Picker Widget V4
    Tuesday February 03, 2009
  • Unobtrusive Table Sort Script
    Friday November 18, 2005
  • Unobtrusive Date-Picker Widget V5
    Wednesday September 09, 2009
All articles