使用css实现对话气泡的效果
/* Basic bubble styles */
.chat-bubble {
position: relative;
display: inline-block;
padding: 10px 15px;
border-radius: 20px;
max-width: 75%; /* Adjust as needed */
margin-bottom: 10px;
clear: both;
}
/* User bubble */
.user .chat-bubble {
background-color: #007bff; /* Example color */
color: white;
float: right;
}
.user .chat-bubble::before {
content: "";
position: absolute;
bottom: 0;
right: -10px;
border-bottom-left-radius: 10px;
border-width: 10px;
border-style: solid;
border-color: transparent #007bff transparent transparent; /* Match background */
}
/* Other user/bot bubble */
.other .chat-bubble {
background-color: #e9ecef; /* Example color */
color: #343a40;
float: left;
}
.other .chat-bubble::before {
content: "";
position: absolute;
bottom: 0;
left: -10px;
border-bottom-right-radius: 10px;
border-width: 10px;
border-style: solid;
border-color: transparent transparent transparent #e9ecef; /* Match background */
}
/* Example HTML structure */
```html
<div class="chat-container">
<div class="user">
<div class="chat-bubble">Hello!</div>
</div>
<div class="other">
<div class="chat-bubble">Hi there!</div>
</div>
<div class="user">
<div class="chat-bubble">How are you?</div>
</div>
</div>
Explanation and Key Improvements:
- Clearer Structure: Uses
user
andother
classes to differentiate between message senders, making styling and scripting easier. - Triangle "Tail": The
::before
pseudo-element creates the small triangle that points towards the sender, making it clear who said what. The border colors are set totransparent
except for the side that creates the triangle, and this color matches the background color of the bubble. - Responsive Design:
max-width: 75%
prevents bubbles from stretching across the entire screen on larger displays. You can adjust this value as needed. - Float Positioning:
float: right
for user bubbles andfloat: left
for other bubbles positions them on the correct sides.clear: both
ensures each bubble starts on a new line. - Simple Styling: Basic styling is provided for background color, text color, padding, and border-radius. Customize these to fit your design.
Further Enhancements:
- Avatars: Add avatar images next to the bubbles for better visual identification.
- Timestamps: Include timestamps with each message.
- Different Bubble Styles: Create variations for different message types (e.g., images, files).
- JavaScript Interaction: Use JavaScript to dynamically add new bubbles to the chat container. You'll probably want to use a framework/library for this, or at least handle the DOM manipulation carefully.
This improved CSS provides a solid foundation for creating dynamic and visually appealing chat bubbles. Remember to adapt the HTML structure and CSS classes to your specific project requirements.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现