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

如何通过CSS完美调整聊天室布局 解决滚动与消息遮挡问题

问题解决方法

核心修改逻辑

  • 先清除浏览器默认边距,统一盒模型规则,避免默认样式撑出页面溢出
  • 修正.chat容器的高度计算值,要同时减去顶部head的高度和它下方的留白间距,保证整体内容高度刚好适配视口,不会出现全局滚动条
  • 给.chat_area改用纵向flex布局,让chat_body自动填充剩余可用高度,不用手动计算vh减固定值,彻底避免内容被输入框遮挡或者溢出页面的问题

完整修改后的CSS代码

/* 新增全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.wrap {
  width: 90%;
  margin: 0 auto;
}

.head {
  width: 100%;
  height: 60px;
  background-color: blue;
  margin-bottom: 80px;
}

/* 修改chat容器高度计算 */
.chat {
  background-color: #ccc;
  height: calc(100vh - 140px); /* 减去head的60px高度+80px下margin */
  display: flex;
}

.chat_list {
  flex: 1;
  height: 100%;
  background-color: #ee3f4d;
}

/* 新增chat_area的flex纵向布局配置 */
.chat_area {
  flex: 2;
  background-color: #35333c;
  display: flex;
  flex-direction: column;
}

.chat_head {
  background-color: #51c4d3;
  height: 60px;
}

/* 修改chat_body的高度配置 */
.chat_body {
  overflow-y: auto;
  flex: 1; /* 自动填充剩余高度,无需手动计算固定值 */
}
.chat_body .other_txt {
  display: block;
  background: yellow;
  padding: 8px;
  border-radius: 0 20px 20px 20px;
  margin: 10px;
}
.chat_body span {
  color: #ccc;
}
.chat_body .own_txt {
  display: block;
  background: yellow;
  margin-left: auto;
  padding: 8px;
  border-radius: 0 20px 20px 20px;
  margin: 10px;
  white-space: pre-wrap;
}

/* 移除chat_input的绝对定位,用flex布局自然排在底部 */
.chat_input {
  background-color: #43b244;
  height: 150px;
}
.chat_input .input_message {
  resize: none;
  width: 90%;
  height: 60px;
  margin: 12px 24px;
}

效果验证

修改后页面全局不会出现滚动条,聊天消息增多时仅chat_body区域内部出现滚动条,滚动到底部所有内容可见,不会被下方输入框遮挡,完全符合需求。后续如果调整chat_head或chat_input的高度,无需额外修改chat_body的参数,适配性更强。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 11:09:02