These are wonderful for adding some extra information to links - such as the title of the target page, a brief description, etc. However, they can't be styled at all. You're stuck with that bland yellow, and you can't control the width, padding, or font of the tooltip.
Enter some CSS magic. We can use a few nifty CSS attributes to create our own tooltips - completely customizable down to the size, font, color, and location.
Quick CSS Overview
In order to create this tooltip, we need to know about a few CSS attributes.
First the "Display" attribute. This can have several options - block, inline, or none. If you set an element to "display: none" it will not be displayed.
Why would you want an element to be hidden? So that you can only show it under certain circumstances. If you allow something to change the attribute to "display: block" then the element will be displayed. This is how CSS expanding menus work.
Next, we need to know about "a: hover". Anchor elements can have a number of pseudo-classes - hover, visited, and active. By creating an "a: hover" style declaration, the anchor can have different style when the mouse is hovered over the anchor.
Finally, we'll use the "position" attribute. This can be either "position: relative" or "position: absolute". If you give a block-level element "position: absolute" you can place it anywhere on the page and have it display on top of the other elements. It's kind of like a sticky note you throw on top of the rest of the web page.
How Do They Build a Tooltip?
With these three attributes in hand, we can start building our customized tooltips.
The tooltip itself is going to be a block-level element (< span >) with a "position: absolute" setting. This lets the tooltip appear on top of text - creating its own box on top of everything, instead of displacing other text to make room for itself.
The < span > element will start with a "display: none" setting, so that it doesn't appear. Once we trigger it (i.e. mouseover the text), we want the "display" attribute to change to "display: block".
We accomplish this last piece by including the < span > inside an < a > tag. In the style declaration, a < span > inside of an < a > will have "display: none", while a < span > inside of an < a:hover > will have "display: block".
Can I Have Some Code to Copy and Paste?
Here's the basic css style info along with a little HTML to test this out with. Note that all of my tags have extra spaces inside of them so that they display correctly on Associated Content. If you copy and paste this code, remove those extra spaces.
style type="text/css" >
a.info span {
display: none;
}
a.info:hover {
position: relative;
}
a.info:hover span {
display: block;
position: absolute;
border: thin solid black;
background-color: yellow;
}
< /style >
< p >Here's some test text. Here is a bit of text with a
< a class="info" >mouseover tooltip
< span >Ta-da! Tooltip magic! Bla di bla bla.< /span>
< /a >.< /p >Jazzing It Up and Other Final Notes
Granted, this is very basic... but we haven't really styled it yet!
Once you've built the tooltip, you can style it however you wish. A couple things you might want to try out are setting the width of the tooltip (on the "span" declaration), changing the background color, and customizing the margins of the text in the tooltip.
You can also change the position of the tooltip by adding "top:", "left:", "right:", "bottom:", or some combination thereof to the "a.info:hover span" declaration.
A few things to note, as well.
You can add other stuff in the < span > element. For example, you can add < br / > tags to add line breaks. You can add other < a > tags inside. However, the mouse needs to move directly from the hovered anchor into the span block, or else the user will never be able to click on the link - in other words, make sure the tooltip overlaps the link.
Although the tooltip needs to be embedded in an anchor tag, that doesn't mean it needs to be inside a link. Notice how there is no "href=''" attribute in the anchor tag above. You may therefore want to add some special styling to this type of anchor (which I declared as a special class, "info") so that a user knows it is a mouseover but not a regular link.
Finally, the "position: relative" attribute needs to remain inside the "a.info:hover" declaration. Just do it.
Be creative and start styling your own tooltips today.
Published by B. Rock
I'm a recent graduate, a newly wed, and a (no longer first year) teacher. I teach HS Social Studies in a New Jersey city. I graduated from the Rutgers Grad School of Ed in May of 2007. In July '07, I... View profile
- Styling CSS and HTML Unordered Lists
- MillionDollarHosting.net Adds New Features to Its Web Hosting Control Panel
- Evrsoft First Page 2006: Free HTML Editor
- Barbie Collectibles Are Not Just for the Young
- HTML Tutorial #1 - Introduction
- Photoshop Tutorial: How to Create Lightning
- No javascript is required - just perfectly valid HTML and CSS.
- This technique utilizes a few CSS attributes: display, position, and a:hover
- Start with the basic mark-up to create a tooltip, then style to your heart's content





6 Comments
Post a Commentoops! I forgot to remove the styling so it shows up in the previous post
I added a couple of lines to the css
a.info:hover span
display: block;width:250px;
position: absolute;
border: thin solid red;
background-color: #ccffcc;
and I added an image
p>Here's some test text. Here is a bit of text with a
a class="info">mouseover tooltip
span >img src="smpetals.png" alt="Image" border="0" width="60" height="60"
align="absmiddle">font face="arial, helvetica, sans-serif" size="2" color=red
align=middle>
scroll here to see full gallery! br />/span>
/a>./p>
great tut I made this tooltip following your instructions really easyHere's some test text. Here is a bit of text with a
mouseover tooltip
scroll here to see full gallery!
.
< span > is an inline element, not a block level element - though you tell it to display as a block level element in your CSS code
Nice article.
FYI - In order for this to work in IE8 you must have a href for the anchor tag.
You can set the width for each individual element though so I don't see that as a big deal, unless your tooltip content is also dynamic I suppose. Mine isn't so that is fine for me!
What I do have a problem with is anchor is at the bottom of the window, the tooltip appears below it and therefore out of sight. I don't supposed anyone can come up with a simple solution to make the tooltip always appear in a spot where it is visible?
Nice tutorial.
Only thing that bothers me is that you can't have a dynamic width of the tooltip.