html - 如何将网格元素高度设置为其余空间

标签 html css css-grid

在下面的示例中 - navbar27px 高度,topauto 高度;

问题 - 如果我在 story 中有一些内容 - 滚动不起作用。它变成了 - 内容高度?

如何设置 story 高度 - 作为从 divtop 到屏幕底部的整个剩余空间,这样滚动条就可以工作了。

* {
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
}

.navbar {
  position: sticky;
  top: 0;
  width: 100%;
  background: lightseagreen;
  color: white;
  height: 27px;
}

.grid {
  position: fixed;
  top: 27px;
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: calc(100vh - 27px);
  grid-column-gap: 9px;
  background: silver;
}

.grida,
.gridb {
  display: grid;
  grid-template-rows: auto 1fr;
  background: gold;
}

.divtop {
  background: lightgreen;
  height: auto;
}

.story {
  overflow-y: scroll;
}
<div class='navbar'>NAVBAR</div>
<div class='grid'>
  <div class='grida'>
    <div class='divtop'>TOP</div>
    <div class='story'>STORY</div>
  </div>
  <div class='gridb'>
    <div class='divtop'>TOP</div>
    <div class='story'>STORY</div>
  </div>
</div>

最佳答案

我只是根据您的网格高度为您的 grida,gridb 类添加了 max-height。评论以获取更多信息。我希望这个解决方案会有所帮助。

* {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
}

.navbar {
    position: sticky;
    top: 0;
    width: 100%;
    background: lightseagreen;
    color: white;
    height: 27px;
}

.grid {
    position: fixed;
    top: 27px;
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    height: calc(100vh - 27px);
    grid-column-gap: 9px;
    background: silver;
}

.grida,
.gridb {
    display: grid;
    grid-template-rows: auto 1fr;
    background: gold;
    max-height: calc(100vh - 27px);
}

.divtop {
    background: lightgreen;
    height: auto;
}
.story {
    overflow-y: scroll;
}
<div class='navbar'>NAVBAR</div>
<div class='grid'>
    <div class='grida'>
        <div class='divtop'>TOP</div>
        <div class='story'>STORY</div>
    </div>
    <div class='gridb'>
        <div class='divtop'>TOP</div>
        <div class='story'>STORY</div>
    </div>
</div>

关于html - 如何将网格元素高度设置为其余空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55255679/

相关文章:

javascript - 如何动态添加/删除表单中的 2 个或更多字段

css - 两种不同尺寸之间的响应式设计

html - 如何为 HTML div 标签设置边框

css - 在 CSS Grid 中,为什么 1FR+9FR 在小屏幕尺寸下的表现与 10FR 不同?

html - 如何制作带有方格图案的响应式网格?

php - 检查网站宽度的最准确的自动化方法是什么?

python - 尽管设置正确,媒体中的 Django 图像仍不显示

html - 响应式设计 - Iphone 纵向 vs 横向

css - 带有 tailwindcss 的特殊网格布局