Visit DynamiX Web Design! E-Mail DynamiX Web Design!

css: Create awesome tooltips without javascript!

Recently, we came across the need for some very simple tooltips. The only real caveat was that they needed to be lightweight, and since we already had a number of scripts running on the page, we preferred to make these using just css. Thankfully, our dear friend the <span> tag is up to the challenge of making these tips fairly easily.

Example 1

We’ll start with an easy example. You have a link to somewhere, and want people to see a message before they click on it.

Like this:
Hi Labs visitors! Don’t forget to check out DynamiX Web DesignIt’ll rock your socks off!!

Here’s the HTML:
Hi Labs visitors! Don't forget to check out <a href="http://www.dynamixwebdesign.com">DynamiX Web Design<span>It'll rock your socks off!</span></a>!

It doesn’t matter whether you place the span tag before or after the actual text for the A tag, just as long as it’s inside it.

Here’s the CSS:
.dynamixtip{position:relative;}
.dynamixtip span{display:none;}
.dynamixtip:hover span{display:block;position:absolute; left:0;top:20px;}
This part is just for the look/feel of the tip, style as you wish:
.dynamixtip:hover span{padding:3px;background:#5f9215; color:#fff;border:1px solid #3f6607;font-size:10px;}

Simple isn’t it? Now let’s get more complicated (but still easy).

Example 2

What if you wanted to put a picture in that tooltip, and float it to the right of your link?

Like this:
Balloons are cool!this balloon rocks

Here’s the HTML:
<a href="http://www.sxc.hu/photo/1011246">Balloons are cool!<span><img src="http://www.dynamixlabs.com/images/balloon.gif" alt="this balloon rocks" /></span></a>

Here’s the CSS:
.dynamixtip2{position:relative;}
.dynamixtip2 span{display:none;}
.dynamixtip2:hover span{display:block;position:absolute; left:130px;top:-50px;}

Configure “left” and “top” as desired to position your tip, and you’re done.

Example 3

Let’s say you wanted to tell someone that this balloon is red.

this balloon is totally redThis balloon is red!

This one is just as easy as the earlier examples. There are a few ways to do this, depending on what you want/need to do for this item, but we chose to simply encase the image in a div and apply our class to it. Note that IE 6 doesn’t support the hover property when applied directly to a div. Alternatively, you can simply replace the div with another span tag, and add “display:block” to .dynamixtip3. If you want us to get fancier, leave a comment and we’ll be happy to get into some alternatives.

Here’s the HTML
<div><img src="http://www.dynamixlabs.com/images/balloon.gif" alt="this balloon is totally red"/><span>This balloon is red!</span></div>

Here’s the CSS
.dynamixtip3{position:relative;text-align:center;}
.dynamixtip3 span{display:none;}
.dynamixtip3:hover span{display:block;position:absolute;left:65%;top:25%;}
This part is just for the look/feel of the tip, style as you wish:
.dynamixtip3:hover span{padding:3px;background:red; border:1px solid #800;color:#fff;font-size:11px;}

Notice that for this example, we positioned the tip using percentages instead of pixels. You can of course also use em if you prefer.

That’s all there is to it! Pretty simple, huh?

Compatibility:
This method has been tested in and is compatible with Internet Explorer 6 (beware of :hover when applied to divs, as well as the “bottom” declaration on absolutely positioned elements), 7, 8, Firefox (PC and Mac), Netscape and Safari (PC and Mac).

That’s all folks!
Thank you for taking the time to read one of our articles. If you still find yourself stuck, we’d be happy to lend a hand. Simply leave us a comment below, and we’ll do our best to help you out. Found something that we’ve missed? We’d love to hear it.

Tags: , , , ,

Pass This Article On! Tiny Url: http://tinyurl.com/mwr9hu

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

4 Responses to “css: Create awesome tooltips without javascript!”

  1. Todd Says:

    Thanks for the post! Love the balloons, lol.

  2. Buster BluthNo Gravatar Says:

    This is just was I needed!

  3. emmieNo Gravatar Says:

    hello :) i really like example 3,
    and would like to use it for my Myspace.
    i put it on there to see if it works, but it dosn't work right,
    like i see the baloon and in just normal text it says, "this baloon is red!"
    could you give me some help in how this works?
    email me at; linkinparkisl0ve@yahoo.com

  4. dynamixlabsNo Gravatar Says:

    Hi Emmie!
    Could you send us a link to your page? Did you add the CSS in example 3 onto the page as well?

We want your feedback! Leave us your thoughts below.






DynamiX Web Design

DynamiX Labs is Copyright © 2010 DynamiX Web Design, LLC.
DynamiX Labs is proudly powered by WordPress

Entries & Comments.  |  thanks & about.


The DynamiX Mascot button submit it! Background
hire dynamix!