实现完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat Bubble Example</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
padding: 20px;
}
.chat-container {
display: flex;
flex-direction: column;
max-width: 600px;
margin: 0 auto;
}
.chat-bubble {
position: relative;
padding: 10px 15px;
border-radius: 10px;
margin: 10px 0;
max-width: 70%;
line-height: 1.5;
}
.chat-bubble.left {
background-color: #e5e5ea;
align-self: flex-start;
}
.chat-bubble.left::before {
content: '';
position: absolute;
top: 15px;
left: -10px;
width: 0;
height: 0;
border: 10px solid transparent;
border-right-color: #e5e5ea;
border-left: 0;
border-top: 0;
margin-top: -5px;
}
.chat-bubble.right {
background-color: #007aff;
color: white;
align-self: flex-end;
}
.chat-bubble.right::before {
content: '';
position: absolute;
top: 15px;
right: -10px;
width: 0;
height: 0;
border: 10px solid transparent;
border-left-color: #007aff;
border-right: 0;
border-top: 0;
margin-top: -5px;
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-bubble left">
你好!这是一个左边的聊天气泡。
</div>
<div class="chat-bubble right">
你好!这是一个右边的聊天气泡。
</div>
<div class="chat-bubble left">
这个气泡的样式和微信聊天类似。
</div>
<div class="chat-bubble right">
是的,我们可以通过CSS轻松实现这个效果。
</div>
</div>
</body>
</html>