Grains Of Sand Web Design, CSS Demos & HTML5 Templates by tupence

CSS3 Demos - Tooltip Styling

Use only CSS to create styled tooltips for links or anything else you need them for

What We're Aiming For

Hover over the link to see the tooltip. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer accumsan urna ac metus accumsan semper. Suspendisse ut nunc nisl. Pellentesque facilisis, risus in mollis rutrum, lectus dui iaculis velit, quis ultricies sapien arcu in ante. Pellentesque tristique blandit tincidunt. Suspendisse potenti. Donec in faucibus ligula. In blandit facilisis erat, et lacinia libero pellentesque non. Suspendisse placerat sodales tempor. Aenean cursus lorem sed sapien dictum quis convallis urna ultrices. Sed aliquet pulvinar ante, a fermentum ante vulputate sit amet. Donec ut odio in nisi feugiat ultricies at sit amet est. Donec eget vestibulum nulla. Cras eleifend sollicitudin nibh dictum luctus. Sed convallis ipsum id metus dictum suscipit.

This is what you should see...
CSS tooltip

Step By Step

Step One
Add the link and tooltip text and add basic styling to the link

HTML
  • /*The Full HTML*/
  • <p><a class="tooltip" href="#" tip="This is a link to somewhere cool, and the tooltip gives more info about that cool place...">Hover over the link to see the tooltip</a>. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer accumsan urna ac metus accumsan semper. Suspendisse ut nunc nisl. Pellentesque facilisis, risus in mollis rutrum, lectus dui iaculis velit, quis ultricies sapien arcu in ante. Pellentesque tristique blandit tincidunt. Suspendisse potenti. Donec in faucibus ligula. In blandit facilisis erat, et lacinia libero pellentesque non. Suspendisse placerat sodales tempor. Aenean cursus lorem sed sapien dictum quis convallis urna ultrices. Sed aliquet pulvinar ante, a fermentum ante vulputate sit amet. Donec ut odio in nisi feugiat ultricies at sit amet est. Donec eget vestibulum nulla. Cras eleifend sollicitudin nibh dictum luctus. Sed convallis ipsum id metus dictum suscipit.</p>
  • /*The Deconstructed Link*/
  • <a
  • class="tooltip" /*the class will be used to style the tooltip*/
  • href="http://www.someplace.com" /*target of the link*/
  • tip="This is a link to somewhere cool, and the tooltip gives more info about that cool place..."> /*IMPORTANT - this is the text that will appear in the tooltip. I've added an attribute called tip to hold the text. You could use the title attribute, but many browsers display the content of 'title' when a link is hovered so that would make the tooltip redundant - see the image in the notes at the foot of the page*/
  • Hover over the link to see the tooltip /*link text*/
  • </a>
CSS FOR THIS STEP
  • a.tooltip{
  • position:relative; /*ensures that the tooltip is positioned relative to the link rather than the page - see the support section below for more info*/
    }
  • a.tooltip:hover{
  • text-decoration:none; /*required or IE will underline the text in the tooltip*/
  • }

Result

When the link is hovered over, the underline is removed but nothing else happens...


Step Two
Style the tooltip and make it appear when the link is hovered over

HTML
  • same as step one
CSS FOR THIS STEP
  • /****CREATE THE TOOLTIP AND HIDE IT UNTIL THE LINK IS HOVERED OVER***/
  • a.tooltip:after{ /*see http://cssdemos.tupence.co.uk/before.htm for more info on :after and :before*/
  • /***Add and style the tooltip text***/
  • content: attr(tip); /*will display the content of the 'tip' attribute set in the html above*/
  • font-family:Arial, Helvetica, sans-serif;
  • font-size:90%;
  • line-height:1.2em;
  • color:#fff;
  • /***Position the tooltip***/
  • position:absolute; /*see the support section below for more info*/
  • top:27px; /*place the tooltip 27px below the top of the link*/
  • left:-10px; /*place the tooltip -10px from the left side of the link*/
  • /***Create and style the tooltip box***/
  • width:200px; /*how wide you want the tooltip to be*/
  • padding:5px 10px;
  • /*add curved corners*/
  • -moz-border-radius:6px;
  • -webkit-border-radius:6px;
  • border-radius:6px;
  • /*add a gradient background*/
  • background:#c00d3f; /*fallback colour for unsupporting browsers*/
  • background:-webkit-linear-gradient(#c00d3f, #7b0a2a);
  • background:-moz-linear-gradient(#c00d3f, #7b0a2a);
  • background:-o-linear-gradient(#c00d3f, #7b0a2a);
  • background:-ms-linear-gradient(#c00d3f, #7b0a2a);
  • background:linear-gradient(#c00d3f, #7b0a2a);
  • /*add a drop shadow*/
  • -moz-box-shadow: 3px 3px 4px rgba(0,0,0, .65);
  • -webkit-box-shadow: 3px 3px 4px rgba(0,0,0, .65);
  • box-shadow: 3px 3px 4px rgba(0,0,0, .65);
  • /***Hide the tooltip until it's hovered***/
  • display:none;
  • }
  • /****DISPLAY THE TOOLTIP ON HOVER****/
  • a.tooltip:hover{
  • z-index:1000; /*see the support section below*/
  • position:relative; /*see the support section below for more info*/
  • color:#8325f7;
  • }
  • a.tooltip:hover:after{
  • display:block; /*show the tooltip by changing this from none to block*/
  • }

Result

Now the tooltip bubble appears when the link is hovered over, we just need to add the triangle, which we'll do in the next step...


Step Three
Create the triangle

HTML
  • same as step one
CSS FOR THIS STEP
  • /***CREATE AND HIDE THE TRIANGE***/
  • a.tooltip:before{ /*see http://cssdemos.tupence.co.uk/before.htm for more info on :after and :before*/
  • z-index:1000; /*see the support section below*/
  • position:absolute; /*see the support section below*/
  • content:""; /*because there's nothing between the "" the content of the psuedo elements will be blank*/
  • top:15px; /*places the triangle 15px from the top of the link*/
  • left:0px;
  • /*we want the triangle to be 14px wide and high. To do this we'll use borders - the bottom will be 14px and the colour of the triangle; the left and right will be transparent and 7px each. This creates a square 14px by 14px, by setting the left and right border as transparent we remove the sides to create the triangle*/
  • border-right:7px transparent solid;
  • border-bottom:14px #c00d3f solid; /*same colour as the top background gradient for the tooltip*/
  • border-left:7px transparent solid;
  • /*hide the triangle until the link is hovered*/
  • display:none;
  • }
  • /***DISPLAY THE TRIANGLE WHEN THE LINK IS HOVERED OVER***/
  • a.tooltip:hover:before{
  • display:block;
  • }

Result

Now you should be able to see the tooltip and the triangle when the link is hovered over...
CSS tooltip

Full Code For You To Copy And Paste

HTML
  • <p><a class="tooltip" href="#" tip="This is a link to somewhere cool, and the tooltip gives more info about that cool place...">Hover over the link to see the tooltip</a>. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer accumsan urna ac metus accumsan semper. Suspendisse ut nunc nisl. Pellentesque facilisis, risus in mollis rutrum, lectus dui iaculis velit, quis ultricies sapien arcu in ante. Pellentesque tristique blandit tincidunt. Suspendisse potenti. Donec in faucibus ligula. In blandit facilisis erat, et lacinia libero pellentesque non. Suspendisse placerat sodales tempor. Aenean cursus lorem sed sapien dictum quis convallis urna ultrices. Sed aliquet pulvinar ante, a fermentum ante vulputate sit amet. Donec ut odio in nisi feugiat ultricies at sit amet est. Donec eget vestibulum nulla. Cras eleifend sollicitudin nibh dictum luctus. Sed convallis ipsum id metus dictum suscipit.</p>
CSS
  • a.tooltip{
    position:relative;
    }
  • a.tooltip:hover{
    text-decoration:none;
    }
  • a.tooltip:after{
    content: attr(tip);
    font-family:Arial, Helvetica, sans-serif;
    font-size:90%;
    line-height:1.2em;
    color:#fff;
    width:200px;
    padding:5px 10px;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
    background:#c00d3f;
    background:-webkit-linear-gradient(#c00d3f, #7b0a2a);
    background:-moz-linear-gradient(#c00d3f, #7b0a2a);
    background:-o-linear-gradient(#c00d3f, #7b0a2a);
    background:-ms-linear-gradient(#c00d3f, #7b0a2a);
    background:linear-gradient(#c00d3f, #7b0a2a);
    -moz-box-shadow: 3px 3px 4px rgba(0,0,0, .65);
    -webkit-box-shadow: 3px 3px 4px rgba(0,0,0, .65);
    box-shadow: 3px 3px 4px rgba(0,0,0, .65);
    position:absolute;
    top:27px;
    left:-10px;
    display:none;
    }
  • a.tooltip:before{
    z-index:1000;
    position:absolute;
    content:"";
    top:15px;
    left:0px;
    border-right:7px transparent solid;
    border-left:7px transparent solid;
    display:none;
    }
  • a.tooltip:hover{
    z-index:1000;
    position:relative;
    color:#8325f7;
    }
  • a.tooltip:hover:after{
    display:block;
    }
  • a.tooltip:hover:before{
    display:block;
    }

Support Section

Explanations Of The CSS3 Used In The Demo

Postioning

The demo uses postition:absolute together with position:relative, so here's a brief explanation...
position:relative -- lets you position an object relative to its normal position, using top, bottom, left and right
position:absolute -- removes an object from the normal flow and positions it exactly where you say on the page
position:relative with position:absolute -- lets you position a child element inside its parent. If you don't first use position:relative on the parent element, the absolutely positioned child will move outside the parent.

See a further explanation and example of using relative and absolute positioning together.

z-index

z-index specifies the stack order of an element within the document. Elements with a higher z-index will appear infront of those with a lower z-index.
Note that it only works on positioned objects.

Why Not Use The Title Attribute?

You could use the title attribute within the link to hold the tooltip text, but browsers already display the content of title when a link is hovered over, which would make the styled tooltip redundant, as demonstrated in this screencap
Tooltip demo using the title attribute

Browser Support

The demo works in all the latest browsers, including IE8, IE9 and IE10 (although the gradients and border-radius don't work in IE8, even with CS3Pie).

More CSS3 Demos This Way