Advertisement
Site Elements

How to Create Script-Free CSS3 Tooltips

by

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.

    <!DOCTYPE html>
    <head>
    <meta charset=utf-8 />
    <title>CSS3 Tooltips - Hover & Clickable</title>
    <link rel="stylesheet" href="styles/themes/tip-transparency.css" />
    <link rel="stylesheet" href="styles/tut.css" />
    
    <!--[if lt IE 9]>
    <link rel="stylesheet" href="styles/ie.css" media="screen" />
    <script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
    <![endif]-->
    
    </head>
    
    <body>
    
    </body>
    </html>

"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.

    <ul>
        <li class="tip"><a href="#">Left Tip</a><strong class="tooltipL">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>

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.

    <li class="tip"><a href="#">Right Tip</a><strong class="tooltipR">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>
    
    <li class="tip"><a href="#">Bottom Tip</a><strong class="tooltipB">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>
    
    <li class="tip"><a href="#">Top Tip</a><strong class="tooltipT">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>
    </ul>

The Mobile Friendly Tooltip

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.

    <ul>
        <li class="tipClick"><a href="#">Left Tip</a><strong class="tooltipL">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>
        
        <li class="tipClick"><a href="#">Right Tip</a><strong class="tooltipR">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>
        
        <li class="tipClick"><a href="#">Bottom Tip</a><strong class="tooltipB">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>
        
        <li class="tipClick"><a href="#">Top Tip</a><strong class="tooltipT">Add images in the tooltip. <img class="ref" src="images/profolio.jpg" height="80" width="80" alt="" /><a href="#"> You can even add links!</a></strong></li>
    </ul>

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!

Related Posts
  • Web Design
    Complete Websites
    Building the Responsive Timeline Portfolio PagePortfolio thumb
    During this tutorial we will be building the fantastic Timeline Portfolio as seen in an earlier tutorial by Tomas Laurinavicius. We will be using some responsive techniques as well as CSS3 animations, Sass and a little bit of jQuery.Read More…
  • Web Design
    Case Studies
    How They Did It: Typekit's New HomepageTypekit retina
    Typekit recently redesigned their homepage with some new services in mind. When Typekit joined Adobe, they set out to bring us a new way to handle fonts on the web. Not only did they create a fairly simple way to embed fonts on the web, but they have now officially launched a desktop sync option, which allows Creative Cloud subscribers to sync fonts to their computer directly from Typekit. This has been in a beta form for a while now, and provides a much easier route to local fonts than finding them elsewhere!Read More…
  • Web Design
    UX
    Walk Users Through Your Website With Bootstrap TourTour retina
    When you have a web application which requires some getting used to from your users, a walkthrough of the interface is in order. Creating a walkthrough directly on top of the interface makes things very clear, so that's what we're going to build, using Bootstrap Tour.Read More…
  • Web Design
    Email
    Creating a Simple Responsive HTML EmailEmail new rwd thumb
    In this tutorial I will show you how to create a simple responsive HTML email which will work in every email client, including all the new smartphone mail clients and apps. It uses minimal media queries and a fluid width approach to ensure maximum compatibility.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Creating Template FilesCreating wordpress theme from html 400
    In the first part of this series, I showed you how to prepare your HTML and CSS files for WordPress, ensuring the structure would work, the code was valid and that the correct classes were being used. In this tutorial you'll learn how to take your index.html file and split it up into a set of template files for use by WordPress.Read More…
  • Web Design
    HTML/CSS
    How to Customize the Foundation 4 Top BarFoundation preview
    Zurb's Foundation 4 features a brilliant top bar which has become almost symbolic of a Foundation site build. Today we're going to look at how you can implement it in a different way, placing it elsewhere on the page, giving you a custom and responsive horizontal navigation menu.Read More…