【问题标题】:Scrolling only inside flexbox child [duplicate]仅在 flexbox 子项内滚动 [重复]
【发布时间】:2021-06-19 08:27:08
【问题描述】:

我是一名 CSS 初学者,我正在尝试使用滚动和弹性框。这是我目前的尝试

body {
  margin: 0;
  height: 100vh; 
}

#root {
  height: 100vh; 
  display: flex; 
  flex-direction: column;
}

header, footer {
  min-height: 50px; 
  background-color:blue; 
  flex: 0 0 auto
}

#main-panel {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  background-color: grey;
}

#main-panel-header {
  height: 20px;
  width: 100%;
  flex: 0 0 auto;
  background-color: red;
  text-align: center;
}

#main-panel-content {
  background-color: orange;  
  overflow-y: auto;
  flex: 1 1 auto;
}

ul {
  list-style-type: none;
  text-align: center;
  line-height: 100px;
  font-weight: bold;
  font-family: sans-serif;
  font-size: 1em;
  color: green;
}
<div id="root">
  <header id="header">Header</header>
  <div id="main-panel">
    <div id="main-panel-header">Main Panel Header 2</div>
    <div id="main-panel-content">
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
      </ul>
    </div>
  </div>
  <footer id="footer">Footer</footer>
</div>

我正在尝试使main-panel-content 滚动,并始终保持页眉和页脚可见。现在,滚动发生在整个页面上,而不是我的主面板内容。由于整个设计很灵活,我不想为主面板使用固定的高度。

关于如何调试此类问题的任何提示?

【问题讨论】:

  • min-height:0 到 #main-panel 并且您在 overflow-y 中有错字
  • 感谢您的帮助!关于为什么需要min-height 的任何指示?
  • 我添加了一个副本
  • 刚看到,谢谢!

标签: html css flexbox scrollbar


【解决方案1】:

您可以将以下代码添加到#main-panel

overflow-y:scroll;  
display:block;

您可以找到完整代码:https://codepen.io/chamman01/pen/poeYMwG

希望你得到你的答案

【讨论】:

  • 谢谢,但我希望红色标题也可以修复且不可滚动。在主面板上设置 min-height:0 按照 cmets 中的建议进行修复。
猜你喜欢
  • 1970-01-01
  • 2018-02-07
  • 2021-12-12
  • 2021-04-28
  • 2017-10-28
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 2018-05-25
相关资源
最近更新 更多