influxweb
4/21/2015 - 6:42 PM

Super simple CSS tooltips

Super simple CSS tooltips

@import url(http://fonts.googleapis.com/css?family=Roboto:400,500,600,700);

$pink: #e91e63;
$paper: #efefef;
$clouds: #ecf0f1;
$cubic: cubic-bezier(.64,.09,.08,1);

html, body {
  background: $clouds;
  height: 100%;
  overflow-x: hidden;
  width: 100%;
}
* {
  box-sizing: border-box;
}
h1, h2, p {
  font-family: "Roboto", sans-serif;
}
h1 {
  background: $pink;
  color: $paper;
  font-size: 26px;
  margin-bottom: 50px;
  padding: 40px 0 40px 20%;
}
body > div {
  background: #fff;
  border-radius: 2px;
  box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.2);
  margin-bottom: 30px;
  margin-left: 20%;
  padding: 20px;
  width: 60%;
  &:last-of-type {
    margin-bottom: 100px;
  }
}
h2 {
  color: $pink;
  font-weight: 500;
  margin-bottom: 16px;
}
p {
  display: inline;
  margin-right: 50px;
}
[class^="tooltip"] {
  position: relative;
  &:after {
    background: #999;
    border-radius: 2px;
    color: #fff;
    content: attr(data-tooltip);
    left: 50%;
    opacity: 0;
    padding: 6px 10px;
    position: absolute;
    top: 1.4em;
    transform: translateX(-50%) translateY(-2px);
    transition: opacity 0.2s $cubic, transform 0.2s $cubic;
    visibility: hidden;
    white-space: nowrap;
    z-index: 2;
  }
  &:hover {
    &:after {
      display: block;
      opacity: 1;
      transform: translateX(-50%) translateY(0);
      visibility: visible;
    }
  }
}
.tooltip--left {
  &:after {
    left: 0;
    top: -4px;
    transform: translateX(-112%) translateY(0);
  }
  &:hover {
    &:after {
      transform: translateX(-110%) translateY(0);
    }
  }
}
.tooltip--right {
  &:after {
    left: 100%;
    top: -4px;
    transform: translateX(12%) translateY(0);
  }
  &:hover {
    &:after {
      transform: translateX(10%) translateY(0);
    }
  }
}
.tooltip--triangle {
  &:before {
    border-bottom: solid 5px #999;
    border-left: solid 5px transparent;
    border-right: solid 5px transparent;
    content: "";
    height: 0;
    left: 50%;
    opacity: 0;
    position: absolute;
    top: 1.1em;
    transform: translateX(-50%) translateY(-2px);
    transition: opacity 0.2s $cubic, transform 0.2s $cubic;
    visibility: hidden;
    width: 0;
    z-index: 3;
  }
  &:hover {
    &:before {
      display: block;
      opacity: 1;
      visibility: visible;
      transform: translateX(-50%) translateY(0);
    }
  }
}