<h1>CSS-only Tooltip</h1>
<p>
  This is just some filler text, but
<span class="tooltip tooltip-bottom" data-tip="yay, tooltip content! provided by your friendly data-* attribute">this thing has a tooltip</span> and here we have more filler text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
<p>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim. This is just some filler text, but
<span class="tooltip" data-tip="yay, bottom tooltip content! provided by your friendly data-* attribute">this thing has a tooltip</span> and here we have more filler text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, <span class="tooltip" data-tip="yay, bottom content! provided by your friendly data-* attribute">quis</span> nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
// CSS-only Tooltip
// work in progress, still needs more testing. So far works well in Chrome, FF, Opera, Safari, IE9-11. 
// IE 8 seems to be ok, just don’t forget fallback colors and visibility: hidden.
// Goals: 
// - Make tooltip work across browsers including IE8
// - Tooltip shows up on either top or bottom of anchor (with extra class .tooltip-bottom added)
// - Tooltip supports multiple line text

@import url(https://fonts.googleapis.com/css?family=Montserrat:700,400);

*, *:before, *:after {
  -moz-box-sizing: border-box; 
  -webkit-box-sizing: border-box; 
  box-sizing: border-box;
}
// this is just for looks
body {
  margin: 1em 2em;
  line-height: 1.5;
  background-color: #EAEAEA;
  color: rgb(0,19,49);
  text-align: left;
  font-family: 'Montserrat', sans-serif;
}
p {
  max-width: 40em;
}

// variables
$tip-distance: 24px;
$tip-triangle-width: 6px;
$tip-color-fallback: #001331;
$tip-color: rgba(0,19,49,.9);
// style the tooltip anchor
.tooltip {
  position: relative;
  display: inline-block;
  cursor: help;
  border-color: $tip-color-fallback; // IE8 fallback
  border-bottom: 1px dotted $tip-color;
  background-color: #EAEADE; // IE8 fallback
  background-color: rgba(255,255,0,.05);
    &:after {
      content: attr(data-tip);
      position: absolute;
      bottom: $tip-distance;
      left: -10px;
      min-width: 100px;
      max-width: 300px;
      padding: 5px 7px 5px 10px;
      pointer-events: none;
      font-size: .75em;
      background-color: $tip-color-fallback;
      background-color: $tip-color;
      color: #FFF;
      border-radius: 2px;
    }
    // this creates the little triangle
    &:before {
      content: "";
      width: 0;
      height: 0;
      position: absolute;
      left: 5px;
      bottom: $tip-distance - $tip-triangle-width;
      border-left: $tip-triangle-width solid transparent;
      border-right: $tip-triangle-width solid transparent;
      border-top: $tip-triangle-width solid $tip-color-fallback; // IE8 fallback
      border-top: $tip-triangle-width solid $tip-color;
    }
    &:before,  
    &:after {
      display: block;
      visibility: hidden; // because opacity:0 is not enough for IE8
      opacity: 0;  
      transition: opacity 0.2s ease-out, margin 0.2s ease-out;
    }
    &:hover {
      color: #999;
      &:before,  
      &:after {
        opacity: 1;
        margin-bottom: 3px;
        visibility: visible;
      }
    }
}

.tooltip-bottom {
  &:after {
    top: $tip-distance;
  }
  &:before {
    top: $tip-distance - $tip-triangle-width;
    border-top: 0;
    border-left: $tip-triangle-width solid transparent;
    border-right: $tip-triangle-width solid transparent;
    border-bottom: $tip-triangle-width solid $tip-color-fallback; // IE8 fallback
    border-bottom: $tip-triangle-width solid $tip-color;
  }
  &:hover {
    &:before,  
    &:after {
      bottom: auto;
      margin-bottom: 0;
      margin-top: 3px;
    }
  }
}
View Compiled
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.