锦方的个人网页

css 提示

发布 · 约 14 字 · 预计 1 分钟

介绍使用 CSS 制作四个方向的悬浮提示框,包括箭头、定位和渐显效果。

归档记录:本文最后更新于 2017 年,文中涉及的软件版本、配置或命令可能已随环境演进发生变化。

系列连载:CSS 笔记系列(第 5 / 8 篇)
  1. 1. css 单词
  2. 2. css 导航栏
  3. 3. css 表单
  4. 4. css 下拉
  5. 5. css 提示(当前)
  6. 6. css 图片
  7. 7. css RWD
  8. 8. css 扩散列表

下面是完整的 CSS 提示框示例代码:

<!doctype html>
<html>
<head>
    <style>
         /*---1-------------------------------------------
            	通用
        -------------------------------------------------*/    
        .tooltip{
            position: relative;
            top: 120px;
            display: inline-block;
            border-bottom:1px dotted black;
        }
        /*---2-------------------------------------------
            	这里是上拉文字的
        -------------------------------------------------*/    
       .tooltiptexttop{
            visibility: hidden;
            width:120px;
            background-color: black;
            color:#fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px 0;
            position: absolute;
            z-index: 1;
            bottom: 150%;
            left: 50%;
            margin-left:-60px;
        }
        .tooltip .tooltiptexttop::after{
            content:"";
            position: absolute;
            top: 100%;
            left:50%;
            margin-left: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: black transparent transparent transparent;
        }
        .tooltip:hover .tooltiptexttop{
            visibility: visible;
        }
        /*---3-------------------------------------------
            	这里是左拉文字的
        -------------------------------------------------*/  
        .tooltip .tooltiptextleft{
            visibility: hidden;
            width:120px;
            background-color: black;
            color: #fff;
            text-align: center;
            padding: 5px 0;
            position: absolute;
            z-index: 1;
            top: -5px;
            right: 105%;
        }
        .tooltip:hover .tooltiptextleft{
            visibility: visible;
        }


        /*---4-------------------------------------------
            	这里是下拉文字的
        -------------------------------------------------*/  
        .tooltip .tooltiptextbottom{
            visibility: hidden;
            width: 120px;
            background-color: #000;
            color: white;
            text-align: center;
            border-radius: 6px;
            padding: 5px 0;
            position: absolute;
            z-index: 1;
            top: 100%;
            left:50%;
            margin-left: -60px;
            opacity: 0;
            transition: opacity 1s;
        }
        .tooltip:hover .tooltiptextbottom{
            visibility: visible;
            opacity: 1;
        }


        /*---5-------------------------------------------
            	这里是右拉文字的
        -------------------------------------------------*/  
        .tooltip .tooltiptextright{
            visibility: hidden;
            width:120px;
            background-color: black;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px 0;
            position: absolute;
            z-index: 1;
            top: -5px;
            left: 110%;
        }
        .tooltip .tooltiptextright::after{
            content: "";
            position: absolute;
            top: 50%;
            right: 100%;
            margin-top: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: transparent black transparent transparent;
        }
        .tooltip:hover .tooltiptextright{
            visibility:visible;
        }
    </style>
</head>
<body style="text-align:center;">
    <div class="tooltip">在上面的标签
        <span class="tooltiptexttop">我是不是在上面</span>
    </div></br>
    <div class="tooltip">在左边的标签
        <span class="tooltiptextleft">我是不是在做面</span>
    </div></br>
    <div class="tooltip">在下面的标签
        <span class="tooltiptextbottom">渐渐弹出在下面</span>
    </div></br>
    <div class="tooltip">在右面的标签
        <span class="tooltiptextright">在右面有箭头的</span>
    </div>
</body>
</html>

✉ 对本文有补充或勘误?欢迎就本文写信交流。