css 如何在网格中占用所有可用的垂直空间?

6jjcrrmo  于 2023-04-01  发布在  其他
关注(0)|答案(2)|浏览(114)

我有一个css网格,我希望最后一行占据所有可用的垂直空间
我的代码片段的.main-content区域中的.r3应该占据剩余的空间。我如何才能做到这一点?
目前.r3的高度只是它需要的高度(height:auto;,如果你愿意)
这是我尝试过的一小段,你必须使用“整页”功能来可视化这种情况。

html,
body {
  margin: 0;
  padding: 0;
}

.app-root {
  display: flex;
  height: 100vh;
  background-color: #e0e0e0;
}
.app-grid {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 30px;
  grid-auto-columns: 1fr;
  gap: 0px 0px;
  grid-auto-flow: row;
  grid-template-areas:
    "navbar navbar"
    "sidebar main-content"
    "statusbar statusbar";
}

.navbar {
  grid-area: navbar;
  background-color: yellow;
}

.statusbar {
  grid-area: statusbar;
  background-color: black;
  color: white;
}

.sidebar {
  grid-area: sidebar;
  background-color: red;
}

.main-content {
  grid-area: main-content;
  padding: 12px;
  background-color: blue;
}

.container {
  display: grid;
  grid-template-columns: 1fr 300px;
  grid-template-rows: auto auto 1fr;
  gap: 15px 15px;
  grid-template-areas:
    "r1 r1"
    "r2 r2"
    "r3c1 r3c2";
}

.r1 {
  grid-area: r1;
  background-color: turquoise;
}
.r2 {
  grid-area: r2;
  background-color: brown;
}
.r3c1 {
  grid-area: r3c1;
  background-color: orange;
}
.r3c2 {
  grid-area: r3c2;
  background-color: white;
}
<div class="app-root">
  <div class="app-grid">
    <div class="navbar">
      nav goes brrr
    </div>

    <div class=sidebar>sidebar goes brr</div>
    <div class=main-content>

      <div class="container">
        <div class="r1">r1
        </div>

        <div class="r2">r2
        </div>

        <div class="r3c1">r3c1
        </div>

        <div class="r3c2">r3c2
        </div>
      </div>

    </div>

    <div class="statusbar">
      status goes brrr
    </div>
  </div>
</div>
u5i3ibmn

u5i3ibmn1#

如果对元素r1、r2 ...使用网格区域,则可以使用对齐拉伸

html,
body {
  margin: 0;
  padding: 0;
}

.app-root {
  display: flex;
  height: 100vh;
  background-color: #e0e0e0;
}

.app-grid {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 30px;
  grid-auto-columns: 1fr;
  gap: 0px 0px;
  grid-auto-flow: row;
  grid-template-areas: "navbar navbar" "sidebar main-content" "statusbar statusbar";
}

.navbar {
  grid-area: navbar;
  background-color: yellow;
}

.statusbar {
  grid-area: statusbar;
  background-color: black;
  color: white;
}

.sidebar {
  grid-area: sidebar;
  background-color: red;
}

.main-content {
  grid-area: main-content;
  padding: 12px;
  background-color: blue;
}

.container {
  display: grid;
  width: 100%;
  height: 100%;
  grid-template-columns: 1fr 300px;
  grid-template-rows: auto auto 1fr;
  gap: 15px;
}

.r1 {
  align-self: start;
  grid-area: 1 / 1 / 2 / 3;
  background-color: turquoise;
}

.r2 {
  grid-area: 2 / 1 / 3 / 3;
  align-self: start;
  background-color: brown;
}

.r3c1 {
  grid-area: 3 / 1 / 4 / 2;
  background-color: orange;
  align-self: stretch;
}

.r3c2 {
  grid-area: 3 / 2 / 4 / 3;
  background-color: white;
  align-self: stretch;
}
<div class="app-root">
  <div class="app-grid">
    <div class="navbar">
      nav goes brrr
    </div>

    <div class=sidebar>sidebar goes brr</div>
    <div class=main-content>

      <div class="container">
        <div class="r1">r1
        </div>

        <div class="r2">r2
        </div>

        <div class="r3c1">r3c1
        </div>

        <div class="r3c2">r3c2
        </div>
      </div>

    </div>

    <div class="statusbar">
      status goes brrr
    </div>
  </div>
</div>
qnzebej0

qnzebej02#

您只需要将最后一行的高度设置为1fr,将第二行的高度设置为auto。这将使第二行以空间作为其内容,而最后一行将占用所有剩余的空间。
你只需要编辑app-grid类就可以了:

grid-template-rows: 60px auto 1fr;

这将实现以下结果:

相关问题