Patrick Burt - A Blog for Web People

How To Do Easy Tooltip Popups

Friday, February 8th, 2008

Categories: Web Development

RSS Comment Feed

Trackback

AddThis Social Bookmark Button

This was something I thought of doing (originally, I was trying to make it CSS only, but I ran into problems with IE), so I figured why not make an extra easy to understand, good looking CSS/Javascript tooltip.

What we’re going to be making is a popup tooltip (or help box) that pops up once you over an icon, (in this case, a question mark). The method that we will look over validates for both CSS and HTML.

Demo

Click here to see a demo.

Stylesheet

The CSS is pretty straight-forward. Once you put this on your site, you can place as many tooltips as you like on your website, and you can style them with one chunk of CSS. I’ve taken the liberty of adding comments in the CSS to explain some of the attributes. Unless I say otherwise, these attributes are required.

.questionMark {
position:relative;
height:26px; /*dimensions of our image*/
width:26px;
background:url(/wp-content/questionMark.gif) top left no-repeat;
}
.toolTip {
position:absolute;
left:26px; /*Moves it to the right beside the question mark*/
top:0;
display:none;
/*The attributes below make it look pretty*/
width:100px;
padding:5px;
border:1px solid #ffffff;
background-color:#eeeeee;
font:10px/12px Arial, Helvetica, sans-serif;
}

That seems easy doesn’t it? Let’s jump into the HTML (and Javascript).

Code

<div onmouseover=”document.getElementById(’tt1′).style.display=’block’” onmouseout=”document.getElementById(’tt1′).style.display=’none’” class=”questionMark”>
<div id=”tt1″ class=”toolTip”>Eat your vegetables. Brush Your Teeth</div>
</div>

As you can see, the html code is a little more complicated, but I’ll explain.

What we have is our question mark div container (class=questionMark), within it, we have our tooltip (class=toolTip). I created some Javascript that looks for the id name of the tooltip: id=tt1, and makes it visible when the question mark is moused over, of course, when the mouse leaves the question mark, the tooltip is made invisible.

In terms of scalability, since Javascript requires a unique id (or class) name to do this, you’d only have to increment the value after the “tt” (eg. tt2, tt3, tt4…). The style will be retained so across the board updates should be easy.

Hope that helps. :)

Bookmark this blog using any bookmark manager!

Cartier-118195

Related Posts


Subscribe to this Post

7 Responses to “How To Do Easy Tooltip Popups”

  1. Mat Says:

    Hi,
    thanks for making things so clear and easy.
    I changed the code of your example so that the tooltip contains text and images, and is opened by the “onClick” event. Now I would like to add a link inside the tooltip, in order to close it (instead of the “onmouseout” event)… Is it possible ? (I’m a beginner in javascript!)

    Thanks in advance
    Mathieu

  2. Patrick Burt Says:

    Hi Mat,

    Thanks for the comment.
    Take out the onmouseout event.
    Create a link. and put the onmouseout event in an onClick event in the A tag. :)

    CLOSE ME

  3. Mat Says:

    well, I must do something wrong… it works when the link is outside the tooltip, but not when inside )-:
    Here’s my code :

    Eat your vegetables. Brush Your Teeth
    CLOSE ME

    Is there something I can change ?

    Thanks again.

  4. Patrick Burt Says:

    That won’t work. In your browser, do View Source, and look at me Close Me link’s code.

  5. Dating Says:

    Is there a way to create the pop-up without using CSS? In other words, to keep the code right on the HTML page?

    Also, if I already have a CSS style sheet, would i be taking the code you prescribe and enter it in there next to the other style info?

    Thanks!

  6. Patrick Burt Says:

    Yeah, you can stick the CSS in the header of the page.
    The answer to the second question would be yes. :P

  7. Speed Dating NYC Says:

    Hey Patrick,

    Thanks very much for the help. I’ll be using the pop-up tool tip very soon. I already implemented it, I am just not finished with the design of the page I am working on. Apart from tooltips and widgets, do you have any suggestions for other cool things i can add to my page?

Leave a Reply