CSS3 tooltips

If your icon or button has insufficient text or none at all, or it just needs some additional explanation, then you surely need a CSS3 tooltip for it. Why’s that? Because, as they have proved till now, they can help you improve your website usability.

Having said that, in this article you’ll learn how to create your own CSS3 tooltips: no images, no javascript.

View demo

“Do I really need them?”

The HTML title attribute is the default additional info you can use. But, the default title‘s can’t be styled. So, if you want something cool, that you can style it as you wish, then a custom CSS3 tooltip is the solution.

How is made

The method might be familiar to you, a relative positioned element who wraps an absolute positioned one. With this article I’m not trying to reinvent the wheel, I’m just showing you how to create some cool CSS3 tooltips.

Below you can see the proper structure, note the two pointers (made using :before and :after pseudo-elements ) who overlap:


How the “bordered” pointer is made

Here are the ingredients that were used to create them:

HTML

<a href="#" class="tooltip">
  your text
  <span>Your custom description</span>
</a>

Why an anchor?

Just for compatibility reasons. IE6 has a problem with the :hover pseudo-class used with other elements than anchor.

If you want to use them, and anchors are not an option for you, then you can use this to trigger the tooltips for IE6:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
  $(function() {
    if ($.browser.msie && $.browser.version.substr(0,1)<7)
    {            
      $('.tooltip').mouseover(function(){              
            $(this).children('span').show();                
          }).mouseout(function(){
            $(this).children('span').hide();
          })
    }
  });        
</script>

CSS

.tooltip
{
  position: relative;
  background: #eaeaea;
  cursor: help;
  display: inline-block;
  text-decoration: none;
  color: #222;
  outline: none;
}

.tooltip span
{
  visibility: hidden;
  position: absolute; 
  bottom: 30px;
  left: 50%;
  z-index: 999;
  width: 230px;
  margin-left: -127px;
  padding: 10px;
  border: 2px solid #ccc;
  opacity: .9;
  background-color: #ddd;                     
  background-image: -webkit-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
  background-image: -moz-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
  background-image: -ms-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
  background-image: -o-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
  background-image: linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));  
  -moz-border-radius: 4px;
  border-radius: 4px;  
  -moz-box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
  -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
  box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;  
  text-shadow: 0 1px 0 rgba(255,255,255,.4); 
}

.tooltip:hover
{
  border: 0; /* IE6 fix */
}

.tooltip:hover span
{
  visibility: visible;
}

.tooltip span:before,
.tooltip span:after
{
  content: "";
  position: absolute;
  z-index: 1000;
  bottom: -7px;
  left: 50%;
  margin-left: -8px;  
  border-top: 8px solid #ddd;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;        
  border-bottom: 0;  
}

.tooltip span:before
{
  border-top-color: #ccc;
  bottom: -8px;
}

View demo

Browser support

This is a cross-browser solution, it works also on browsers like IE6 and IE7. Although, it looks quite different as we're talking here about progressive enhancement.

73 thoughts on “CSS3 tooltips

  1. Very nice tut! it looks awesome!! thank you!!

    but i don’t really understand what ” _background: transparent; ” does! is it in order of make IE works?

    • Alejandro, the _background: transparent line is only to solve an IE6 bug.

      For example, instead using background property, you could use also border: none .

      One of the above will fix disappearing spancontent on a:hover. Read more here.

      PS. As a matter of fact, I will update the article soon and I’ll use border property instead background.

      • Thanks a lot Red!

        i see it much more clear with your explanation and the article you’ve linked is quite simple and explicit. but oh sweet god how i hate IE6! :)

  2. I like it! With HTML5 becoming more widely used I’m wondering if it might be good to use the tag rather than a tag, since can be used for related content.

    Just a thought.

    • Looks like my HTML got stripped from my comment. I was saying using the “aside” tag rather than a “span” tag.

      • Seth, thanks for your comment and sorry, HTML code is note allowed here.

        Regarding your question, I’m not sure the HTML5 aside element would match (semantically speaking).

        Instead, I’m thinking about the HTML5 custom attribute data-*. Maybe I’ll write an article about using this HTML5 stuff to create a tooltip. :)

        • Thanks for a great way of showing how to use pure css for tooltips, as I’m trying to miminize the load on the server.

          As for using html5, I just a had look at which tag to use, and the “head” and the “detail” tag would be the new tags to use, and they fit exactly this purpuse.

          The “detail” tag even has an attribute of “open”, so actually made just for stuff like a tooltip.

          • Torben, it’s nice to hear you liked this article!

            Regarding using now a HTML5 semantic tag for these tooltips, I couldn’t say as the drafts are continuously changing!

      • It seems that by adding this (below) to the CSS and using area coordinates in the HTML, you can get the tooltip to map specific areas. What has me stumped though is getting the tool tip to appear over the specific part of the image defined by the coordinates rather than at the fixed position. Any help much appreciated, thanks.

        map { position: relative; }
        area + span { position: absolute; margin-left: -9999em; }
        area:hover + span {

        position: absolute;
        bottom: 10px;
        left: 50%;
        z-index: 999;
        width: 230px;
        margin-left: -127px;
        padding: 10px;
        opacity: .9;
        font: 1.1em helvetica,arial,sans-serif;
        color: #000050;
        border: 2px #000dc7;
        text-shadow: #000 1px 1px 1px;
        background-color: #61bde7;
        background-image: -webkit-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
        background-image: -moz-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
        background-image: -ms-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
        background-image: -o-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
        background-image: linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
        -moz-border-radius: 4px;
        border-radius: 4px;
        -moz-box-shadow: 0 2px 4px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
        -webkit-box-shadow: 0 2px 4px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
        box-shadow: 0 2px 4px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
        text-shadow: 0 1px 0 rgba(255,255,255,.4);
        }

  3. thank you,
    it was simple to add css3 transitions for fade-in and out-effects and put the tip on top of the bubble. css-only. rocks big time!

  4. Hello,

    thanks for providing this great way of adding a tool-tip. It really does look amazing, stumbled over this site by searching for a nice way to implement a decent looking tool-tip function.

    Hmh. I have been trying to add this to my site, but I cannot get this to work. First of all, the page I am trying to add it to lists perks for a game like

    number, skill

    and I thought it would be nice to have the actual text of the skill as mouse-over and thought this would work great.


    player.addperk 000be127 : adds the Alchemist 1/5 perkPotions and poisons you make are 20% stronger.

    or

    player.addperk 000be127 : adds the Alchemist 1/5 perkPotions and poisons you make are 20% stronger.

    I can see the cursor change, changed background but when I hover over the text no window pops up.

    The page is located at: http://skyrim.mmo-game.eu/cheats-and-console/ (I have only this one line under Alchemy atm with the span tags, before I convert the whole content and would prefer to not use anchor for it.

    • It seems to not work with the template I am using. List items, etc will no longer be properly aligned. But this post inspired me on how to get started on this and the result is ok.

      • Hi Myrddin,
        nice to see you’re using these tooltips I made. I think they look pretty good now on your site, perhaps they just need some tweaks but you’re on the right track.

        Good luck and thanks for sharing your thoughts with us!

  5. Hi, Catalin Rosu / Red..

    What is ‘margin-left :-127px’ for ?
    If I apply this tooltip in the first part of a line, then the left part of the baloon is cropped and gone.
    I’ve found that I need to increase the value of ‘margin-left’ to around -80px..
    but I’m not sure with this solution since I’m just a newbie in CSS.

    Need your advice, Red

    :)

    many thanks

    • Hey Alvin, here’s the explanation:

      .tooltip span {
        width: 230px;
        position: absolute;
        left: 50%;
        margin-left: -127px;
        padding: 10px;
        border: 2px solid #ccc;
      }
      

      According to the CSS box model, the total computed width for the .tooltip span will be:

      width + left padding + right padding + left border + right border = 230+10+10+2+2 = 254px (in our case).

      Now, while .tooltip span is basically the content wrapper for the tooltip, we need to center it horizontally. This can be done by using absolute positioning, set left/right value to 50%, and then set margin-left/right to half of the total width (254px/2=127px).

      Hope that helps!

  6. I cannot get this (or any other similar CSS3 tooltips) to work properly. For some reason, having position: relative on the anchor makes the tooltips not show up. The span is there and positioned properly, but it’s invisible. On the surface, it appears to be a stacking order issue, except that the tooltip shows up (in the wrong place, but at least it’s visible) when I remove position:relative from the anchor. However, that (naturally) causes other problems. Any idea why this might happen? I’ve played with stacking orders and positioning on containing elements with no luck.

    • Kim, I’d suggest you to start with a clean HTML template. Then, just add your styles progressively and see what breaks your tooltips.

      • I’ve already fixed this, actually, but thanks! Turns out I had an overflow: hidden set by a sprite generator’s code that I had forgotten to delete. Works nicely now, thanks for the great tutorial!

  7. Hi Red,

    Really like the implementation here. I’m interested in using this on an HTML document in Safari on an iPad. The tooltips appear OK, but there is no way of triggering a mouseout event to make them disappear again in your example. Do you know how we could do this?

    • Hi Ed, I’m glad you like my tutorial.

      Regarding your question, I’d suggest you using jQuery’s event.stopPropagation() after you initially hide the tooltips when clicking on the document.

      Something like that:

      $("html").click(function(){
        $(".tooltip span").hide();
      });
      
      $(".tooltip").click(function(event){
        event.stopPropagation();
      });    
      

      Hope that helps!

  8. Small CSS modification that adds an animation:
    body
    {
    color: #222;
    font-family: ‘Lucida sans’, Arial, Helvetica;
    width: 600px;
    margin: 50px auto;
    font-size: small;
    line-height: 1.4;
    }

    .tooltip
    {
    position: relative;
    background: #eaeaea;
    cursor: help;
    display: inline-block;
    text-decoration: none;
    color: #222;
    outline: none;
    }

    .tooltip span
    {
    opacity: 0;
    left: 50%;
    position: absolute;
    z-index: 999;
    width: 230px;
    margin-left: -127px;
    padding: 10px;
    border: 2px solid #ccc;
    background-color: #ddd;
    background-image: -webkit-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
    background-image: -moz-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
    background-image: -ms-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
    background-image: -o-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
    background-image: linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));
    -moz-border-radius: 4px;
    border-radius: 4px;
    -moz-box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
    box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;
    text-shadow: 0 1px 0 rgba(255,255,255,.4);
    transition: opacity 1s, bottom 1s;
    -moz-transition: opacity 1s, bottom 1s; /* Firefox 4 */
    -webkit-transition: opacity 1s, bottom 1s; /* Safari and Chrome */
    -o-transition: opacity 1s, bottom 1s; /* Opera */
    }

    .tooltip:hover
    {
    border: 0; /* IE6 fix */
    }

    .tooltip:hover span
    {
    opacity: .9;
    bottom: 30px;
    }

    .tooltip span:before,
    .tooltip span:after
    {
    content: “”;
    position: absolute;
    z-index: 1000;
    bottom: -7px;
    left: 50%;
    margin-left: -8px;
    border-top: 8px solid #ddd;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-bottom: 0;
    }

    .tooltip span:before
    {
    border-top-color: #ccc;
    bottom: -8px;
    }

    /* Yellow */

    .yellow-tooltip span
    {

    border-color: #e1ca82;
    background-color: #ffeaa6;
    }

    .yellow-tooltip span:after
    {
    border-top-color: #ffeaa6;
    }

    .yellow-tooltip span:before
    {
    border-top-color: #e1ca82;
    }

    /* Navy */

    .navy-tooltip span
    {
    color: #fff;
    text-shadow: 0 1px 0 #000;
    border-color: #161a1f;
    background-color: #1e2227;
    }

    .navy-tooltip span:after
    {
    border-top-color: #1e2227;
    }

    .navy-tooltip span:before
    {
    border-top-color: #161a1f;
    }

    /* Blue */

    .blue-tooltip span
    {
    border-color: #59add4;
    background-color: #61bde7;
    }

    .blue-tooltip span:after
    {
    border-top-color: #61bde7;
    }

    .blue-tooltip span:before
    {
    border-top-color: #59add4;
    }

    /* Pink */

    .pink-tooltip span
    {
    border-color: #ce4378;
    background-color: #ea4c88;
    }

    .pink-tooltip span:after
    {
    border-top-color: #ea4c88;
    }

    .pink-tooltip span:before
    {
    border-top-color: #ce4378;
    }

  9. Hello, nice work, but how can I close a tooltip an the IPad? Is it possible to add a close button? Or close it by clicking on the Tooltip itself?

    Thanks :-)

    • Hi Tim,

      Tooltips were always tricky on iOS, as they are triggered on hover. As you already know, there’s no hover on iPad.

      Perhaps a quick solution would be just to hide them (using JavaScript) for iOS?

      You can check my CSS3 animated dropdown menu to see an example related to targeting specific devices.

      • I think JQuery ist a bit to heavy for simply unhide it by clickin on the Tooltip. This simple function don’t need to be enabled for specific diveces, because it don’t influence the PC-user, its just on top. I will see what I can do… Thank you very much

  10. very well written tutorial. I tried and it does work beautifully. My dilemma is – It does not work on ipad…any suggestion on how I can get this to work on Ipad?

  11. Another great tutorial Red!

    This is great for text!

    How would you apply the same technique using an image with hotspots???

    Would really appreciate your help!

  12. I´m 16 years old, and i´m trying to be like you;but more taller an dwith less hair
    Anyway, I would like to thank you for your job!
    Portugal is with you!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>