css制作气泡对话
使用CSS制作气泡对话框
气泡对话框常见于聊天界面或提示信息,通过CSS的伪元素和边框属性实现。
基本结构 HTML部分需要一个容器元素包裹对话内容:
<div class="bubble">这是一条气泡消息</div>
基础样式实现 通过border-radius创建圆角矩形,配合伪元素生成三角形箭头:
.bubble {
position: relative;
background: #e0f7fa;
padding: 12px;
border-radius: 8px;
max-width: 200px;
margin: 10px;
}
.bubble::after {
content: '';
position: absolute;
width: 0;
height: 0;
border: 10px solid transparent;
}
箭头方向控制 左侧箭头:
.bubble.left::after {
left: -20px;
top: 50%;
transform: translateY(-50%);
border-right-color: #e0f7fa;
}
右侧箭头:

.bubble.right::after {
right: -20px;
top: 50%;
transform: translateY(-50%);
border-left-color: #e0f7fa;
}
顶部/底部箭头 顶部箭头:
.bubble.top::after {
top: -20px;
left: 50%;
transform: translateX(-50%);
border-bottom-color: #e0f7fa;
}
底部箭头:
.bubble.bottom::after {
bottom: -20px;
left: 50%;
transform: translateX(-50%);
border-top-color: #e0f7fa;
}
阴影和动画效果 添加阴影增强立体感:

.bubble {
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
淡入动画:
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.bubble {
animation: fadeIn 0.3s ease-out;
}
响应式调整 根据屏幕尺寸调整气泡大小:
@media (max-width: 600px) {
.bubble {
max-width: 150px;
padding: 8px;
font-size: 14px;
}
}
多气泡排列 聊天界面中的交替气泡布局:
.message-container {
display: flex;
flex-direction: column;
}
.message-container .bubble {
align-self: flex-start;
}
.message-container .bubble.right {
align-self: flex-end;
background: #bbdefb;
}






