5

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.

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>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • Is this not due to the hard-coding of widths? If you remove `width:40px` from the `li` options it's flush on both sides and the gaps are equal. If not, please clarify what the desired appearance is. – Charlie Jan 10 '19 at 09:19
  • Check out this [article](https://medium.com/cloudaper/how-to-create-a-flexible-square-grid-with-css-grid-layout-ea48baf038f3) and its associated [pen](https://codepen.io/krystof-k/pen/vdNQGB), I'm pretty sure that's what you need – Jake Jan 10 '19 at 09:39
  • @Charlie how do you guarantee square shapes if you remove the width? – Crashalot Jan 10 '19 at 09:52
  • @Jake thanks will check them out! – Crashalot Jan 10 '19 at 09:52
  • @Crashalot oh yeah, good point. [Maybe this could help with that](https://stackoverflow.com/questions/1495407/maintain-the-aspect-ratio-of-a-div-with-css) – Charlie Jan 10 '19 at 09:57

2 Answers2

3

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

enter image description here

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>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
0

This one is the answer to your first problem no need to give width to 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>
Arshiya Khanam
  • 613
  • 6
  • 12