【问题标题】:CSS grids: align square cells with container edges with consistent grid gap?CSS网格:将方形单元格与具有一致网格间隙的容器边缘对齐?
【发布时间】:2019-01-10 17:28:10
【问题描述】:

目标是将方形单元格与其容器的前缘和后缘对齐,同时在每行上的单元格之间以及每行之间实现一致的间隙。

这个Codepen很接近,但是有两个问题:(1)垂直缝隙和水平缝隙不同; (2) 方块与前缘齐平,但不与后缘齐平。

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 选项中删除width:40px,它的两边都是齐平的,并且间隙相等。如果不是,请说明所需的外观是什么。
  • 查看此article 及其关联的pen,我很确定这就是您所需要的
  • @Charlie 如果去掉宽度,你如何保证方形?
  • @Jake 谢谢你会去看看!
  • @Crashalot 哦,是的,好点。 Maybe this could help with that

标签: html css css-grid


【解决方案1】:

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

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

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>

【讨论】:

    【解决方案2】:

    这是你第一个问题的答案,不需要给 li 宽度。

    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: 5px;
      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>

    【讨论】:

    • 您的解决方案的问题是元素不再是精确的正方形。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2018-07-13
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多