I'm trying to build a responsive CSS grid with three columns that wrap when necessary. I want the minimum column width to be 200px, otherwise use the percentage 30%.
I believe I can use the minmax
CSS calculation to do this, but some strange overflow error occurs when I use percentages rather than pixels.
.wrapper-pix {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(200px, 360px));
}
.wrapper-percent {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(200px, 30%));
}
.box-pix{
background: #4299E1;
height: 200px;
}
.box-percent{
background: #F56565;
height: 200px;
}
<div class="wrapper-pix">
<div class="box-pix"></div>
<div class="box-pix"></div>
<div class="box-pix"></div>
</div>
<div class="wrapper-percent">
<div class="box-percent"></div>
<div class="box-percent"></div>
<div class="box-percent"></div>
</div>
Please also see this CodePen: https://codepen.io/willjonty/pen/ExVpVza.
The blue boxes prefer to wrap and stay at their maximum percentage until the window becomes particularly small. The red boxes, however, will resize their max value with the percentage until they reach the 200px breakpoint. So far, this is all good.
I would expect the red boxes to wrap when the viewport reaches 640px, which is 200 * 3 (for the three columns), plus 2 * 20px gaps. Notice however that when the viewport becomes smaller, the red boxes do not wrap at all, and instead overflow into a hidden part of the window. For some reason, the third box will only wrap when the viewport is 399 or less.
Does anyone know why CSS grid behaves like this? And how I might implement a column max-width of 30% that will wrap without any overflow?
I would use fr
units, but those will hijack auto-fit to increase the number of columns in a row depending on the data. I want 3 equally-sized columns.
Thank you in advance.