本文介绍了CSS网格:将正方形单元与具有一致网格间隙的容器边缘对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是使方形单元格与其容器的前缘和后缘对齐,同时在每一行以及每一行之间的单元格之间保持一致的间隙.

The goal is to align square cells with their container's leading and trailing edges while achieving a consistent gap between cells on each row, and between each row.

此Codepen很接近,但是存在两个问题:(1)垂直间隙与水平间隙不同; (2)正方形与前缘齐平,但与后缘不齐.

This Codepen is close, but there are two problems: (1) vertical gaps are different from horizontal gaps; and (2) the squares are flush with the leading edge, but not the trailing edge.

https://codepen.io/anon/pen/wREmjo

ul {
  display: grid;
  width: 260px;
  grid-template-columns: repeat(auto-fit, minmax(40px, 1fr));
  grid-auto-rows: 1fr;
  grid-gap: 10px;
  list-style-type: none;
  border: 2px solid black;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  background: gray;
}

li {
  width: 40px;
  height: 40px;
}
<ul class="palette">
  <li style="background-color: rgb(0, 0, 255);"></li>
  <li style="background-color: rgb(51, 51, 51);"></li>
  <li style="background-color: rgb(203, 58, 135);"></li>
  <li style="background-color: rgb(237, 106, 90);"></li>
  <li style="background-color: rgb(155, 193, 188);"></li>
  <li style="background-color: rgb(255, 247, 174);"></li>
  <li style="background-color: rgb(184, 51, 106);"></li>
  <li style="background-color: rgb(61, 44, 46);"></li>
  <li style="background-color: rgb(105, 173, 212);"></li>
  <li style="background-color: rgb(245, 223, 22);"></li>
  <li style="background-color: rgb(252, 186, 86);"></li>
  <li style="background-color: rgb(0, 185, 252);"></li>
  <li style="background-color: rgb(181, 141, 182);"></li>
  <li style="background-color: rgb(58, 50, 96);"></li>
</ul>

推荐答案

问题是网格单元很好,但是(li)中的内容与它们不匹配.

The issue is that the grid cells are fine but the content inside (li) is not matching them.

您可以考虑使用百分比值,而不是在li上使用固定的宽度/高度,它们在某些情况下会稍大一些,但仍将是正方形元素:

Instead of using fixed width/height on the li you can consider percentage value and they will be a bit bigger in some cases but will remain square elements:

ul {
  display: grid;
  width: 260px;
  grid-template-columns: repeat(auto-fit, minmax(40px, 1fr));
  grid-auto-rows: 1fr;
  grid-gap: 10px;
  list-style-type: none;
  border: 2px solid black;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  background: gray;
  animation:change 5s linear alternate infinite;
}

li {
  width: 100%;
  padding-top:100%;
}

@keyframes change {
  to {width:120px}
}
<ul class="palette">
  <li style="background-color: rgb(0, 0, 255);"></li>
  <li style="background-color: rgb(51, 51, 51);"></li>
  <li style="background-color: rgb(203, 58, 135);"></li>
  <li style="background-color: rgb(237, 106, 90);"></li>
  <li style="background-color: rgb(155, 193, 188);"></li>
  <li style="background-color: rgb(255, 247, 174);"></li>
  <li style="background-color: rgb(184, 51, 106);"></li>
  <li style="background-color: rgb(61, 44, 46);"></li>
  <li style="background-color: rgb(105, 173, 212);"></li>
  <li style="background-color: rgb(245, 223, 22);"></li>
  <li style="background-color: rgb(252, 186, 86);"></li>
  <li style="background-color: rgb(0, 185, 252);"></li>
  <li style="background-color: rgb(181, 141, 182);"></li>
  <li style="background-color: rgb(58, 50, 96);"></li>
</ul>

这篇关于CSS网格:将正方形单元与具有一致网格间隙的容器边缘对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:03