我试图让.menu-scroll
类的<ul>
列表的div中的项目在超过高度限制时显示滚动条,在我的情况下,它只会不断推动降到.menu-footer
div并在页面中造成很大的问题。
body {
margin: 0;
padding: 0;
}
.grid {
display: grid;
grid-template-columns: 300px 100px auto 100px;
grid-template-rows: 50% 50%;
background-color: gray;
height: 100vh;
}
.grid>#menu {
display: grid;
background-color: blueviolet;
text-align: center;
font-size: 30px;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 2 end;
grid-template-rows: auto 75px;
}
.grid>#posts-wrapper {
display: grid;
background-color: gray;
text-align: center;
font-size: 30px;
grid-column-start: 3;
grid-column-end: 4;
grid-template-rows: 50px auto;
grid-template-columns: auto;
}
#posts {
background-color: white;
grid-row-start: 2;
grid-row-end: 3;
}
.menu-footer {
background-color: green;
}
.menu-scroll {
display: grid;
grid-template-rows: 100px auto;
}
.menu-scroll>.cover {
background-color: yellow;
}
.menu-scroll>.menu-list {
position: sticky;
overflow: scroll;
}
.list-item {
background-color: tomato;
padding: 10px 0px;
margin-top: 5px;
}
<div class="grid" id="container">
<div class="grid-item" id="menu">
<div class="menu-scroll">
<div class="cover">cover</div>
<div class="menu-list">
<ul style="list-style: none;">
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
</ul>
</div>
</div>
<div class="menu-footer">sticky footer</div>
</div>
<div class="grid" id="posts-wrapper">
<div id="posts">
posts
</div>
</div>
</div>
答案 0 :(得分:2)
之所以这样,是因为在您的:
.menu-scroll {
display: grid;
grid-template-rows: 100px auto;
}
您设置了auto
大小调整网格行中除第一个元素以外的所有元素。因此,无论您添加到第二个元素ul
中的<div class="menu-list">
中有多少项,它都会一直增长。
您需要将其设置为在第二个元素上具有明确的高度。
赞:
.menu-scroll {
display: grid;
grid-template-rows: 100px 300px auto;
}
body {
margin: 0;
padding: 0;
}
.grid {
display: grid;
grid-template-columns: 300px 100px auto 100px;
grid-template-rows: 50% 50%;
background-color: gray;
height: 100vh;
}
.grid>#menu {
display: grid;
background-color: blueviolet;
text-align: center;
font-size: 30px;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 2 end;
grid-template-rows: auto 75px;
}
.grid>#posts-wrapper {
display: grid;
background-color: gray;
text-align: center;
font-size: 30px;
grid-column-start: 3;
grid-column-end: 4;
grid-template-rows: 50px auto;
grid-template-columns: auto;
}
#posts {
background-color: white;
grid-row-start: 2;
grid-row-end: 3;
}
.menu-footer {
background-color: green;
}
.menu-scroll {
display: grid;
grid-template-rows: 100px 300px auto;
}
.menu-scroll>.cover {
background-color: yellow;
}
.menu-scroll>.menu-list {
position: sticky;
overflow: scroll;
}
.list-item {
background-color: tomato;
padding: 10px 0px;
margin-top: 5px;
}
<div class="grid" id="container">
<div class="grid-item" id="menu">
<div class="menu-scroll">
<div class="cover">cover</div>
<div class="menu-list">
<ul style="list-style: none;">
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
<li class="list-item">menu item</li>
</ul>
</div>
</div>
<div class="menu-footer">sticky footer</div>
</div>
<div class="grid" id="posts-wrapper">
<div id="posts">
posts
</div>
</div>
</div>