具有固定大小滚动的CSS网格:如何解决底部边距/填充未显示的问题?

时间:2018-05-01 14:54:45

标签: html css margin padding css-grid

修改

所以我看到了指向的重复链接,在我看来,这个问题是为了解决你给出固定大小(固定宽度或固定高度)的问题。

原始问题的解决方案更多的是使用hacky方法修复容器。我决定采用不同的方法来修复它,同时修复我在这个问题中提出的问题。

在CSS中,当使用display: gridfixed 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 &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

当发生这种情况时,存在滚动列表最底部没有填充/边距的问题。如何修改HTML和CSS,因此底部填充/边距将显示在可滚动的CSS网格布局中?

2 个答案:

答案 0 :(得分:0)

这是我使用CSS网格时的解决方案:

/* Fix scrollbar removing padding-bottom */
@supports (-moz-appearance:none) {
  .container::after {
    content: "";
    height: 1px;
    margin: calc(var(--padding) - var(--gap));
  }
}

答案 1 :(得分:-1)

这是一个解决方案(可能是其中之一),展示了如何使用div解决固定高度display: grid中缺少底部填充/边距的问题:

&#13;
&#13;
.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;
 }
&#13;
<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 &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      <div class="itemContainer">
        <div class="itemTitle">15% Off Room Essentials Cubes &amp; Bins</div>
        <div class="itemLink"><a>Get Offer&nbsp;&gt;</a></div>
      </div>
      
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

这里的主要区别是:

  • 网格容器不应指定height属性。允许网格自动符合总内容和网格间隙的高度。
  • 相反,使网格容器内的元素具有最大高度,由所述元素的内部内容定义。

这将拉伸网格容器,而网格元素内容不会溢出网格容器。