CSS网格行应始终覆盖

时间:2019-04-01 08:18:59

标签: html css css3 css-grid

在此示例中,我使用CSS网格。在这种情况下,网格包含的内容并不那么重要。

容器.wrap的背景为红色。我希望asidemain行一直向下扩展以填充.wrap。这意味着应该不再看到灰色和白色覆盖的红色背景。

如本例所示,我使用1fr,但在这种情况下并没有太大作用。我也尝试过auto,但没有区别。

* {
  padding: 0;
  margin: 0;
  color: #fff;
}

.wrap {
  height: 300px;
  background: red;
}

.content {
  background: green;
  display: grid;

  grid-template-areas: "header header"
                        "aside main"
                       "footer footer";
                       
  grid-template-columns: 300px 1fr;
  grid-template-rows: 48px 1fr 50px;
}

header {
  grid-area: header;
  background: black;
}

aside {
  grid-area: aside;
  background: #333;
}

main {
  grid-area: main;
  background: #fff;
  color: #000;
}

footer {
  grid-area: footer;
  background: blue;
}
<div class="wrap">
  <div class="content">
    <header>Header</header>
    <aside>Aside</aside>
    <main>Main</main>
    <footer>Footer</footer>
  </div>
</div>

3 个答案:

答案 0 :(得分:5)

您还需要在height: 100%上设置.content,以便网格填充.wrap的父级,从而设置height

* {
  padding: 0;
  margin: 0;
  color: #fff;
}

.wrap {
  height: 300px;
  background: red;
}

.content {
  background: green;
  display: grid;

  grid-template-areas: "header header"
                        "aside main"
                       "footer footer";
                       
  grid-template-columns: 300px 1fr;
  grid-template-rows: 48px 1fr 50px;
  height: 100%; /* Added */
}

header {
  grid-area: header;
  background: black;
}

aside {
  grid-area: aside;
  background: #333;
}

main {
  grid-area: main;
  background: #fff;
  color: #000;
}

footer {
  grid-area: footer;
  background: blue;
}
<div class="wrap">
  <div class="content">
    <header>Header</header>
    <aside>Aside</aside>
    <main>Main</main>
    <footer>Footer</footer>
  </div>
</div>

答案 1 :(得分:3)

如果我正确理解了您的问题,则应该在height:100%上添加.content,这将导致<aside><main>元素垂直扩展以填充.wrap元素:

* {
  padding: 0;
  margin: 0;
  color: #fff;
}

.wrap {
  height: 300px;
  background: red;
}

.content {
/* Add this */
  height:100%;
  background: green;
  display: grid;

  grid-template-areas: "header header"
                        "aside main"
                       "footer footer";
                       
  grid-template-columns: 300px 1fr;
  grid-template-rows: 48px 1fr 50px;
}

header {
  grid-area: header;
  background: black;
}

aside {
  grid-area: aside;
  background: #333;
}

main {
  grid-area: main;
  background: #fff;
  color: #000;
}

footer {
  grid-area: footer;
  background: blue;
}
<div class="wrap">
  <div class="content">
    <header>Header</header>
    <aside>Aside</aside>
    <main>Main</main>
    <footer>Footer</footer>
  </div>
</div>

答案 2 :(得分:0)

您可以设置高度

set.seed(123)
b <- matrix(rnorm (20000 * 50), nrow=20000,ncol=50)
load("a.RData")

identical(a,b)
[1] TRUE


    * {
      padding: 0;
      margin: 0;
      color: #fff;
    }

    .wrap {
      height: 300px;
      background: red;
    }

    .content {
      background: green;
      display: grid;

      grid-template-areas: "header header"
                            "aside main"
                           "footer footer";
                           
      grid-template-columns: 300px 1fr;
      grid-template-rows: 48px 1fr 50px;
    }

    header {
      grid-area: header;
      background: black;
    }

    aside {
      grid-area: aside;
      background: #333;
      height: 500px;
    }

    main {
      grid-area: main;
      background: #fff;
      color: #000;
      height: 500px;
    }

    footer {
      grid-area: footer;
      background: blue;
    }