css实现Tooltip文字提示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 800px;
            text-align: center;
            margin: 50px auto;
        }

        .tooltip {
            position: relative;
            font-size: 14px;
            cursor: pointer;
        }

        .tooltip:hover::before {
            word-break: keep-all;
            white-space: nowrap;
            content: attr(data-msg);
            position: absolute;
            padding: 2px 6px;
            display: block;
            color: #333;
            border: 1px solid #333;
            border-radius: 5px;
            font-size: 14px;
            line-height:20px;
            top: -30px;
            left: 50%;
            transform: translateX(-50%);
        }

        .tooltip:hover::after {
            content: "﹀";
            position: absolute;
            top: -8px;
            left: 50%;
            -webkit-transform: translateX(-50%);
            -ms-transform: translateX(-50%);
            transform: translateX(-50%);
            background: #fff;
            height: 7px;
            line-height: 13px;
        }
    </style>
</head>

<body>
    <div class="box">
        <a class="tooltip " data-msg="崔永元">李冰冰恨谁</a>
    </div>
</body>

</html>

  参考链接:史上最简单CSS实现Tooltip文字提示! - 知乎 (zhihu.com)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 800px;
            text-align: center;
            margin: 50px auto;
        }

        .tooltip {
            position: relative;
            font-size: 14px;
            cursor: pointer;
        }

        .tooltip:hover::before {
            word-break: keep-all;
            white-space: nowrap;
            content: attr(data-msg);
            position: absolute;
            padding: 2px 6px;
            display: block;
            color: #333;
            border: 1px solid #333;
            border-radius: 5px;
            font-size: 14px;
            line-height:20px;
            top: -30px;
            left: 50%;
            transform: translateX(-50%);
        }

        .tooltip:hover::after {
            content: "﹀";
            position: absolute;
            top: -8px;
            left: 50%;
            -webkit-transform: translateX(-50%);
            -ms-transform: translateX(-50%);
            transform: translateX(-50%);
            background: #fff;
            height: 7px;
            line-height: 13px;
        }
    </style>
</head>

<body>
    <div class="box">
        <a class="tooltip " data-msg="崔永元">李冰冰恨谁</a>
    </div>
</body>

</html>
posted @ 2022-12-02 11:47  牵你不解释  阅读(573)  评论(0)    收藏  举报