使元素在网格部分内滚动

Make an element scroll inside a grid section

我正在尝试在 div 中创建一个可滚动区域,如下图所示。我只希望可滚动区域在不同的屏幕高度上改变高度并相应地滚动。

问题是,如果可滚动内容足够大可以滚动,它会使整个页面滚动。如果它很小,页脚就会正确地位于底部。

这是我目前所拥有的

* {
  margin: 0;
  padding: 0;
  width: 100%;
}

.container {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
}

.header {
  height: 30px;
  background-color: lightblue;
}

.footer {
  height: 30px;
  background-color: lightblue;
}

.content {
  display: grid;
  grid-template-rows: auto 1fr;
}

.profile {
  height: 60px;
  background-color: lightpink;
}

.tabs {
  height: 20px;
  background-color: lightgreen;
}

.scroller {
  background-color: cyan;
  height: 100%;
  overflow-y: scroll;
}

.scrollable-content {
  background-color: yellow;
  width: 200px;
  height: 600px;
  margin: 0 auto;
  position: relative;
}

span {
  bottom: 0;
  left: 0;
  position: absolute;
}
<div class="container">

  <div class="header">Header</div>
  
  <div class="content">
    <div class="profile">Profile</div>
    <div class="tab-control">
      <div class="tabs">Tabs</div>
      <div class="scroller">
        <div class="scrollable-content">scrollable content<span>end</span></div>
      </div>
    </div>
  </div>
  
  <div class="footer">Footer</div>

</div>

任何帮助都适用

设置滚动条的固定高度。 100vh = 浏览器高度 - 140px(页面上所有其他元素的累积高度)

在要滚动的bar上设置overflow-y: auto,.scrollable-content的高度可以任意设置。

.scroller {
  background-color: cyan;
  height: calc(100vh - 140px);
  overflow-y: scroll;
}

.scrollable-content {
  background-color: yellow;
  width: 200px;
  height: 600px;
  margin: 0 auto;
  position: relative;
  overflow-y: auto;
}

如果您将某些元素设置为 overflow: hidden;,它会起作用。为 .container.content.tab-control

设置
.container, .content, .tab-control {
  overflow: hidden;
}

.scroller 元素会有一个小问题,部分内容会被页脚覆盖。

要解决这个问题,还要添加以下内容:

.tab-control {
  display: flex;
  flex-direction: column;
}

.scroller {
  flex: 100% 1 1;
}