Pure CSS tooltip

October 3rd, 2007

Tag:


Today I needed a tooltipsolely based on CSS solely based on CSS, because Javascript-based tooltips would have required enabling javascripting abilities for users. So here is what I did.

First, I created a little css file called tooltip.css with the following content

CSS:
  1. /* Usage:
  2. <a class="tooltip" href="#">SOME TEXT<span>TOOLTIP TEXT</span></a>
  3. */
  4.  
  5. a.tooltip {
  6.     border-bottom: 1px dashed brown;
  7.     text-decoration: none;
  8. }
  9.  
  10. a.tooltip:hover {
  11.     position: relative;
  12. }
  13.  
  14. a.tooltip span {
  15.     display: none;
  16. }
  17.  
  18. a.tooltip:hover span {
  19.     display: block;
  20.     min-width: 10em;
  21.     max-width: 15em;
  22.     position: absolute; top: 10px; left: 0;
  23.     /* formatting only styles */
  24.     padding: 5px; margin: 10px; z-index: 100;
  25.     background: #f0f0f0; border: 1px dotted #c0c0c0;
  26.     opacity: 0.9;
  27.     /* end formatting */;
  28. }

Then I linked this file into the html file where the tooltips should be used

HTML:
  1. <style type="text/css">/PATH/TO//tooltip.css</style>

Finally we use it

HTML:
  1. <a class="tooltip" href="#">SOME TEXT<span>TOOLTIP TEXT</span></a>

Example: Lorem ipsum dolor sit amet, consectetur adipisicing elitt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat., sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Source: CSS Tooltips - Floating HTML Elements by Gabe

Leave a Reply