如何用 CSS-grid 创建粘性 header?

How to create a sticky header with CSS-grid?

我想在使用 CSS 网格滚动时使我的 header 变粘。

我已经尝试过这里提出的解决方案: 含义:

position: sticky; 
top:0;

然而它不起作用...

.wrapper {
  display: grid;
  grid-template-areas: "header" "middle" "footer";
  grid-template-rows: auto 1fr auto;
  height: 100vh;
}


/* Header */

header {
  order: 1;
  grid-area: header;
  display: grid;
  grid-gap: 100px;
  grid-template-areas: "logo nav";
  grid-template-columns: auto 1fr;
  grid-auto-flow: column;
  position: sticky;
  top: 0;
}

nav {
  display: grid;
  grid-area: nav;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}


/* Middle */

.middle {
  order: 2;
  grid-area: middle;
  display: grid;
  grid-template-columns: 50px 1fr 50px;
}

.middle>* {
  grid-column: 2 / -2;
}


/* Footer */

footer {
  order: 3;
  grid-area: footer;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
}

.footer-links {
  display: grid;
  grid-column: 2 /-2;
  grid-template-columns: 1fr;
  grid-auto-flow: column;
  align-items: center;
}
<body>
  <div class="wrapper">

    <!-- Header -->
    <header>
      <a href="./index.html" title="Welcome" class="logo"><img src="img/logo_jaeaess_glitch.png" alt="Logo of the VJ Jääß (Jess de Jesus)" style="width:42px;height:42px"></a>
      <nav>
        <a href="./index.html" title="Welcome" class="welcome active">Welcome</a>
        <a href="./about.html" title="About" class="about">About</a>
        <a href="./artwork.html" title="Art Work" class="artwork">Art Work</a>
        <a href="./events.html" title="Events" class="events">Events</a>
      </nav>
    </header>
    <!-- Middle -->
    <section class="middle">
    </section>

    <footer>
      <div class="footer-links">
        <a href="https://www.instagram.com/jaeaess/" target="_blank">Instagram</a>
        <p>&copy; 2019 Jääß</p>
      </div>
    </footer>
  </div>

</body>

除了 header 向下滚动而不是保持固定...

之外,一切都按照我的意愿显示

(对于那些想知道的人,我下命令只是为了在开发的后期将其移动到媒体查询中)

感谢您的回答!

要使 header 在滚动时固定,您可以将其设为 position: fixed。这需要您为 header 设置一个固定的高度。这将使 header 元素在内容之上流动并忽略相对于 window.

的滚动

  
.wrapper {
  /* the header will not take any vertical place so shift wrapper down a bit*/
  margin-top: 3rem; 
  display: grid;
  /*I removed the header area as we don't need it anymore and would not work with fixed position anways*/
  grid-template-areas: "middle" "footer";
  grid-template-rows: 1fr auto;
   /*I set the wrapper height to `200vw` so its easier to see the header not scrolling. Also take maring top into account*/
  height: calc(200vh - 3rem);
}


/* Header */

header {
  display: grid;
  grid-gap: 100px;
  grid-template-areas: "logo nav";
  grid-template-columns: auto 1fr;
  grid-auto-flow: column;
  position: fixed;
  top: 0;
  height: 3rem;
  width: 100%;
}

nav {
  display: grid;
  grid-area: nav;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}


/* Middle */

.middle {
  order: 2;
  grid-area: middle;
  display: grid;
  grid-template-columns: 50px 1fr 50px;
}

.middle>* {
  grid-column: 2 / -2;
}


/* Footer */

footer {
  order: 3;
  grid-area: footer;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
}

.footer-links {
  display: grid;
  grid-column: 2 /-2;
  grid-template-columns: 1fr;
  grid-auto-flow: column;
  align-items: center;
}
<div class="wrapper">
  <!-- Header -->
  <header>
    <a href="./index.html" title="Welcome" class="logo"><img src="img/logo_jaeaess_glitch.png" alt="Logo of the VJ Jääß (Jess de Jesus)" style="width:42px;height:42px" /></a>
    <nav>
      <a href="./index.html" title="Welcome" class="welcome active">Welcome</a
          >
          <a href="./about.html" title="About" class="about">About</a>
      <a href="./artwork.html" title="Art Work" class="artwork">Art Work</a>
      <a href="./events.html" title="Events" class="events">Events</a>
    </nav>
  </header>
  <!-- Middle -->
  <section class="middle"></section>

  <footer>
    <div class="footer-links">
      <a href="https://www.instagram.com/jaeaess/" target="_blank">Instagram</a
          >
          <p>&copy; 2019 Jääß</p>
        </div>
      </footer>
    </div>

Ps.: 你现在有点过度使用 css 网格。它专为二维布局而设计。如果您使用 flexbox,您的布局会容易得多(!)。在 IE 11 中也使网格工作是 pain.