在简单的 CSS 工具提示上淡入和淡出

Phi*_*ilD 3 html css

新手问题。尝试制作一个简单的 css 工具提示来淡入和淡出,但无法使其工作。搜索了很多但找不到简单的答案。我假设我把过渡 css3 放在错误的地方,但它在其他地方也不起作用......

<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>
Run Code Online (Sandbox Code Playgroud)

html

 <br /><br /><br /><br />
<a href="#" title="Text text text text" class="tooltip"><span title="More">CSS3     Tooltip</span></a>
Run Code Online (Sandbox Code Playgroud)

Abh*_*lks 5

  1. 您不需要同时使用:before:after
  2. 您必须首先定义:after内容及其样式以及过渡。
  3. 然后仅使用过渡属性(例如不透明度)定义:after样式。:hover

演示: 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 */
}
Run Code Online (Sandbox Code Playgroud)