【问题标题】:overflow: scroll on div with flex: 1 without specific height溢出:使用 flex 在 div 上滚动:1 没有特定高度
【发布时间】:2022-01-08 21:31:32
【问题描述】:

我有一个类似于 Discord 的 flexbox 布局。 我的列表是包含所有消息的 messageList。 我想添加 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>

【问题讨论】:

  • 改为以百分比形式给出高度,这将使其保持响应并帮助您解决滚动问题

标签: html css flexbox


【解决方案1】:

这是动态滚动部分的可靠布局,主要使用 flexbox 和网格。在 sn-p 中运行“整页”。显然,您需要媒体查询来针对移动设备进行调整。

 body {
      font-family: sans-serif;
    }

    .container {
      display: flex;
      flex-direction: row;
      position: absolute;
      top: 50px;
      right: 0;
      bottom: 0;
      left: 0;

    }

    header {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: 50px;
      color: #fff;
      background: #40B6C0;

    }

    .column-1 {
      grid-template-columns: auto auto;
      flex-direction: column;
      grid-gap: 0;
      width: 30%;
      flex-grow: 1;
      flex-direction: column;

    }

    .column-1 div {
      height: 50%;
      width: 100%;
    }


    .column-2 {
      background: #FF5C5C;
      width: 70%;
      display: flex;
      background: white;
      flex-grow: 1;
      overflow-y: scroll;
      flex-direction: column;

    }

    .column-2 ul {
      padding: 24px;
    }

    .column-2 li {
      list-style: none;
      padding: 12px 0;
      border-bottom: 1px solid #EAEEF1;
    }

    .column-2 footer {
      position: sticky;
      bottom: 0;
      background: #FF6F6F;
      color: #fff;
      padding: 24px;

    }

    #cell-1 {
      background: #2689B9;
    }

    #cell-2 {
      background: #8383C3;
    }
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no" />


</head>

<body>

  <header>
    This is your header
  </header>
  <div class="container">

    <div class="column-1">
      <div id="cell-1">Memberlist here</div>
      <div id="cell-2">Other Stuff Here</div>
    </div>

    <div class="column-2">
      <ul>
        <li>
          They're not aliens, they're Earth…liens!</li>
        <li>No… It's a thing; it's like a plan, but with more greatness. No… It's a thing; it's like a plan, but with
          more
          greatness. All I've got to do is pass as an ordinary human being. Simple. What could possibly go wrong?</li>

        <li>It's art! A statement on modern society, 'Oh Ain't Modern Society Awful?'!</li>
        <li>Heh-haa! Super squeaky bum time!</li>
        <li>Sorry, checking all the water in this area; there's an escaped fish.</li>
        <li>You hit me with a cricket bat.</li>
        <li>You hit me with a cricket bat. They're not aliens, they're Earth…liens! Aw, you're all Mr. Grumpy Face
          today.
          Father Christmas. Santa Claus. Or as I've always known him: Jeff.</li>

        <li>I'm the Doctor. Well, they call me the Doctor. I don't know why. I call me the Doctor too. I still don't
          know
          why.</li>
        <li>Sorry, checking all the water in this area; there's an escaped fish.</li>
        <li>I am the last of my species, and I know how that weighs on the heart so don't lie to me!</li>
        <li>You know when grown-ups tell you 'everything's going to be fine' and you think they're probably lying to
          make
          you feel better? I'm nobody's taxi service; I'm not gonna be there to catch you every time you feel like
          jumping
          out of a spaceship.</li>

        <li>Annihilate? No. No violence. I won't stand for it. Not now, not ever, do you understand me?! I'm the Doctor,
          the Oncoming Storm - and you basically meant beat them in a football match, didn't you? The way I see it,
          every
          life is a pile of good things and bad things.…hey.…the good things don't always soften the bad things; but
          vice-versa the bad things don't necessarily spoil the good things and make them unimportant.</li>

        <li>It's a fez. I wear a fez now. Fezes are cool. The way I see it, every life is a pile of good things and bad
          things.…hey.…the good things don't always soften the bad things; but vice-versa the bad things don't
          necessarily
          spoil the good things and make them unimportant.</li>

        <li>*Insistently* Bow ties are cool! Come on Amy, I'm a normal bloke, tell me what normal blokes do! They're not
          aliens, they're Earth…liens! Annihilate? No. No violence. I won't stand for it. Not now, not ever, do you
          understand me?! I'm the Doctor, the Oncoming Storm - and you basically meant beat them in a football match,
          didn't you?</li>

        <li>It's a fez. I wear a fez now. Fezes are cool. I am the last of my species, and I know how that weighs on the
          heart so don't lie to me! Heh-haa! Super squeaky bum time! No, I'll fix it. I'm good at fixing rot. Call me
          the
          Rotmeister. No, I'm the Doctor. Don't call me the Rotmeister.</li>

        <li>I'm the Doctor. Well, they call me the Doctor. I don't know why. I call me the Doctor too. I still don't
          know
          why. No, I'll fix it. I'm good at fixing rot. Call me the Rotmeister. No, I'm the Doctor. Don't call me the
          Rotmeister.</li>

        <li>Stop talking, brain thinking. Hush. *Insistently* Bow ties are cool! Come on Amy, I'm a normal bloke, tell
          me
          what normal blokes do! Annihilate? No. No violence. I won't stand for it. Not now, not ever, do you understand
          me?! I'm the Doctor, the Oncoming Storm - and you basically meant beat them in a football match, didn't you?
        </li>

        <li>You hit me with a cricket bat. Stop talking, brain thinking. Hush. Annihilate? No. No violence. I won't
          stand
          for it. Not now, not ever, do you understand me?! I'm the Doctor, the Oncoming Storm - and you basically meant
          beat them in a football match, didn't you?</li>

        <li>Saving the world with meals on wheels. Annihilate? No. No violence. I won't stand for it. Not now, not ever,
          do you understand me?! I'm the Doctor, the Oncoming Storm - and you basically meant beat them in a football
          match, didn't you?</li>

        <li>They're not aliens, they're Earth…liens! Heh-haa! Super squeaky bum time! *Insistently* Bow ties are cool!
          Come on Amy, I'm a normal bloke, tell me what normal blokes do! Stop talking, brain thinking. Hush.</li>

        <li>You know how I sometimes have really brilliant ideas? Annihilate? No. No violence. I won't stand for it. Not
          now, not ever, do you understand me?! I'm the Doctor, the Oncoming Storm - and you basically meant beat them
          in
          a football match, didn't you?</li>
      </ul>

      <footer>type your message here</footer>
    </div>
  </div>
</body>

</html>

【讨论】:

    猜你喜欢
    • 2012-04-04
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    相关资源
    最近更新 更多