css制作会话气泡
使用CSS制作会话气泡
会话气泡通常用于聊天界面或提示信息,通过CSS可以轻松实现。以下是几种常见的方法:
基础会话气泡
通过边框和伪元素创建三角形箭头:

.bubble {
position: relative;
background: #e0e0e0;
padding: 10px 15px;
border-radius: 10px;
max-width: 200px;
}
.bubble::after {
content: '';
position: absolute;
bottom: -10px;
left: 20px;
border-width: 10px 10px 0;
border-style: solid;
border-color: #e0e0e0 transparent;
}
带阴影的会话气泡
添加阴影效果增强立体感:
.bubble-shadow {
position: relative;
background: white;
padding: 12px 18px;
border-radius: 12px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.bubble-shadow::before {
content: '';
position: absolute;
bottom: -10px;
left: 25px;
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid white;
filter: drop-shadow(0 2px 2px rgba(0,0,0,0.1));
}
不同方向的会话气泡
通过调整伪元素位置实现不同方向的箭头:

/* 右侧气泡 */
.bubble-right {
position: relative;
background: #4a8af4;
color: white;
padding: 10px 15px;
border-radius: 10px;
margin-left: auto;
}
.bubble-right::after {
content: '';
position: absolute;
right: -10px;
top: 15px;
border-width: 10px 0 10px 10px;
border-style: solid;
border-color: transparent #4a8af4;
}
圆形会话气泡
使用border-radius创建圆形气泡:
.bubble-round {
display: inline-block;
background: #ff6b6b;
color: white;
padding: 10px 20px;
border-radius: 50px;
position: relative;
}
.bubble-round::after {
content: '';
position: absolute;
bottom: -8px;
left: 30px;
border-width: 8px 8px 0;
border-style: solid;
border-color: #ff6b6b transparent;
}
动画会话气泡
添加动画效果使气泡更有活力:
@keyframes bubble-pop {
0% { transform: scale(0.9); opacity: 0; }
50% { transform: scale(1.05); }
100% { transform: scale(1); opacity: 1; }
}
.bubble-animate {
animation: bubble-pop 0.3s ease-out;
background: #6b5bff;
color: white;
padding: 12px 18px;
border-radius: 12px;
display: inline-block;
}
这些CSS代码片段可以根据需要组合使用,调整颜色、大小和位置以适应不同的设计需求。通过修改伪元素的border属性值,可以改变箭头的形状和方向。






