Tech, Tips, and Tutorials.
Reviews, News, and Rants.

Cool CSS Tooltips

If you’re reading this from an RSS feed, you’ll need to switch over to the web page (the CSS is in the head section, and thus isn’t loaded by an RSS reader)

In this tutorial you’ll learn how to make some really clever CSS-based tooltipsAnd it's actually really easy!.

The most basic way to create a tooltip from a link is to use the title attribute in a link. This small bit of text is displayed in a small box.

A tooltip is often used to explain outgoing links. A simple, unobtrusive tooltip is often all we need to use in our site design.

However what if we wanted to explain something further, or we wanted to add some formatting to our tooltip? Browsers differ in the way they display tooltips, and they differ even further if they spread across multiple lines.

Note that this tutorial assumes that you have a basic understanding of HTML and CSS languages.

Using the span tag to hide text

  1. First thing you have to do is remove any extra title attributes you may have on your link, so that it doesn’t interfere with our CSS tooltip.
  2. Insert the tooltip right after the link text, and enclose it in a span tag, like so: HTML
    <p>
        Check
        <a href="#">this <span>This is tooltip text!<span></a>
        out!
    </p>
  3. Now we need to style this. We should first hide the text inside the span tag:CSS
    a span {
        display: none;
    }
  4. By using the pseudo-class :hover, we can make sure that the text will show only if the user hovers the mouse at our link. We are basically telling the browser: “Change the display from none to block when the user hovers over a link”. Our CSS should now look like this: CSS
    a span {
        display: none;
    }
    a:hover span {
        display: block;
    }
  5. If you try to run your code now, you’ll notice some serious problems. The span element will act like a block and therefore push everything after it downwards!

    How are we going to get the span to appear without interrupting the page flow? We must use absolute positioning to get this. This way, it will have no effect on other elements, except for covering part of them. I’ve also applied some basic styling below to make it look more like a tooltip: a:hover span {
        position: absolute;
        display: block;

        /* styling */
        background-color: #323232;
        color: #FFFFFF;
    }
  6. Even though it’s looking more and more like a tooltip, right away you’ll notice some positioning woes. This is because of the absolute setting that we had to use before.

    To position it properly, we have to turn the link itself in to an anchor that the absolutely positioned span element can work from. To do that we have to set relative positioning on the link.

    In addition, we should also add some absolute positioning by specifying the top and left attributes. Also, we need to ensure that it’s always on the top. To do this we need to add a z-index attribute.

    This is how our complete CSS should look (syntax highlighting and all :)
    CSS
    a {
        position: relative;
    }
    a span {
        display: none;
    }
    a:hover span {
        position: absolute;
        display: block;

        /* positioning */
        top: 30px;
        left: 30px;
        z-index: 100;

        /* styling */
        background-color: #323232;
        color: #FFFFFF;
    }

Extending the concept

This hover/popup method is very powerful and can be extended towards all sorts of things. You can apply certain “tooltip” classes and even apply some HTML on the tooltip. For example, you can have an image gallery, and a larger version of the image pops up in the tooltip. Remember that they don’t even have to be linksSince you don't specifically need the link tag (hover your mouse over the bold text!)

The original concept for the hover/popup CSS method was first shown on Eric Meyer’s website. Thanks, Eric!

Possibly Related Articles (automatically generated)



6 comments:

Refriedchinese said...

hey how do i resize the tool tip?

Simon SC said...

Refriedchinese:

You can always add some size attributes to the a span property like so:

a span {
    display: none;
    width: 50px;
    height: 25px
}

Hope this helps!

refriedchinese said...

hey it does help...now how bout if i don't want to use this



i don't want to use the a href="#">because it will refer to my links, is there any other way to change this?

Simon SC said...

Refriedchinese:

Yes there is a way you can do this. You don't specifically need the <a> tag to make this work.

Look at the source code of the one-before-last paragraph; the bold text:

<span class="tip"><strong>Remember that they don't even have to be links</strong><span>Since you don't specifically need the link tag</span></span> (hover your mouse over the bold text!)

You could then style some CSS the following way:

.tip {
    display:none;
}

.tip:hover {
    top: 30px;
    left: 30px;
    z-index: 100;

    background-color: #323232;
    color: #FFFFFF;
}

Refriedchinese said...

Thanks a lot that was helpful!

Anonymous said...

Hi.Is it possible to get to tooltip content some link? for example: word

Post a Comment