行没有填满高度

问题描述 投票:1回答:2

我一直在尝试使用CSS Grid并尝试开发一个简单的网页。我正在使用fr单元来均匀地创建显示我的内容并使其响应。均匀地创建列以填充屏幕的空间。但另一方面,行没有填满空间。它们正在创建,但不能填满整个屏幕高度。

这是我的HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link href="style.css" type="text/css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900" rel="stylesheet">
  <title>Name of Site</title>
</head>
<body>
  <div class="container">
    <header class="header box">
      <h1>Name</h1>
    </header>
    <nav class="info box">
      <h2>General Information</h2>
    </nav>
    <div class="right box">
      <h3>Right</h3>
    </div>
    <footer class="footer box">
      <h2>Footer</h2>
    </footer>
  </div>
</body>
</html>

这是我的CSS代码:

/* Resets */

  body {
  margin: 0;
}

/* Typography */

h1, h2, h3 {
  font-family: 'Roboto';
  text-align: center;
  font-variant-caps: all-petite-caps;
}

ul, li, p {
  font-family: 'Roboto';
}

.box {
  color: #ffffff;
  padding: 10px 10px;
}

/* CSS Grid */
@media (min-width: 960px) {
  .container {
    display: grid;
    grid-template-areas:
      "h r"
      "i r"
      "f r"; 
    grid-template-columns: 1fr 2fr;
    grid-template-rows: 1fr 3fr 1fr;
  }
}

.header {
  grid-area: h;
  background-color: #57B8BF;
}

.info {
  grid-area: i;
  background-color: #3A7B7F;
}

.footer {
  grid-area: f;
  background-color: #1D3D40;
  color: #ffffff;
}

.right {
  grid-area: r;
  background-color: #ffffff;
  color: black;
}

我一直得到的结果是:

.

我有Firefox Grid显示,因此您可以看到网格的布局。但是我用红色写的图像是在制作行时现在包含的空间。页脚是否应该在列的末尾?

如何让行均匀分布页面的高度,就像使用列一样?

html css css3 css-grid
2个回答
0
投票

只需将你的containerhtml, body调整为100%身高

html, body {
  height: 100%;
}

.container {
  height: 100%;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
}

/* Typography */

h1, h2, h3 {
  font-family: 'Roboto';
  text-align: center;
  font-variant-caps: all-petite-caps;
}

ul, li, p {
  font-family: 'Roboto';
}

.box {
  color: #ffffff;
  padding: 10px 10px;
}

/* CSS Grid */
@media (min-width: 960px) {
  .container {
    height: 100%;
    display: grid;
    grid-template-areas:
      "h r"
      "i r"
      "f r"; 
    grid-template-columns: 1fr 2fr;
    grid-template-rows: 1fr 3fr 1fr;
  }
}

.header {
  grid-area: h;
  background-color: #57B8BF;
}

.info {
  grid-area: i;
  background-color: #3A7B7F;
}

.footer {
  grid-area: f;
  background-color: #1D3D40;
  color: #ffffff;
}

.right {
  grid-area: r;
  background-color: #ffffff;
  color: black;
}
<div class="container">
    <header class="header box">
      <h1>Name</h1>
    </header>
    <nav class="info box">
      <h2>General Information</h2>
    </nav>
    <div class="right box">
      <h3>Right</h3>
    </div>
    <footer class="footer box">
      <h2>Footer</h2>
    </footer>
  </div>

0
投票

我有同样的问题,使用这个:

.container {
  width: 100%;
  height: 100vh;
}
© www.soinside.com 2019 - 2024. All rights reserved.