我正在尝试构建一个带有标题栏,导航标题,页脚和中间主要内容区域的Web布局(侧边栏和主视图分为两部分)。
我打算使用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>
解
将网格高度设置为视口高度 - 将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>
如何使第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>