Get $500+ of the best After Effects files, video templates and music for only $20!
How to Create Script-Free CSS3 Tooltips

How to Create Script-Free CSS3 Tooltips

Tutorial Details
  • Languages: HTML/CSS
  • Difficulty: Intermediate
  • Estimated Completion Time: 20min

Final Product What You'll Be Creating

Ahhh… tooltips. They can be the answer to many of your support worries or if you just want to give a little hint to the user. Today there are tons of websites and applications out there using tooltips but… is there a better way to implement them? I’m excited to say that there is a better way, a Javascript free way… well for the most part. We’re going to be making some really awesome tooltips out of pure CSS3 and then we’re going to create a mobile friendly version, and this is where it gets a little hazy because to get this to work, we have to use a little Javascript fix known as none other than Dean Edwards IE9.js.

Note: There is full IE support for versions 8 and up. IE7 is also supported except for the mobile version (clickable version).


The Video Tutorial

For everyone who wants a peek into how Justin creates this in living color, check out the video below!


The Written Tutorial

And now, the full written step by step guide with fully coded instructions. Be sure to grab the source file download from up above as well!


Step 1 Creating the Markup

In the Head

Let’s start by creating the head of our document and then insert a stylsheet specifically for this tutorial. You can delete the tut.css stylesheet when implementing this in your own project, it is used only for this tutorial.

We’ve used the HTML5 doctype which is !DOCTYPE html and then called our two stylesheets. The first stylesheet is the transparency version but there are more themes to choose from in the source files. The second on is the tut.css we talked about and is only used for the purposes of styling this tutorial, but feel free to use it if you like.

After the stylesheets, is our less than IE9 statement. We’ve used an ie.css file which calls on CSSPIE telling IE 7 & 8 that it’s ok to use CSS3 features because they’re awesome! Then we called on Dean Edwards IE9.js to further tell IE7 & 8 that we need to make this work by using advanced CSS selectors.

    
    
    



    

    

    

    
    

“The user will be able to move their mouse from your the element to the tooltip in order to click on links.”

The Websafe Tooltip

Now that we have our head created, the next thing will be to create the markup for our first tooltip. For this tutorial we’re going to use an unordered list but you can use anything because in the CSS we only used the class name instead of selecting the list itself making it possible to simply insert the classes anywhere you like.

The tip class (ie: li class=”tip”) is used for the text or image that will be hovered over to show the tooltip. Inside of that we’ve added a link with a hash and the hash is so that our link is accessible.

After the tip class is the tooltipL class (ie: strong class=”tooltipL”) which is used for any tooltips that will show to the left of your hover element, in this case the first link.

It’s even possible to add images and links inside of your tooltip. The user will be able to move their mouse from the hover element to the tooltip in order to click on links; this functionality is taken care of in the stylehseet which we’ll discuss later in this tutorial.

Following the example above will be the rest of the tooltips we’ll be creating. Essentially everything will be the same for each tooltip. The only differences or the classes. Instead of tooltipL, we have .tooltipR for all tooltips placed to the right of the hover element, .tooltipB which is placed at the bottom and .tooltipT which is placed at the top. Any time you want to add a new tooltip to your project, all you have to do is use one of these classes depending on where you would like the tooltip to show.

  • Right TipAdd images in the tooltip. You can even add links!
  • Bottom TipAdd images in the tooltip. You can even add links!
  • Top TipAdd images in the tooltip. You can even add links!
  • The Mobile Friendly Toolitp

    The main difference between this type of tooltip and the websafe version is that we use a different class (ie: .tipClick) and simply make it clickable. On mobile phones you can’t hover over something like a tooltip to make it appear so this version solves that issue. It’s also great for action oriented content.

    
    

    That’s all there is too the HTML markup. Now let’s take a look at some CSS to make this baby functional!


    Step 2 Making it Functional & Creating Styles with CSS

    For this tutorial we are going to use the tip-transparency.css style but once again, there are more styles to choose from so pick whichever you like to follow along.

    To start off we are going to add some styles to our main classes.
    Here we add a position of relative and a display of inline-block so that the tooltip stays inline with the hover element.

        .tip, .tipClick{ /* This is any element that has a tooltip attached too it */
            position:relative !important;
            display:inline-block;
        }
    

    Next we want to hide all of the tooltips until they are either hovered over or clicked on. To do that we’ll simply select the class for each direction and use a display: none

        .tip > .tooltipL,
        .tip > .tooltipB,
        .tip > .tooltipR,
        .tip > .tooltipT,
        .tipClick > .tooltipL,
        .tipClick > .tooltipB,
        .tipClick > .tooltipR,
        .tipClick > .tooltipT{ /* Used to hide the tooltip */
            display:none;
        }
    

    To show the tooltip when hovered or clicked on, we’ll do the exact opposite as hiding it and use a display: block after selecting the class for each direction.
    For the clickable tooltips we’re going to add a focus to the hovered element (ie: the first link) and then we’ll immediately match the preceeding tooltip by using the plus selector (ie: a:focus + .tooltipL).

        .tip:hover > .tooltipL,
        .tip:hover > .tooltipB,
        .tip:hover > .tooltipR,
        .tip:hover > .tooltipT, /* This shows the tooltip when an elements with the .tip class is hovered over */
        .tipClick > a:focus + .tooltipL,
        .tipClick > a:focus + .tooltipB,
        .tipClick > a:focus + .tooltipR,
        .tipClick > a:focus + .tooltipT{ /* This shows the tooltip once an element with the .tipClick class is clicked */
            display:block;
        }
    

    Now that we have a workable tooltip, let’s go ahead and style them. For the most part, each tooltip whether left, right, top or bottom will be the same except for a few differences which I will explain later.

    As you can see we want each tooltip to look the same, so they have common backgrounds using an 85% opacity. There is a box shadow to add a little bit of depth, along with solid border to separate the inner-content.
    A few important facts here are… the min-width:200px to keep the arrow inline with the hover element, a postion of absolute so that we can show the tooltip without affecting any of its surrounding elements, and a z-index of 10 so that the tooltip is always on top.

        .tooltipL, .tooltipR, .tooltipB, .tooltipT{
            background: #333333; /* old browsers */
            background: rgba(0,0,0,0.85); /* For IE9 */
            background: -moz-linear-gradient(top, rgba(51,51,51,0.85) 0%, rgba(0,0,0,0.85) 100%); /* firefox */
            background: linear-gradient(top, rgba(51,51,51,0.85) 0%, rgba(0,0,0,0.85) 100%); /* future browsers */
            -pie-background: linear-gradient(top, rgba(51,51,51,0.25) 0%, rgba(0,0,0,0.25) 100%); /* pie for less than ie9 */
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(51,51,51,0.85)), color-stop(100%,rgba(0,0,0,0.85))); /* webkit */
            -moz-box-shadow: 0 0 6px 1px #666666;
            -webkit-box-shadow: 0 0 6px 1px #666666;
            box-shadow: 0 0 6px 1px #666666;
            border:solid #FFFFFF;
            color: #E7E7E7;
            min-width:200px;
            padding: 5px;
            position: absolute;
            text-align: center;
            z-index: 10;
        }
    

    So for the differences in each tooltip is where they are positioned relative to the hover element. These values seemed to work the best and should not have to be changed, however if they do need to be changed to fit with your project then the best solution would be to use a dynamic repositioning using javascript… but that is beyond the scope of this tutorial.

    By looking at the CSS code below you can see that we have to repostition the tooltips so that they are inline with their respective hover element, whereas using a relative position on the main classes kept the tooltips contained inside the hover element, it does not position them next to it.

        .tooltipL{ /* The styling on the tooltip */
        left: -226px;
        margin-top: -28px;
    	}
    
        .tooltipB{ /* The styling on the tooltip */
        top: 30px;
    	left: -183px;
    	}
    
        .tooltipR{ /* The styling on the tooltip */
        right: -226px;
        margin-top: -28px;
    	}
    
        .tooltipT{ /* The styling on the tooltip */
        bottom:26px;
        left: -186px;
    	}
    

    Almost finished! Now we need to create the arrow for our tooltip which will point towards the hover element. Again, there are a few differences which I will show you later.

    So looking at the code below, we will insert the arrow “before” each tooltip. What’s important here and why we can make an arrow using CSS is the “content” declaration. We’ll add a position of absolute so that we can position the arrow however we want. Put a display: block and a height/width of 0, along with a solid 10 pixel border which will act as our actual arrow.

        .tooltipL:before, .tooltipR:before, .tooltipB:before, .tooltipT:before{
            border-style: solid;
            border-width: 10px;
            height:0px;
            width:0px;
            position:absolute;
            display:block;
            content:"\00a0";
        }
    

    The difference here is what direction we want the arrow to be pointing and for that we use a solid background for one side and a transparent background for the rest. Also we have to move each one down from the top and to the right and bump the arrow flush with the tooltip so that it looks like on element.

        .tooltipL:before{
            border-color: transparent transparent transparent #333333; /* Right facing arrow */
            top:5px;
            right:-20px;
        }
    
        .tooltipB:before{
            border-color: transparent transparent #333333 transparent; /* Top facing arrow */
            right:5px;
            top:-20px;
        }
    
        .tooltipR:before{
            border-color: transparent #333333 transparent transparent; /* Left facing arrow */
            top:5px;
            left:-20px;
        }
    
        .tooltipT:before{
            border-color: #333333 transparent transparent transparent; /* Bottom facing arrow */
            right:5px;
            bottom:-20px;
        }
    

    After you’ve completed that, you’re all done, and the final result should look something like the image below. If it doesn’t look like the image, please go back and check your code… you may have missed something… probably something small.

    In order to make yours look exactly like the tut, we need to use tut.css and that code should look like this…

        body{background: url(images/maintenance/bg.png) repeat-x #252C2E;}
    
    	a{color:#FFE25F;}
    
    	a:hover{color:#CFE25F;}
    
    	img{border:none;}
    
    	li.tip, li.tipClick {margin-right:30px; margin-left:30px; display:inline;}
    
    	.tip > a, .tipClick > a{font-size:18px;}
    
    	h2{color:#d2d2d2; text-align:center; font-size:36px; position:relative;}
    
    	ul > h2{color:#999;}
    
    	ul{margin: 0 auto;position: relative;text-align: center;top: 130px;width: 960px;}
    
    	li.tip > a, li.tipClick > a{color:#CCC;}
    
    	img.ref{margin:0 auto; display:block;}
    

    All Done!

    Congrats! You’re now finished making some really awesome tooltips without using a lick of javascript except to make IE 7 & 8 behave. I sincerely hope you enjoyed this tutorial and that you were able to learn something valuable.

    Remeber, be patient and keep soaking up as much knowledge as you can and before you know it you’ll be a master at whatever you do!

    Justin Hubbard is elion on Codecanyon
    Add Comment

    Discussion 17 Comments

    1. Really nice! but the clickable ones don’t work. I use Chrome in it’s last version =)

      • Kenneth Powers says:

        Same thing here. Chrome 12.0.742.112 on Windows 7. They also don’t work on my Droid Incredible running Android 2.3.4 or my Asus EEE Transformer running Android 3.1.

        The hover tips work in all three places, however. I just click the hover spots on the mobile devices and the tips stay open.

      • Author

        Thank you for taking part in this tutorial… I’m glad you enjoyed it! ;)

        Please go back and check your code and see if you may have missed something as this has been tested in all browsers. It’s probably something really simple.

        • Matt says:

          The clickable tooltips do not work in the latest version of Chrome in the version provided by nettuts.

        • Liz says:

          Nope, the clickable tooltips in your demo version doesn’t work for me either (using OS X Chrome). I think the problem is on your end, mate.

      • Bruno Mateus says:

        Me too… Works perfectly on Firefox 4 and 5.

        IE 8 gives me a 403 error.

        Safari and Chrome work, but not the clickable links.

    2. I will try it for one of my project. :)

      The clickable tooltips don’t work with Safari.

    3. Michał Czernow says:

      Also there are some problems in Opera (newest). Left and right tip is too low to be hoverable (so if you try to click anything on them, they will disappear). The problem? Well differences between browsers in dealing with positioning inside inline elements. I’d quickly changed display mode for .tip to “inline-block” and it worked perfectly.

    4. Techeese says:

      great tutorial to add in my collection but as usual the class has been used

    5. Simon says:

      Can confirm the clickable ones do not work in the latest Chrome or Safari (on windows)

      No problem with the hover ones though (strange)

    6. Carlos Cessa says:

      Yep also confirmed, the clickable version doesn’t work on chrome, I try it from the demo provided in the tutorial, should check that out.
      But overall great tutorial :D

    7. Cesare says:

      In the demo on this link:

      http://webdesigntutsplus.s3.amazonaws.com/166_CSS3_Tooltips/css-tooltips/css-tooltips/tips.htm

      clickable tooltips do NOT work on Safari and Chrome (MacOsX).

    8. Chris says:

      The problem with Chrome and Safari not working when clicked is that pseudo class :focus is not supported in Webkit.

      Check out the the bug here:

      https://lists.webkit.org/pipermail/webkit-unassigned/2011-May/327772.html

    9. Jeremy says:

      This doesn’t display correctly on my site. I’m using wordpress and I’m using it in a sidebar. It gets cut off I’m assuming because it’s in the sidebar’s div. Halp?

    Add a Comment