这有点奇怪,因为我最终使用flexbox来对齐sidenav
和header
中的菜单项,并使用网格来显示/分配{{1} }和nav
组件。
我已经尝试过使仅基于网格或基于flexbox的侧边栏(或标题)具有粘性的方法,但是它们根本不起作用。
这是我所拥有的一个例子:
header
CSS:
<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>
答案 0 :(得分:1)
像您想做的那样将grid和flexbox属性组合在一起是正确的-您需要添加一些内容才能使它们起作用:
header
和sidebar
保持粘性。header
指定grid-template-rows
的高度(请参见下面的代码段)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>
答案 1 :(得分:0)
首先将display: grid
添加到容器中,然后从display: none
元素中删除nav
。然后将overflow: auto
添加到main
元素以启用滚动功能。
.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>