Quick Tip: Tooltips, Courtesy of HTML5 Data Attributes

Quick Tip: Tooltips, Courtesy of HTML5 Data Attributes

Tutorial Details
  • Topic: HTML5 + CSS
  • Difficulty: Intermediate
  • Screencast duration: 15 mins
This entry is part 3 of 3 in the series Static Admin Bar

Tooltips are relatively simple to implement by just adding a bit of markup to your HTML and employing a bit of CSS. However, if you’ve never heard of HTML5 data attributes, you may want to check out this alternative (and much cleaner) approach.


Watch Screencast

If you’ve been following the Admin Bar series, this screencast should finish things off nicely. If you haven’t been following along, don’t worry; this screencast will teach you something you can use in all kinds of situations. We’re going to look at a couple of options to get our tooltips up and running.

Don’t like ads? Download the video, or subscribe to Webdesigntuts+ screencasts via iTunes!

If, for some crazy reason, you’d rather not watch me demonstrate things, below are a couple of snippets to take away and play with. Note: these are simplified examples – you may want to add browser prefixes and additional styling etc.


Tooltip Snippet: Added Markup

The first example uses additional markup in the form of a <span> within our anchor. It works just fine, allows us to add a decorative ‘point’ to our tooltip, and is currently the safer option where browser compatibility is concerned.

HTML:

<a href="#" class="tooltip">This is the link<span>this is the tip!</span></a>

CSS:

a.tooltip span {
	font-size: 10px;
	position:absolute;
	z-index: 999;
	white-space:nowrap;
	bottom:9999px;
	left: 50%;
	background:#000;
	color:#e0e0e0;
	padding:0px 7px;
	line-height: 24px;
	height: 24px;
	opacity: 0;
	transition:opacity 0.4s ease-out;
	}
a.tooltip span::before {
	content: "";
	display: block;
	border-left: 6px solid #000000;
	border-top: 6px solid transparent;
	position: absolute;
	top: -6px;
	left: 0px;
	}
a.tooltip:hover span {
	opacity: 1;
	bottom:-35px;
	}

Tooltip Snippet: HTML5 Data Attribute

Here’s the example which cleans up our markup, uses the HTML5 data-attribute to hold the value of our tooltip, and the css ::before pseudo-element to display it. Much neater.

HTML:

<a href="#" class="tooltip" data-tip="this is the tip!">This is the link</a>

CSS:

a.tooltip::before {
	content: attr(data-tip) ;
	font-size: 10px;
	position:absolute;
	z-index: 999;
	white-space:nowrap;
	bottom:9999px;
	left: 50%;
	background:#000;
	color:#e0e0e0;
	padding:0px 7px;
	line-height: 24px;
	height: 24px;
	opacity: 0;
   	transition:opacity 0.4s ease-out;
	}
a.tooltip:hover::before	{
	opacity: 1;
	bottom:-35px;
	}

Useful Resources

Series Navigation<< Building a Static Admin Bar for the Browser

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.webjobcentral.com John

    AHHHH, I just built one in JS for a client a month ago that I could have used this for. Thanks for the tutorial!

  • question

    it there a way we can get the border back on the data-tip element?

    • http://www.snaptin.com Ian Yates
      Author

      Well, as we’re generating the tooltip using the ::before pseudo-element, we can’t then generate more content from it.

      We could use a sibling element (a.tooltip::after) and position that above the tooltip – a bit disjointed, but it would work in this case.

      The only alternative (I can think of) would be using a background image which includes the point. Then add some padding on top of the tooltip to allow for it..

      Hope that helps!

  • Pingback: Quick Tip: Tooltips, Courtesy of HTML5 Data Attributes | Shadowtek | Hosting and Design Solutions

  • http://eichefam.net Paul

    Niiiice.

    I did something similar for “publish” and “delete” glyph buttons, just using the class to determine what content to display. Using the HTML5 data attribute is a fantastic idea!

    • Florian

      Wouldn’t it be better to use something more semantic and accessible like the title attribute?

      • http://www.snaptin.com Ian Yates
        Author

        On the one hand, you’re right. The title attribute isn’t without its problems though (did you watch the screencast?!)

  • Philkav82

    Great series thus far Ian, learned some neat tricks, however my missus informs me were all out of coffee :)

    • http://www.snaptin.com Ian Yates
      Author

      Great to hear (that you’ve learned something, not that you’re out of coffee) – thanks :)

  • Jan Wessel

    Interesting tutorial, yet I must say, that I would rather use a solution that relies on the title-attribute and Javascript being active. That way, the browser would show a tooltip even if the user turned scripts off.

  • http://www.fakeword.com/ Markov Generator

    Oh man. I’m a bit late to the HTML5 party but I suspect I’m not alone in that respect. This tutorial has encouraged me to embrace HTML5 and learn more about getting the most from it. KUDOS!

  • David

    Thanks for the tool tip ! Creating the small triangular pointer with borders is a great idea. On another note, I am loving the wallpaper you are using, and having visited the campsite myself in Buttermere to walk up Red Pike, was just wondering where you acquired it from, or if in fact you took it yourself :) Thanks.

    • http://www.snaptin.com Ian Yates
      Author

      Good spot! It’s a photo I took a couple of Februarys back when I was camping with my two brothers – we also wandered over a cold and blustery Red Pike at one point, and I’m pretty sure we sampled an ale by the same name too :) Thanks for your comment!

  • Pingback: Learn HTML5: HTML5 Tutorials and Guides | Vandelay Design Blog

  • Pingback: Learn HTML5: HTML5 Tutorials and Guides | CS5 Design

  • Pingback: Learn HTML5: HTML5 Tutorials and Guides

  • Snagy

    Is there a way to add links or customize the content of the data-tip?

  • Pingback: CSS3HTML5 » Learn HTML5: HTML5 Tutorials and Guides

  • http://chovy.com chovy

    I needed to add a.tooltip { position: relative; } other wise it was 50% from window.

  • Pingback: Tweet-Parade (no.45 Nov 2011) | gonzoblog.nl

  • Pingback: All You Need to Know About the HTML5 Data Attribute - Website Design Prices

  • cvwlc

    Thanks for this! Could I add an image instead of text as the tooltip? i.e. data-img=”img src=’images/img.jpg’”

  • Kim

    This is an excellent tutorial and well explained. Thanks very much.