1. Web Design
  2. Site Elements

How to Create Script-Free CSS3 Tooltips


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.

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.

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.

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

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

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.

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.

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.

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.

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

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!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.