PhilD
PhilD

Reputation: 277

fade in and out on simple css tooltip

Noobish question. Trying to make a simple css tooltip to fade in and out but cant get it to work. searched a lot but couldn't find simple answer. I'm assuming i put the transition css3 in the wrong place but its not working in the others either...

<style>

    .tooltip{
        display: inline;
        position: relative;
    }

    .tooltip:hover:after{
        border-radius: 5px;
        bottom: 26px;
        content: attr(title);
        left: 20%;
        adding: 5px 15px;
    position: absolute;
    z-index: 98;
    width: 220px;
     }

.tooltip:hover:before{

border-width: 6px 6px 0 6px;
bottom: 20px;
        content: "";
        left: 50%;
        position: absolute;
        z-index: 99;
          opacity: 0;
 -webkit-transition: opacity 1s ease-in-out;
   -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
   -o-transition: opacity 1s ease-in-out;
   transition: opacity 1s ease-in-out;
     }

</style>

html

 <br /><br /><br /><br />
<a href="#" title="Text text text text" class="tooltip"><span title="More">CSS3     Tooltip</span></a>

Upvotes: 3

Views: 11852

Answers (3)

immayankmodi
immayankmodi

Reputation: 8590

I was looking for the same but without javascript or jquery. I found following way using pure CSS3 transitions.

CSS3

a.tooltip span
{
  position: absolute;
  top: 30px;
  left: 50%;
  margin-left: -76px;
  z-index: 999;
  width: 250px;
  height: auto;
  color: #fff;
  font-size: 12px;
  line-height: 20px;
  border-radius: 6px;
  padding: 2px;
  text-align: center;
  background: #000;
  border: 1px solid #808080;
  visibility: hidden;
  opacity: 0;
  -webkit-transition: all 2s ease-out 0s;
  -moz-transition: all 2s ease-out 0s;
  -ms-transition: all 2s ease-out 0s;
  -o-transition: all 2s ease-out 0s;
  transition: all 2s ease-out 0s;
}
a:hover.tooltip span
{
  visibility: visible;
  -webkit-opacity: 0.90;
  -moz-opacity: 0.90;
  opacity: 0.90;
}

HTML

<a class="tooltip" href="#">Hover Me
    <span>Some text information that you want to display in tooltip</span></a>

For more details with live demo, visit Tooltip Example using CSS3 Transitions here.

Upvotes: 1

Leon
Leon

Reputation: 2149

You need to set the properties inside .tooltip:after, not .tooltip:hover:after.

Then add:

.tooltip:after {
    opacity:0;
}

And:

.tooltip:hover:after {
    opacity:1;
}

The .tooltip:hover:before is not needed for this.

See http://jsfiddle.net/NHkQr/

Upvotes: 2

Abhitalks
Abhitalks

Reputation: 28417

  1. You don't need both :before and :after.
  2. You have to first define :after content and its styles, with transition.
  3. Then define :after styles on :hover with only the transition properties (e.g. opacity).

Demo: http://jsfiddle.net/abhitalks/aRzA3/1/

CSS:

.tooltip:after {
    content: attr(title); /* define content here */
    ...
    opacity: 0; /* define initial transition property */
    -webkit-transition: opacity 1s ease-in-out; /* define transitions */
    transition: opacity 1s ease-in-out;
}
.tooltip:hover:after {
    opacity: 1; /* provide only the final transition property */
}

Upvotes: 5

Related Questions