CSS实现简单气泡

3,937 阅读1分钟

一、 最简单气泡

1. 效果图

image.png

2. 代码

<!DOCTYPE html>
<html lang="ch">
    <head>
        <meta charset="UTF-8">
        <title>测试</title>
        <style>
            .bubble-box {
                position: relative;
                border: 2px solid #0038ff;
                border-radius: 5px;
                width: 200px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                margin-left: 20px;
            }

            .bubble-box::before {
                content: " ";
                position: absolute;
                right: 100%;
                top: 50%;
                margin: -5px 0 0;
                border: 10px solid transparent;
                border-right-color: #0038ff;
            }

            .bubble-box::after {
                content: " ";
                position: absolute;
                right: 100%;
                top: 50%;
                margin-top: -2px;
                border: 7px solid transparent;
                border-right-color: #fff;
            }
        </style>
    </head>
    <body>
        <div class="bubble-box">
            行走的bug制造机
        </div>
    </body>
</html>

3. 思路

左侧的小三角使用伪类::before image.png 小三角需要有一个白色背景的小三角覆盖到有颜色的小三角上, 需要使用::after image.png

二、 斜三角形气泡

1. 效果图

image.png

2. 代码

<!DOCTYPE html>
<html lang="ch">
    <head>
        <meta charset="UTF-8">
        <title>测试</title>
        <style>
            .bubble-box {
                position: relative;
                border: 2px solid #0038ff;
                border-radius: 5px;
                width: 200px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                margin-left: 20px;
            }

            .bubble-box::before {
                content: " ";
                position: absolute;
                border: 10px solid transparent;
                border-top-color: #0038ff;
                border-right-color: #0038ff;
                right: 100%;
                top: 8%;
                transform: skewY(16deg);
            }
        </style>
    </head>
    <body>
        <div class="bubble-box">
            行走的bug制造机
        </div>
    </body>
</html>

3. 思路

与简单气泡原理一样, 只不过不用after这个伪类, 使用 transform 进行角度倾斜达到效果

三、 拖尾气泡

1. 效果图

image.png

2. 代码

<!DOCTYPE html>
<html lang="ch">
    <head>
        <meta charset="UTF-8">
        <title>测试</title>
        <style>
            .bubble-box {
                position: relative;
                color: #fff;
                border: 2px solid #0038ff;
                background: #0038ff;
                border-radius: 25px;
                width: 200px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                margin-left: 20px;
            }

            .bubble-box::before {
                content: " ";
                position: absolute;
                z-index: -1;
                bottom: -5px;
                right: -8px;
                height: 22px;
                border-right: 20px solid #0038ff;
                border-bottom-left-radius: 15px 15px;
                transform: translate(0, -2px);
            }

            .bubble-box::after {
                content: " ";
                position: absolute;
                z-index: 1;
                bottom: -5px;
                right: -58px;
                width: 26px;
                height: 22px;
                background: #fff;
                border-bottom-left-radius: 10px;
                transform: translate(-30px, -2px);
            }
        </style>
    </head>
    <body>
        <div class="bubble-box">
            行走的bug制造机
        </div>
    </body>
</html>

3. 思路

小尾巴的思路和三角形的思路实现是一样的,只不过小尾巴的弧度采用的是 border-bottom-left-radius: 15px 15px