我正在尝试构建一个带有标题栏,导航标题,页脚和中间主要内容区域的网页布局(侧边栏和主视图分为两部分)。
我打算为它使用CSS Grid布局。我当前的代码管理它,除了:主要内容(和侧边栏)调整其内容。那不是我想要的。
如何让第3个网格行填充所有剩余的垂直空间?
* {
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;
}

<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>
&#13;
答案 0 :(得分:2)
<强>解决方案强>
将网格高度设置为视口高度 - 将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;
}
<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>
答案 1 :(得分:2)
如何让第3个网格行填充所有剩余的垂直空间?
在您的布局中,没有剩余的垂直空间。由于容器没有定义的高度,因此其高度取决于内容的高度。因此,没有额外的空间可供分发。
但是,如果您的容器具有视口的高度......
.grid { height: 100vh }
...然后你可以获得侧边栏和主要内容(统称为第三行)以获取所有剩余高度:
.grid { grid-template-rows: min-content min-content 1fr min-content; }
而不是
.grid { grid-template-rows: min-content min-content auto min-content; }
* {
box-sizing: border-box;
}
.grid {
height: 100vh;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
grid-template-rows: min-content min-content 1fr 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;
}
body {
margin: 0;
}
<div class="grid">
<div class="title">
<h1>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">
<h2>Main content</h2>
<br>Should occupy all the remaining space.
</div>
</div>