CSS 具有固定大小滚动的网格:如何解决未显示底部 margin/padding 的问题?
CSS Grid with fixed size scrolling: How to solve issue where bottom margin/padding isn't shown?
编辑:
所以我看到了重复的link,在我看来,这个问题是解决你给定大小(固定宽度或固定高度)的问题。
原始问题的解决方案更多是关于使用 hacky 方法修复容器。我决定采用不同的方法来解决它,同时解决我自己在这个问题中提出的问题。
在CSS中,当使用display: grid
和fixed height
时,打开overflow: auto
,你可能会得到这样的布局,如下所示:
.scroll {
overflow: auto;
background-color: #bbddee;
}
.container {
padding: 10px;
display: grid;
grid-template-columns: 1fr;
grid-row-gap: 10px;
height: 420px;
}
.itemContainer {
border: 1px solid #efefef;
padding: 10px 20px;
background-color: #fff;
}
.itemTitle {
text-align:center;
margin: 0 auto;
}
.itemLink {
text-align: center;
margin: 0 auto;
}
<div id="retailerContent" class="tab-pane fade in active show">
<div class="scroll">
<div id="retailerOffersContainer" class="container">
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
</div>
</div>
</div>
发生这种情况时,会出现滚动列表最底部没有 paddings/margins 的问题。应该如何修改 HTML 和 CSS,以便底部 padding/margin 显示在可滚动的 CSS 网格布局中?
这里有一个解决方案(可能是众多解决方案中的一个),展示了如何使用 display: grid
:
解决固定高度 div
底部 padding/margin 缺失的问题
.container {
padding: 10px;
display: grid;
grid-row-gap: 10px;
grid-template-columns: 1fr;
width: 100%;
}
.itemContainer {
border: 1px solid #efefef;
background-color: #fff;
max-height: 100%;
padding: 10px 20px;
height: 100%;
}
.itemTitle {
text-align: center;
margin: 0 auto;
line-height: 20px;
}
.itemLink {
text-align: center;
margin: 0 auto;
line-height: 20px;
}
<div id="retailerContent" class="tab-pane fade in active show">
<div style="height: 420px;background-color: #bbddee;">
<div id="retailerOffersContainer" class="container">
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
</div>
</div>
</div>
这里的主要区别是:
- 网格容器不应指定高度 属性。允许网格自动符合总内容的高度和网格间隙。
- 相反,使网格容器内的元素具有最大高度,由所述元素的内部内容定义。
这将拉伸网格容器,网格元素内容不会溢出网格容器之外。
这是我在使用 CSS 网格时的修复:
/* Fix scrollbar removing padding-bottom */
@supports (-moz-appearance:none) {
.container::after {
content: "";
height: 1px;
margin: calc(var(--padding) - var(--gap));
}
}
编辑:
所以我看到了重复的link,在我看来,这个问题是解决你给定大小(固定宽度或固定高度)的问题。
原始问题的解决方案更多是关于使用 hacky 方法修复容器。我决定采用不同的方法来解决它,同时解决我自己在这个问题中提出的问题。
在CSS中,当使用display: grid
和fixed height
时,打开overflow: auto
,你可能会得到这样的布局,如下所示:
.scroll {
overflow: auto;
background-color: #bbddee;
}
.container {
padding: 10px;
display: grid;
grid-template-columns: 1fr;
grid-row-gap: 10px;
height: 420px;
}
.itemContainer {
border: 1px solid #efefef;
padding: 10px 20px;
background-color: #fff;
}
.itemTitle {
text-align:center;
margin: 0 auto;
}
.itemLink {
text-align: center;
margin: 0 auto;
}
<div id="retailerContent" class="tab-pane fade in active show">
<div class="scroll">
<div id="retailerOffersContainer" class="container">
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
</div>
</div>
</div>
发生这种情况时,会出现滚动列表最底部没有 paddings/margins 的问题。应该如何修改 HTML 和 CSS,以便底部 padding/margin 显示在可滚动的 CSS 网格布局中?
这里有一个解决方案(可能是众多解决方案中的一个),展示了如何使用 display: grid
:
div
底部 padding/margin 缺失的问题
.container {
padding: 10px;
display: grid;
grid-row-gap: 10px;
grid-template-columns: 1fr;
width: 100%;
}
.itemContainer {
border: 1px solid #efefef;
background-color: #fff;
max-height: 100%;
padding: 10px 20px;
height: 100%;
}
.itemTitle {
text-align: center;
margin: 0 auto;
line-height: 20px;
}
.itemLink {
text-align: center;
margin: 0 auto;
line-height: 20px;
}
<div id="retailerContent" class="tab-pane fade in active show">
<div style="height: 420px;background-color: #bbddee;">
<div id="retailerOffersContainer" class="container">
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
<div class="itemContainer">
<div class="itemTitle">15% Off Room Essentials Cubes & Bins</div>
<div class="itemLink"><a>Get Offer ></a></div>
</div>
</div>
</div>
</div>
这里的主要区别是:
- 网格容器不应指定高度 属性。允许网格自动符合总内容的高度和网格间隙。
- 相反,使网格容器内的元素具有最大高度,由所述元素的内部内容定义。
这将拉伸网格容器,网格元素内容不会溢出网格容器之外。
这是我在使用 CSS 网格时的修复:
/* Fix scrollbar removing padding-bottom */
@supports (-moz-appearance:none) {
.container::after {
content: "";
height: 1px;
margin: calc(var(--padding) - var(--gap));
}
}