如何在 CSS 网格中垂直使用所有可用空间?

tho*_*low 9 html css css-grid

我正在尝试构建一个带有标题栏、导航页眉、页脚和中间的主要内容区域的 Web 布局(侧边栏和主视图一分为二)。

我打算使用 CSS Grid 布局。我当前的代码管理这一切,除了:主要内容(和侧边栏)根据其内容进行调整。这不是我想要的。

如何让第三个网格行填充所有剩余的垂直空间?

* {
  box-sizing: border-box;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  grid-template-rows: min-content min-content auto min-content;
  grid-gap: 10px;
  grid-template-areas: "header header" "nav nav" "sidebar main" "footer footer";
}

.title {
  grid-area: header;
  background-color: #1BC336;
}

.navigation {
  grid-area: nav;
  background-color: #C3A21B;
}

.sidebar {
  grid-area: sidebar;
  background-color: gold;
  overflow: auto;
}

.main {
  grid-area: main;
  background-color: #1BC3B9;
  overflow: auto;
}

.footer {
  grid-area: footer;
  background-color: #5C1BC3;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div class="title">
    <h3>Title Bar</h1>
  </div>
  <div class="navigation">Navbar</div>
  <div class="sidebar">Sidebar:<br>Info-type stuff about what's currently being shown in main</div>
  <div class="footer">Footer</div>
  <div class="main">
    <h1>Main content</h2><br>Should occupy all the remaining space. <br>Test<br>Test<br>Test<br>Test<br>Test</div>
</div>
Run Code Online (Sandbox Code Playgroud)

kuk*_*kuz 7

解决方案

网格高度设置为视口高度- 添加height: 100vh.grid并将默认浏览器body边距重置为零。


为什么

这是因为,除非容器(你给了它display: grid)有一组height,将采取尽可能多的空间及其内容。因此,当您给出高度时,现在有可用空间来填充. 请参阅下面的演示:

* {
  box-sizing: border-box;
}
body { /* ADDED */
  margin: 0;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  grid-template-rows: min-content min-content auto min-content;
  grid-gap: 10px;
  grid-template-areas: "header header" "nav nav" "sidebar main" "footer footer";
  height: 100vh; /* ADDED */
}

.title {
  grid-area: header;
  background-color: #1BC336;
}

.navigation {
  grid-area: nav;
  background-color: #C3A21B;
}

.sidebar {
  grid-area: sidebar;
  background-color: gold;
  overflow: auto;
}

.main {
  grid-area: main;
  background-color: #1BC3B9;
  overflow: auto;
}

.footer {
  grid-area: footer;
  background-color: #5C1BC3;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grid">
  <div class="title">
    <h3>Title Bar</h1>
  </div>
  <div class="navigation">Navbar</div>
  <div class="sidebar">Sidebar:<br>Info-type stuff about what's currently being shown in main</div>
  <div class="footer">Footer</div>
  <div class="main">
    <h1>Main content</h2><br>Should occupy all the remaining space. <br>Test<br>Test<br>Test<br>Test<br>Test</div>
</div>
Run Code Online (Sandbox Code Playgroud)


Mic*_*l_B 5

如何让第三个网格行填充所有剩余的垂直空间?

在您的布局中,没有剩余的垂直空间。由于您的容器没有定义的高度,它的高度基于内容的高度。所以没有额外的空间来分配。

但是,如果您的容器具有视口的高度...

.grid { height: 100vh }
Run Code Online (Sandbox Code Playgroud)

...然后您可以获取侧边栏和主要内容(统称为第三行)以获取所有剩余高度:

.grid { grid-template-rows: min-content min-content 1fr min-content; }
Run Code Online (Sandbox Code Playgroud)

代替

.grid { grid-template-rows: min-content min-content auto min-content; }
Run Code Online (Sandbox Code Playgroud)

.grid { height: 100vh }
Run Code Online (Sandbox Code Playgroud)
.grid { grid-template-rows: min-content min-content 1fr min-content; }
Run Code Online (Sandbox Code Playgroud)