CSS实现聊天气泡

效果图

在这里插入图片描述
在这里插入图片描述

HTML代码

<a-row :gutter="10">
      <a-col :span="24" style="margin-bottom: 10px">
        <div class="chat-bubble chat-bubble-left">
          <span>收到一条消息</span>
        </div>
      </a-col>
      <a-col :span="24" style="margin-bottom: 10px">
        <div class="chat-bubble chat-bubble-left">
          <span>收到一条消息收到一条消息收到一条消息收到一条消息收到一条消息收到一条消息收到一条消息收到一条消息</span>
        </div>
      </a-col>
      <a-col :span="24" style="margin-bottom: 10px">
        <div class="chat-bubble chat-bubble-left">
          <span>收到一条消息收到一条消息收到一条消息收到一条消息收到一条消息</span>
        </div>
      </a-col>
      <a-col :span="24" style="margin-bottom: 10px">
        <div class="chat-bubble chat-bubble-right chat-bubble-primary">
          <span>发送一条消息</span>
        </div>
      </a-col>
      <a-col :span="24" style="margin-bottom: 10px">
        <div class="chat-bubble chat-bubble-right chat-bubble-primary">
          <span>发送一条消息发送一条消息发送一条消息发送一条消息发送一条消息发送一条消息发送一条消息发送一条消息发送一条消息</span>
        </div>
      </a-col>
    </a-row>

CSS样式

.chat-bubble{
  color: #333;
  border-radius: 5px;
  box-shadow: 3px 5px 15px rgba(0,0,0,.2);
  padding: 5px 10px;
  width: auto;
  max-width: 50%;
  text-align: left;
  display: inline-block !important;
  position: relative;
  word-break:break-all;
  background-color: #ffffff;
  transition: all .2s;
  cursor: pointer;
}
.chat-bubble:hover{
  transform: scale(1.03);
}
.chat-bubble-left{
  float: left;
}
.chat-bubble-left:before{
  content: '';
  width: 10px;
  height: 10px;
  left: -10px;
  top: calc(50% - 5px);
  position: absolute;
  border-left: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-top: 5px solid transparent;
  border-right: 5px solid #ffffff;
}
.chat-bubble-right{
  float: right;
}
.chat-bubble-right:after{
  content: '';
  width: 10px;
  height: 10px;
  right: -10px;
  top: calc(50% - 5px);
  position: absolute;
  border-left: 5px solid #ffffff;
  border-bottom: 5px solid transparent;
  border-top: 5px solid transparent;
  border-right: 5px solid transparent;
}
/**
   chat-bubble-primary
 */
.chat-bubble-left.chat-bubble-primary{
  background:linear-gradient(90deg,#2b92e4, #30a1dc) !important;
  color: #ffffff !important;
}
.chat-bubble-left.chat-bubble-primary:before{
  border-right: 5px solid #2b92e4 !important;
}
.chat-bubble-right.chat-bubble-primary{
  background:linear-gradient(90deg,#30a1dc,#2b92e4) !important;
  color: #ffffff !important;
}
.chat-bubble-right.chat-bubble-primary:after{
  border-left: 5px solid #2b92e4 !important;
}
/**
   chat-bubble-success
 */
.chat-bubble-left.chat-bubble-success{
  background:linear-gradient(90deg,#4caf50, #66b869) !important;
  color: #ffffff !important;
}
.chat-bubble-left.chat-bubble-success:before{
  border-right: 5px solid #4caf50 !important;
}
.chat-bubble-right.chat-bubble-success{
  background:linear-gradient(90deg, #66b869,#4caf50) !important;
  color: #ffffff !important;
}
.chat-bubble-right.chat-bubble-success:after{
  border-left: 5px solid #4caf50 !important;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现聊天气泡效果,可以使用以下的CSS样式: HTML结构: ```html <div class="chat-container"> <div class="chat-message received"> <div class="message-bubble"> <span class="message">Hello!</span> </div> </div> <div class="chat-message sent"> <div class="message-bubble"> <span class="message">Hi there!</span> </div> </div> <div class="chat-message received"> <div class="message-bubble"> <span class="message">How are you?</span> </div> </div> <div class="chat-message sent"> <div class="message-bubble"> <span class="message">I'm good, thanks. How about you?</span> </div> </div> </div> ``` CSS样式: ```css .chat-container { display: flex; flex-direction: column; padding: 20px; background-color: #eee; height: 400px; overflow-y: scroll; } .chat-message { display: flex; margin-bottom: 10px; } .received { align-self: flex-start; } .sent { align-self: flex-end; } .message-bubble { background-color: #fff; border-radius: 10px; padding: 10px; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1); position: relative; } .message-bubble:before { content: ""; position: absolute; top: 0; border-style: solid; border-width: 0 10px 10px 0; } .received .message-bubble:before { left: -10px; border-color: #fff transparent transparent transparent; } .sent .message-bubble:before { right: -10px; border-color: #007bff transparent transparent transparent; } .message { line-height: 1.5; font-size: 14px; } ``` 解释: - `chat-container`是聊天窗的容器,设置了`flex`布局和垂直方向的排列,以及一些基本的样式,如背景色、高度和滚动条等。 - `chat-message`是每个聊天消息的容器,设置了`flex`布局和一些基本的样式,如外边距等。 - `.received`和`.sent`分别表示接收到的消息和发送的消息,设置了不同的对齐方式。 - `message-bubble`是消息气泡的容器,设置了一些基本的样式,如背景色、边框半径、内边距和阴影等,以及相对定位。 - `.message-bubble:before`是消息气泡的三角形,使用`content`属性来插入一个空内容,设置了绝对定位、上边距、边框样式和边框宽度,并根据消息的来源设置了不同的位置、边框颜色和方向。 - `message`是消息内容的容器,设置了一些基本的样式,如行高和字体大小等。 这些样式可以根据实际需求进行调整,以达到更好的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值