html - 溢出 : scroll on div with flex: 1 without specific height

标签 html css flexbox

我有一个类似于 Discord 的 flexbox 布局。 我的列表是包含我所有消息的消息列表。 我想添加 overflow-y: 滚动到它,但它不起作用,因为我没有特定的高度。 另外,我不想添加特定高度,因为它应该是动态的。

我的解决方案是。

最大高度:计算(100vh - header.height - send.height)

但它仅在您具有特定的标题高度并发送时才有效。

还有其他办法吗?

示例:https://jsfiddle.net/AvaPLaIN/b7umpgzn/44/

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  border: 2px solid black;
}

.header {
  width: 100%;
  border: 2px solid red;
  min-height: 3rem;
}

.content {
  flex: 1;
  display: flex;
  border: 2px solid green;
}

.chatbox {
  flex: 1;
  border: 2px solid yellow;
  display: flex;
  flex-direction: column;
}

.members {
  width: 15rem;
  border: 2px solid blue;
}

.list {
  flex: 1;
  border: 2px solid grey;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.5rem;
  overflow-y: scroll;
}

li {
  width: 100%;
  border: 1px solid black;
}

.send {
  height: 5rem;
  border: 2px solid purple;
}
<div class="container">
  <div class="header">
    Header here
  </div>
  <div class="content">
    <div class="chatbox">
      <ul class="list">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
        <li>10</li>
      </ul>
      <div class="send">
        Send Message here
      </div>
    </div>
    <div class="members">
      Memberlist here
    </div>
  </div>
</div>

最佳答案

只需在 .list 中使用 flex: 1 1 0 而不是 flex: 1

问题与flex-basis 属性和flex: 1 简写有关。

flex-basis: 0pxflex-basis: 0% 在某些情况下有不同的结果。这是解释问题的答案:https://stackoverflow.com/a/63475286/15924950

此外,我做了一个简单的演示来展示 flex-basis: 0pxflex-basis: 0% 之间的区别:https://codepen.io/shadow-mike/pen/NWpNVvv

关于html - 溢出 : scroll on div with flex: 1 without specific height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70198644/

相关文章:

javascript - 无法使用过渡调整 <div> 容器的大小

javascript - css flex - 最大化元素大小,同时保持它们填充不同大小的容器

html - 在 Google Drive 上显示 HTML 文件?

javascript - 使用 clone() 绑定(bind)重复事件

html - 如何在屏幕中间设置轮播?

java - 在Java中将映射的键和值解析为一个大字符串

php - mysql/php/javascript/css 日历建议

html - 带有滚动文本的滑动叠加

html - React/Flexbox/SPA/无滚动条 : Trouble segmenting page into header/content/footer that takes up the whole page

jquery - div 内的图像将内容 + 内容推送到与图像相同的高度(使用 CSS Flexbox)