【问题标题】:How do you fill the remaining space with grid container in css?如何在 CSS 中用网格容器填充剩余空间?
【发布时间】:2021-09-06 05:29:02
【问题描述】:

我是新手。我正在尝试制作一个高度为 100vh 的网页,其中页眉和页脚将分别占用 80 像素和 50 像素。中间剩下的任何空间都应该完全由网格容器接管。到目前为止我确实成功了,但是中间容器只占用了它的内容需要的量。请参考下图:

另外,我的印象是页脚标签会自动将该部分放在视口的底部。但这也没有发生。我必须使用绝对定位吗?

* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}

html {
  font-size: 10px;
}

body {
  height: 100vh;
  overflow-x: hidden;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
}

header {
  padding-left: 40px;
  padding-right: 40px;
}

nav {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  min-height: 80px;
}

.nav-menuList {
  display: inline-flex;
  justify-content: space-between;
  align-items: center;
  gap: 4rem;
}

.nav-list {
  list-style-type: none;
  font-family: sans-serif;
  font-size: 1.4rem;
  font-weight: 500;
  line-height: 4.4rem;
  color: #000;
}

.grid-container {
  display: grid;
  grid-template-rows: 1fr 1fr;
  row-gap: 4rem;
  grid-template-areas:
    "h1"
    "h-btn";
  padding: 0 40px;
  align-self: center;
}

h1 {
  font-family: sans-serif;
  font-size: 4.8rem;
  font-weight: 300;
  line-height: 6.4rem;
  color: #000;
  grid-area: h1;
  width: 75%;
}

.button-group {
  grid-area: h-btn;
}

footer {
  padding: 0 40px;
}
<body>
  <div class="container">
    <header>
      <nav>
        <div class="logo">
          <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M0 4C0 1.79086 1.79086 0 4 0H28C30.2091 0 32 1.79086 32 4V28C32 30.2091 30.2091 32 28 32H4C1.79086 32 0 30.2091 0 28V4Z" fill="black" />
          </svg>
        </div>
        <div class="menu">
          <ul class="nav-menuList">
            <li class="nav-list">menu</li>
            <li class="nav-list">menu</li>
            <li class="nav-list">menu</li>
          </ul>
        </div>
      </nav>
    </header>
    <main>
      <div class="grid-container">
        <h1>Lorem ipsum dolor sit amet</h1>
        <div class="button-group">
          <button class="primary-btn hero-btn">CTA</button>
        </div>
      </div>
    </main>
    <footer>This is footer</footer>
  </div>

</body>

【问题讨论】:

  • 这样简单的设计也可以用flexbox来完成,这样会更容易。

标签: html css css-grid


【解决方案1】:

弹性盒解决方案:

  1. 应用于body 或弹性盒容器:min-height: 100vh;
  2. 申请header:height: 80px;footer:height: 50px;
  3. 添加flex-grow: 1; 使main 或内容框占用剩余空间:flex-grow: 1;

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

header {
  height: 80px;
}

footer {
  height: 50px;
}

main {
  flex-grow: 1;
}




/* for demonstration purpose only */
body {
  margin: 0;
}

header,
footer,
main {
  display: flex;
  justify-content: center;
  align-items: center;
}

header {
  background-color: pink;
}

main {
  background-color: lightblue;
}

footer {
  background-color: lightgreen;
}
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS-Grid 解决方案:

  1. 应用于body 或网格容器:min-height: 100vh;
  2. 定义行:grid-template-rows: 80px auto 50px;,这意味着auto 将占用所有剩余空间。

body {
  height: 100vh;
  display: grid;
  grid-template-rows: 80px auto 50px;  
}




/* for demonstration purpose only */
body {
  margin: 0;
}

header,
footer,
main {
  display: flex;
  justify-content: center;
  align-items: center;
}

header {
  background-color: pink;
}

main {
  background-color: lightblue;
}

footer {
  background-color: lightgreen;
}
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

【讨论】:

    【解决方案2】:

    您也可以将.container 设置为网格:

    * {
      margin: 0px;
      padding: 0px;
      box-sizing: border-box;
    }
    
    html {
      font-size: 10px;
    }
    
    body {
      height: 100vh;
      overflow-x: hidden;
    }
    
    .container {
      max-width: 1400px;
      margin: 0 auto;
      display: grid;
      grid-template-rows: minmax(80px, max-content) 1fr minmax(50px,max-content);
      height: 100%;
    }
    
    header {
      padding-left: 40px;
      padding-right: 40px;
    }
    
    nav {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      min-height: 80px;
    }
    
    .nav-menuList {
      display: inline-flex;
      justify-content: space-between;
      align-items: center;
      gap: 4rem;
    }
    
    .nav-list {
      list-style-type: none;
      font-family: sans-serif;
      font-size: 1.4rem;
      font-weight: 500;
      line-height: 4.4rem;
      color: #000;
    }
    
    .grid-container {
      display: grid;
      grid-template-rows: 1fr 1fr;
      row-gap: 4rem;
      grid-template-areas:
        "h1"
        "h-btn";
      padding: 0 40px;
      align-self: center;
      
    }
    
    h1 {
      font-family: sans-serif;
      font-size: 4.8rem;
      font-weight: 300;
      line-height: 6.4rem;
      color: #000;
      grid-area: h1;
      width: 75%;
    }
    
    .button-group {
      grid-area: h-btn;
    }
    
    footer {
      padding: 0 40px;
    }
    <body>
      <div class="container">
        <header>
          <nav>
            <div class="logo">
              <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 4C0 1.79086 1.79086 0 4 0H28C30.2091 0 32 1.79086 32 4V28C32 30.2091 30.2091 32 28 32H4C1.79086 32 0 30.2091 0 28V4Z" fill="black" />
              </svg>
            </div>
            <div class="menu">
              <ul class="nav-menuList">
                <li class="nav-list">menu</li>
                <li class="nav-list">menu</li>
                <li class="nav-list">menu</li>
              </ul>
            </div>
          </nav>
        </header>
        <main>
          <div class="grid-container">
            <h1>Lorem ipsum dolor sit amet</h1>
            <div class="button-group">
              <button class="primary-btn hero-btn">CTA</button>
            </div>
          </div>
        </main>
        <footer>This is footer</footer>
      </div>
    
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 2013-11-04
      • 1970-01-01
      • 2012-01-10
      • 2014-06-16
      相关资源
      最近更新 更多