You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

聊天评论与多层回复UI开发:圆角及样式问题求助(最多三层嵌套)

修复聊天界面的圆角与嵌套样式问题

问题说明

参照设计稿开发聊天界面UI时,无法解决消息气泡圆角及嵌套回复的样式问题,界面最多支持三层嵌套回复。

设计稿

设计稿

当前代码

CSS 代码

/* styles.css */

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 20px;
}

.chat-container {
  max-width: 600px;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.message {
  position: relative;
  padding: 10px 20px 10px 60px;
  /* Adjust padding to make space for the avatar */
  border-radius: 5px;
  margin-bottom: 10px;
}

.message.reply {}

.avatar {
  position: absolute;
  top: 0;
  left: 10px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #ccc;
}

.message-content {
  position: relative;
  z-index: 2;
}

.replies {
  margin-top: 30px;
  margin-left: 25px;
  /* Align nested replies with avatars */
}

.message::before {
  content: '';
  position: absolute;
  left: 30px;
  /* Align with the center of the avatar */
  top: 0;
  bottom: 42px;
  width: 1px;
  background: #ddd;
}

.message.reply::before {
  background: #dddddd;
}

.message.reply::after {
  content: "";
  position: absolute;
  inset-inline-start: 0px;
  left: -55px;
  top: 5px;
  height: 25px;
  width: 60px;
  -webkit-border-start: 1px solid #ddd;
  border-inline-start: 1px solid #ddd;
  border-bottom: 1px solid #dddddd;
  background: transparent;
  border-end-start-radius: 10px;
}

HTML 代码

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<div class="chat-container">
  <div class="message" id="message-1">
    <img src="https://picsum.photos/40/40" alt="User 1" class="avatar">
    <div class="message-content">Hello, this is the first message.</div>
    <div class="replies">
      <div class="message reply" id="reply-1-1">
        <img src="https://picsum.photos/40/40" alt="User 2" class="avatar">
        <div class="message-content">This is a reply to the first message.</div>
        <div class="replies">
          <div class="message reply" id="reply-1-1-1">
            <img src="https://picsum.photos/40/40" alt="User 3" class="avatar">
            <div class="message-content">This is a nested reply.</div>
          </div>
        </div>
      </div>
      <div class="message reply" id="reply-1-2">
        <img src="https://picsum.photos/40/40" alt="User 4" class="avatar">
        <div class="message-content">Another reply to the first message.</div>
      </div>
    </div>
  </div>
</div>

修复方案

针对圆角和嵌套样式问题,修改后的代码如下:

修改后的 CSS 代码

/* styles.css */

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 20px;
  background-color: #f5f5f5;
}

.chat-container {
  max-width: 600px;
  margin: 0 auto;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.message {
  position: relative;
  padding: 12px 16px;
  margin-bottom: 30px;
}

/* 主消息气泡样式 */
.message:not(.reply) .message-content {
  background-color: #e5e5ea;
  border-radius: 18px;
  padding: 10px 15px;
  margin-left: 60px;
}

/* 回复消息气泡样式 */
.message.reply .message-content {
  background-color: #007aff;
  color: white;
  border-radius: 18px;
  padding: 10px 15px;
  margin-left: 60px;
}

.avatar {
  position: absolute;
  top: 0;
  left: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #ccc;
}

.message-content {
  position: relative;
  z-index: 2;
}

.replies {
  margin-top: 10px;
  margin-left: 50px;
}

/* 主消息的连接线 */
.message:not(.reply)::before {
  content: '';
  position: absolute;
  left: 20px;
  top: 40px;
  bottom: -30px;
  width: 1px;
  background: #ddd;
}

/* 回复消息的连接线 */
.message.reply::before {
  content: '';
  position: absolute;
  left: -30px;
  top: 20px;
  height: 1px;
  width: 30px;
  background: #ddd;
}

/* 回复消息的拐角连接线 */
.message.reply::after {
  content: '';
  position: absolute;
  left: -30px;
  top: 0;
  bottom: 20px;
  width: 1px;
  background: #ddd;
}

/* 最后一条回复去掉底部连接线 */
.replies .message:last-child::before {
  bottom: 20px;
}

修改后的 HTML 代码(结构不变,仅样式依赖修改后的CSS)

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<div class="chat-container">
  <div class="message" id="message-1">
    <img src="https://picsum.photos/40/40" alt="User 1" class="avatar">
    <div class="message-content">Hello, this is the first message.</div>
    <div class="replies">
      <div class="message reply" id="reply-1-1">
        <img src="https://picsum.photos/40/40" alt="User 2" class="avatar">
        <div class="message-content">This is a reply to the first message.</div>
        <div class="replies">
          <div class="message reply" id="reply-1-1-1">
            <img src="https://picsum.photos/40/40" alt="User 3" class="avatar">
            <div class="message-content">This is a nested reply.</div>
          </div>
        </div>
      </div>
      <div class="message reply" id="reply-1-2">
        <img src="https://picsum.photos/40/40" alt="User 4" class="avatar">
        <div class="message-content">Another reply to the first message.</div>
      </div>
    </div>
  </div>
</div>

修复说明:

  • 给消息气泡添加背景色,将圆角调整为18px,贴合设计稿样式
  • 重新调整嵌套回复的缩进,强化层级区分
  • 重构连接线样式,实现设计稿中的垂直+水平拐角连接效果
  • 移除最后一条回复的多余底部连接线,优化视觉体验

内容的提问来源于stack exchange,提问作者Momin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 22:35:55