如何阻止 flex 元素增长超过 window

How to stop flex element from growing over window

嘿,我目前正在努力解决以下问题:我有 <div class="result-text"> 包含文本。我希望 <header><footer> 始终可见,但如果 div 中的文本太多,它们就会消失,我可以滚动。这不是我想要的。

<div> 应该是可滚动的。

“主”滚动条不应该存在。唯一可以滚动的是包含所有文本的 <div> 。如果我调整 window 的大小,则页眉、侧边栏和页脚必须始终可见。带有文本的 <div> 元素应始终填充剩余的文本。

html,
body {
  margin: 0;
  background-color: gray;
}

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  display: flex;
  flex: 1;
  background-color: #00ac17;
}

header {
  height: 35px;
  background-color: #004d20;
}

footer {
  height: 25px;
  background-color: #49fa7e;
  color: rgb(19, 19, 19);
}

.sidebarleft {
  flex: 0 0 55px;
  background-color: blue;
}


.data {
  flex-grow: 1;
  background-color: rgb(185, 46, 46);
}

.textcontent {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 50%;
  float: left;
  background-color: rgb(152, 43, 255);
}

.diagram {
  height: 100%;
  width: 50%;
  float: left;
  background-color: rgb(113, 1, 165);
}

.input-textarea {
  resize: none;
  height: 100px;
}

.result-text {
  background-color: grey;
  flex: 1;
  overflow: scroll;
  text-align: justify;
  padding: 20px;
}

.button1 {
  flex: 0;
}

.button2 {
  flex: 1;
}

.button3 {
  flex: 1;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Layout</title>
  </head>
  <body>
    <div class="wrapper">
      <header></header>
      <main>
        <div class="sidebarleft"></div>
        <div class="data">
          <div class="textcontent">
            <textarea class="input-textarea"></textarea>
            <button class="button1">button1</button>
            <div class="result-text">
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text .
            </div>
          </div>
          <div class="diagram"></div>
        </div>
      </main>
      <footer></footer>
    </div>
  </body>
</html>

我制作了一个 fiddle,您可以查看其中包含我的所有代码:https://jsfiddle.net/n3zyLgcb/4/

下面的设计应该保留,如果我调整整个 window:

但是如果文本太多,它看起来像这样:(不是我想要的行为)

不想要最右边的滚动条:(像@Faizal_Hussain说的那样改变了我的代码。)

html,
body {
  margin: 0;
  background-color: gray;
}

.wrapper {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  display: flex;
  flex: 1;
  background-color: #00ac17;
}

header {
  height: 35px;
  background-color: #004d20;
  position:fixed;
  width:100%;
}

footer {
  height: 25px;
  background-color: #49fa7e;
  color: rgb(19, 19, 19);
}

.sidebarleft {
  flex: 0 0 55px;
  background-color: blue;
}


.data {
  flex-grow: 1;
  background-color: rgb(185, 46, 46);
}

.textcontent {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 50%;
  float: left;
  background-color: rgb(152, 43, 255);
}

.diagram {
  height: 100%;
  width: 50%;
  float: left;
  background-color: rgb(113, 1, 165);
}

.input-textarea {
  resize: none;
  height: 100px;
}

.result-text {
  background-color: grey;
  flex: 1;
  overflow: scroll;
  text-align: justify;
  padding: 20px;
  max-height:500px;
  overflow:scroll;
}

.button1 {
  flex: 0;
}

.button2 {
  flex: 1;
}

.button3 {
  flex: 1;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Layout</title>
  </head>
  <body>
    <div class="wrapper">
      <header>nk</header>
      <main>
        <div class="sidebarleft"></div>
        <div class="data">
          <div class="textcontent">
            <textarea class="input-textarea"></textarea>
            <button class="button1">button1</button>
            <div class="result-text">
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text . Here is a lot of text . Here is a lot of text . Here is a
              lot of text . Here is a lot of text . Here is a lot of text . Here
              is a lot of text . Here is a lot of text . Here is a lot of text .
              Here is a lot of text . Here is a lot of text . Here is a lot of
              text .
            </div>
          </div>
          <div class="diagram"></div>
        </div>
      </main>
      <footer>mkmk</footer>
    </div>
  </body>
</html>

因为页眉和页脚都处于静态位置,您可以在此处查看静态位置,https://www.w3schools.com/css/css_positioning.asp

修复的方式应该是当我们滚动时这些页眉和页脚的位置不应该改变,为此添加位置固定, 喜欢

header {
  height: 35px;
  background-color: #004d20;
  position:fixed;
  width:100%;
  top : 0
}

footer {
height: 25px;
background-color: #49fa7e;
color: rgb(19, 19, 19);
position : fixed;
bottom : 0
width : 100%
}

main{
overflow-y: auto;
}