固定元素到视口顶部

3
这是一种奇怪的困境,因为我最终使用flexbox来对齐我的侧边栏和标题菜单项,并使用网格来显示/分配空间给我的nav和标题组件。
我尝试过让仅基于网格或flexbox的侧边栏(或标题)保持粘性的方法,但它们完全不起作用。
这是一个我所拥有的示例:
<div class="grid-container">
    <nav>
        <div class="menu-item">Link</div>
    </nav>
    <div class="header"> Header Content Here </div>
    <main> Main Section. This should be able to scroll while Nav and 
           Header are sticking to the top
    </main>
</div>

CSS:

.grid-container {
    grid-template-columns: 200px 1fr;
    grid-template-areas: 
      "sidenav header"
      "sidenav main";
      box-shadow: 5px 10px !important;
  }

nav {
  grid-area: sidenav;
  box-shadow: 0 -1px 12px 2px whitesmoke;
  background-color: white;
  display: none;
  z-index: 100;
  flex-direction: column;
}

.header {
  grid-area: header;
  background-color: #f8f9ff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0px 16px;
}

main {
  grid-area: main;
  background-color: #f8f9ff;
}
2个回答

1
将网格和弹性框属性结合起来使用是正确的做法 - 您需要添加一些内容使它们正常工作:
  • 将视口高度添加到您的网格中,以便您可以使您的headersidebar粘性。
  • 您可以使用grid-template-rows指定header的高度(请参见下面的代码片段)
  • 添加overflow-y: auto来强制只有main溢出。

请参见下面的演示:

body  {
  margin: 0;
}

.grid-container {
  display: grid;
  height: 100vh; /* Add total height of the grid */
  grid-template-columns: 200px 1fr; /* sets sidenav width */
  grid-template-rows: 75px 1fr; /* sets header height */
  grid-template-areas: "sidenav header" "sidenav main";
  box-shadow: 5px 10px !important;
}

nav {
  grid-area: sidenav;
  box-shadow: 0 -1px 12px 2px whitesmoke;
  background-color: white;
  z-index: 100;
  border: 1px solid;
}

.header {
  grid-area: header;
  background-color: #f8f9ff;
  display: flex; /* flexbox to center items */
  align-items: center;
  justify-content: space-between;
  padding: 0px 16px;
  border: 1px solid;
}

main {
  grid-area: main;
  background-color: #f8f9ff;
  border: 1px solid;
  overflow-y: auto; /* add scrolling */
}
<div class="grid-container">
  <nav>
    <div class="menu-item">Link</div>
  </nav>
  <div class="header"> Header Content Here </div>
  <main> Main Section. This should be able to scroll while Nav and Header are sticking to the top<br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here
    <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum
    some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/>    Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some
    text here <br/> Lorel ipsum some text here <br/> Lorel ipsum some text here <br/>
  </main>
</div>


0

首先,在容器中添加display: grid并从nav元素中删除display: none。然后在main元素中添加overflow: auto 以启用滚动功能。

.grid-container {
  height: 100vh;
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas: "sidenav header"
                       "sidenav main";
}

nav {
  grid-area: sidenav;
  background-color: aqua;
}

header {
  grid-area: header;
  height: 50px;
  background-color: orange;
}

main {
  grid-area: main;
  overflow: auto;
  background-color: #f8f9ff;
}

body {
  margin: 0;
}
<div class="grid-container">
  <nav>nav section</nav>
  <header>header section</header>
  <main>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and
    Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This
    should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking
    to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll
    while Nav and Header are sticking to the top<br><br>Main Section. This should be able to scroll while Nav and Header are sticking to the top<br><br>
  </main>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接