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

I just came across a neat CSS trick. Check out the fiddle...

.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;
}
<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>

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>

Here's an explanation to answer your first question (I'll leave the actual CSS to others as I'm lazy — please upvote their answers which you think deserve the votes!):

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

  • When rendering a border with varying edge colors but the same style (in your case, solid ), the seam dividing each pair of adjacent corners is a diagonal line. It's quite similar to what the diagram here depicts of the groove , ridge , inset and outset border styles.

    Note that while all browsers behave the same way and have done so for as long as I can remember, this behavior is not fully defined in either the CSS2.1 spec or the CSS Backgrounds and Borders module. The latter has a section describing color and style transitions at corners, and the description seems to imply that for borders with zero corner radii, the line that is rendered is in fact a line that joins the corner of the padding edge with the corner of the border edge (resulting in a 45-degree angled line for equal-width borders), but the spec still cautions that this may not always be the case (especially since it does not even account for borders with zero corner radii explicitly).1

  • By the content (original W3C) box model, a 40x40 area is created out of the 20-pixel borders, with the content dimensions being defined as 0x0.

  • Dividing a square with diagonal lines joining its four corners results in four right triangles whose right angles meet at the square's midpoint (see below).

  • The top, bottom and left borders are white to match the background of the .tooltiptail element's container, while the right border is a shade of blue to match the background color of the tooltip:

    border-color: #ffffff #a0c7ff #ffffff #ffffff;
    
  • The result is this, with the borders labeled, and the border boundaries added using my trusty Line Tool:

    Reorienting the tooltip tail is simply a matter of switching the tooltip color around. For example, this would yield a tail that's attached to the bottom of a tip:

    border-color: #a0c7ff #ffffff #ffffff #ffffff;
    

    jsFiddle preview


    1 If you're a stickler for standards compliance, you may as well consider all this a hack.


    I do this tooltip with only one div element.

    HTML:

    <div class="tooltip">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent augue justo, venenatis non tincidunt sit amet, suscipit eget ligula.</div>
    

    CSS:

    .tooltip{
        position: relative;
        border: 1px solid #73a7f0;
        width: 200px;
        margin-left: 20px;
        padding: 5px 14px;
        border-radius: 4px;
        -webkit-border-radius: 4px;
        -moz-border-radius: 4px;
        box-shadow: 0px 0px 6px rgba(0, 0, 0, .7);
        -webkit-box-shadow: -0px 0px 6px rgba(0, 0, 0, .7);
        -moz-box-shadow: 0px 0px 6px rgba(0, 0, 0, .7);
    }
    .tooltip:before{
        content: ' ';
        display: block;
        position: absolute;
        left: -8px;
        top: 15px;
        width: 14px;
        height: 14px;
        border-color: #73a7f0;
        border-width: 1px;
        border-style: none none solid solid;
        background-color: #fff;
        box-shadow: -2px 2px 3.5px rgba(0, 0, 0, .5);
        -webkit-box-shadow: -2px 2px 3.5px rgba(0, 0, 0, .5);
        -moz-box-shadow: -2px 2px 3.5px rgba(0, 0, 0, .5);
        transform: rotate(45deg);
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
    }
    

    Demo

    Explanation:

    I have my normal div with border just like other example. The tail is a simple combination of CSS :

  • I use the pseudo selector :before (:after works fine too)
  • I force the content with a white space to make the tail visible.
  • I rotate my box from 45deg to fix the corner in the side of the tooltip
  • No surprise for the size and the positioning.
  • I add a border on the 2 sides i want.
  • And finally i add the shadows to the outside border.
  • 链接地址: http://www.djcxy.com/p/15812.html

    上一篇: 高度等于动态宽度(CSS流体布局)

    下一篇: 我如何使用纯CSS创建“工具提示尾巴”?