Search code examples
csscss-shapes

Tooltip with a triangle


I have created a jsFiddle for this question.

a.tip {
    position: relative;
}

a.tip span {
    display: none;
    position: absolute;
    top: -5px;
    left: 60px;
    width: 125px;
    padding: 5px;
    z-index: 100;
    background: #000;
    color: #fff;
    -moz-border-radius: 5px; /* this works only in camino/firefox */
    -webkit-border-radius: 5px; /* this is just for Safari */
}

a:hover.tip {
    font-size: 99%; /* this is just for IE */
}

a:hover.tip span {
    display: block;
}
        
<center><a href="http://google.com/" class="tip">Link!<span>Hi its me!</span></a></center>

Basically I have a tooltip on my site, and it appears to the right of my link. But on the left hand side of the black box I would like a triangle attached to the box pointing to the link, is it possible to do this using only CSS? just like this but to the left

tooltip with arrow


Solution

  • You can do it with CSS, by using the css triangle trick

    a.tip span:before{
        content:'';
        display:block;
        width:0;
        height:0;
        position:absolute;
    
        border-top: 8px solid transparent;
        border-bottom: 8px solid transparent;
        border-right:8px solid black;
        left:-8px;
    
        top:7px;
    }
    

    Demo at http://jsfiddle.net/dAutM/7/


    live snippet

    a.tip {
      position: relative;
    }
    
    a.tip span {
      display: none;
      position: absolute;
      top: -5px;
      left: 60px;
      width: 125px;
      padding: 5px;
      z-index: 100;
      background: #000;
      color: #fff;
      -moz-border-radius: 5px;
      /* this works only in camino/firefox */
      -webkit-border-radius: 5px;
      /* this is just for Safari */
    }
    
    a.tip span:before {
      content: '';
      display: block;
      width: 0;
      height: 0;
      position: absolute;
      border-top: 8px solid transparent;
      border-bottom: 8px solid transparent;
      border-right: 8px solid black;
      left: -8px;
      top: 7px;
    }
    
    a:hover.tip {
      font-size: 99%;
      /* this is just for IE */
    }
    
    a:hover.tip span {
      display: block;
    }
    <center><a href="http://google.com/" class="tip">Link!<span>Hi its me!</span></a></center>