【问题标题】:How have 2 sections fill full page with header / main / footer in grid如何让 2 个部分在网格中用页眉/主/页脚填充整页
【发布时间】:2021-10-27 12:40:55
【问题描述】:

大家好,

我需要帮助,我想在我的<main> 中有 2 个部分,我希望第一部分填满整页,如果我滚动我让第二部分再次填满整页。所以我对此进行了测试:

html, body {
 width: 100%;
 height: 100%;
 margin: 0;
}

.container {
 display: grid;
 grid-template-rows: auto 1fr auto;
 grid-template-columns: 100%;
 min-height: 100%;
}

header,
footer {
  height: 100px;
  background-color: rgba(0,0,0,0.7);
}

header {
}

main {
}

section {
    display: block;
    background: #CFF;
}

.one {
    background: red;
    height: 100%;
}

.two {
    background: cyan;
    height: 100%;
}
<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
  </head>
  <body>
     <div class="container">
         <header></header>
         <main>
           <section class="one"></section>
           <section class="two"></section>
         </main>
         <footer></footer>
     </div>
  </body>
</html>

如您所见,我的页脚位于第一部分的底部,但我希望他位于第二部分的真正底部。 如果可能的话,我想修复我的标题,但如果我把 position : fixed; 放在 header {} 我的标题是隐藏的??

抱歉我的英语不好。

你能帮帮我吗,祝你有美好的一天!

【问题讨论】:

  • html, body { height: 100%; }根本没有影响。 height: 100% 表示使用父级的全高。父母必须有一个定义的宽度而不是一个计算的宽度来适应内容。在任何情况下,html 和 body 都没有父级可以使用 height: 100%;。你想要的是min-height: 100vh;

标签: html css css-grid responsive


【解决方案1】:
  1. 首先删除所有height: 100%,否则它们根本不会与您的声明一起使用。
  2. 这里不需要网格。
  3. 只需添加:.one { height: calc(100vh - 100px);
  4. 添加.two { height: calc(100vs - 200px);以减去页脚和页眉高度。
  5. 添加:header { position: sticky; }。默认情况下,这不会将标题移出流。不过它会粘在顶部。

由于您的页脚和页眉都有 100 像素的固定高度,您可以使用 calc 函数来调整这两个部分的大小。

html, body {
 margin: 0;
}


header,
footer {
  height: 100px;
  background-color: rgba(0,0,0,0.7);
}

header {
  position: sticky;
  top: 0;
}

section {
    display: block;
    background: #CFF;
}

.one {
    background: red;
    height: calc(100vh - 100px);
}

.two {
    background: cyan;
    height: calc(100vh - 200px);
}
<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
  </head>
  <body>
     <div class="container">
         <header></header>
         <main>
           <section class="one"></section>
           <section class="two"></section>
         </main>
         <footer></footer>
     </div>
  </body>
</html>

【讨论】:

    【解决方案2】:

    *{
    margin:0;
    padding:0;
    }
    header {
    position: fixed;
    height: 20vh;
    width: 100vw;
    z-index: 1;
    background-color: black;
    }
    
    section {
        display: block;
        background: #CFF;
        
    }
    
    .one {
        top:20vh;
        background: red;
        height: 100vh;
    }
    
    .two {
        background: cyan;
        height: 100vh;
    }
    footer{
    height: 20vh;
    background-color: blue;
    }
    <!DOCTYPE html>
    <html lang="fr">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title></title>
      </head>
      <body>
         <div class="container">
             <header>434343</header>
             <main>
               <section class="one"></section>
               <section class="two"></section>
             </main>
             <footer></footer>
         </div>
      </body>
    </html>

    您可以使用属性高度:100vh;将任何 div 的高度设置为视口的高度。 100vh 用于固定元素的高度或 div 屏幕或视口的高度。我也修复了您的“位置:已修复”问题。现在您可以滚动并且您的标题将可见。您可以通过修复标题的宽度和高度来实现这一点,就像我已经修复的那样。

    【讨论】:

      【解决方案3】:

      在这种情况下,您可以轻松地将one 部分和two 部分中的percentage 单位更改为vh 类。就是这样 !在这里您可以阅读更多关于units 的信息。在header 选择器上设置position: sticky 和top: 0,以使您的导航按您的意愿工作;)

      html, body {
       width: 100%;
       height: 100%;
       margin: 0;
      }
      
      .container {
       display: grid;
       grid-template-rows: auto 1fr auto;
       grid-template-columns: 100%;
       min-height: 100%;
      }
      
      header,
      footer {
        height: 100px;
        background-color: rgba(0,0,0,0.7);
      }
      
      header {
       position: sticky;
       top: 0;
      }
      
      main {
      }
      
      section {
          display: block;
          background: #CFF;
      }
      
      .one {
          background: red;
          height: 100vh;
      }
      
      .two {
          background: cyan;
          height: 100vh;
      }
      <!DOCTYPE html>
      <html lang="fr">
        <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title></title>
        </head>
        <body>
           <div class="container">
               <header></header>
               <main>
                 <section class="one"></section>
                 <section class="two"></section>
               </main>
               <footer></footer>
           </div>
        </body>
      </html>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-20
        • 1970-01-01
        • 1970-01-01
        • 2021-06-24
        相关资源
        最近更新 更多