Advertisement
  1. Web Design
  2. HTML/CSS
  3. HTML

Quick Tip: Tooltips, Courtesy of HTML5 Data Attributes

Scroll to top
Read Time: 2 min
This post is part of a series called Design and Build a Static Admin Bar .
Building a Static Admin Bar for the Browser

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.

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:

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

CSS:

1
2
a.tooltip span {
3
	font-size: 10px;
4
	position:absolute;
5
	z-index: 999;
6
	white-space:nowrap;
7
	bottom:9999px;
8
	left: 50%;
9
	background:#000;
10
	color:#e0e0e0;
11
	padding:0px 7px;
12
	line-height: 24px;
13
	height: 24px;
14
	
15
	opacity: 0;	
16
	transition:opacity 0.4s ease-out; 
17
	}		
18
	
19
a.tooltip span::before {
20
	content: "";
21
	display: block;
22
	border-left: 6px solid #000000;
23
	border-top: 6px solid transparent;
24
	position: absolute;
25
	top: -6px;
26
	left: 0px;
27
	}
28
	
29
a.tooltip:hover span {
30
	opacity: 1;
31
	bottom:-35px;
32
	}

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:

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

CSS:

1
2
a.tooltip::before {
3
	content: attr(data-tip) ;	
4
	
5
	font-size: 10px;
6
	position:absolute;
7
	z-index: 999;
8
	white-space:nowrap;
9
	bottom:9999px;
10
	left: 50%;
11
	background:#000;
12
	color:#e0e0e0;
13
	padding:0px 7px;
14
	line-height: 24px;
15
	height: 24px;
16
	
17
	opacity: 0;  
18
   	transition:opacity 0.4s ease-out; 
19
	}
20
	
21
a.tooltip:hover::before	{
22
	opacity: 1;
23
	bottom:-35px;
24
	}

Useful Resources

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.