Search code examples
csstooltipcss-shapes

How can I create a "tooltip tail" using pure CSS?


I just came across a neat CSS trick:

HTML:

<div>Cool Trick:<br />
    <div class="tooltiptail"></div>
</div>
<br />

<div>How do I get this effect with only CSS?<br />
    <div class="anothertail"></div>
</div>

CSS:

.tooltiptail {
    display: block;
    border-color: #ffffff #a0c7ff #ffffff #ffffff;
    border-style: solid;
    border-width: 20px;
    width: 0px;
    height: 0px;
}

.anothertail {
    background-image: url(http://static.jqueryfordesigners.com/demo/images/coda/bubble-tail2.png);
    display: block;
    height: 29px;
    width: 30px;

}

Check out the fiddle... http://jsfiddle.net/duZAx/1/

This creates a little arrow/triangle-like effect, a "tooltip tail". This blows my mind! I'm really interested in knowing how this works?!

Further, is there a way to extend this CSS trick to create an effect as follows...

        enter image description here

This is an interesting problem.

  1. Can this be done using only CSS, without the shadow?
  2. Can this be done with the shadow and having it cross-browser compatible?

Solution

  • Here's an example with a box-shadow, all latest version browsers should support this

    http://jsfiddle.net/MZXCj/1/

    HTML:

    <div id="toolTip">
        <p>i can haz css tooltip</p>
        <div id="tailShadow"></div>
        <div id="tail1"></div>
        <div id="tail2"></div>
    </div>
    

    CSS:

    body {font-family:Helvetica,Arial,sans-serif;}
    
    #toolTip {
        position:relative;
    }
    
    #toolTip p {
        padding:10px;
        background-color:#f9f9f9;
        border:solid 1px #a0c7ff;
        -moz-border-radius:5px;-ie-border-radius:5px;-webkit-border-radius:5px;-o-border-radius:5px;border-radius:5px;
    }
    
    #tailShadow {
        position:absolute;
        bottom:-8px;
        left:28px;
        width:0;height:0;
        border:solid 2px #fff;
        box-shadow:0 0 10px 1px #555;
    }
    
    #tail1 {
        position:absolute;
        bottom:-20px;
        left:20px;
        width:0;height:0;
        border-color:#a0c7ff transparent transparent transparent;
        border-width:10px;
        border-style:solid;
    }
    
    #tail2 {
        position:absolute;
        bottom:-18px;
        left:20px;
        width:0;height:0;
        border-color:#f9f9f9 transparent transparent transparent;
        border-width:10px;
        border-style:solid;
    }
    

    body {
      font-family: Helvetica, Arial, sans-serif;
    }
    #toolTip {
      position: relative;
    }
    #toolTip p {
      padding: 10px;
      background-color: #f9f9f9;
      border: solid 1px #a0c7ff;
      -moz-border-radius: 5px;
      -ie-border-radius: 5px;
      -webkit-border-radius: 5px;
      -o-border-radius: 5px;
      border-radius: 5px;
    }
    #tailShadow {
      position: absolute;
      bottom: -8px;
      left: 28px;
      width: 0;
      height: 0;
      border: solid 2px #fff;
      box-shadow: 0 0 10px 1px #555;
    }
    #tail1 {
      position: absolute;
      bottom: -20px;
      left: 20px;
      width: 0;
      height: 0;
      border-color: #a0c7ff transparent transparent transparent;
      border-width: 10px;
      border-style: solid;
    }
    #tail2 {
      position: absolute;
      bottom: -18px;
      left: 20px;
      width: 0;
      height: 0;
      border-color: #f9f9f9 transparent transparent transparent;
      border-width: 10px;
      border-style: solid;
    }
    <div id="toolTip">
      <p>i can haz css tooltip</p>
      <div id="tailShadow"></div>
      <div id="tail1"></div>
      <div id="tail2"></div>
    </div>