Restricting responsive grid to 2 columns max? - css

I have a two column layout in which one column displays a number of images on a grid. The size and number of the images vary. I want the images to either show one a line, or two per line, depending on the size of the image and the screen. I have the below grid set up:
#media (max-width: 2000px) {
.productimg {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(195px, 2fr));
grid-gap: 5px;
grid-auto-flow: row dense;
}
The images are given a class .u-max-full-width to keep them contained within the grid:
.u-max-full-width {
max-width: 100%;
box-sizing: border-box; }
For the most part this works. See below for the ideal layout:
However, at certain screen widths the images break into three columns. This happens with particularly thin images as soon as it is possible to put three 195px width images on the same line. I thought it might be that the images reach their source width and then rather than remain there shrink back to 195px but they are moving down to 195px before reaching their max size.
For example, images with a width of 336, 353, and 390 respectively will switch to a three col. when the container div is only 595px wide, more than small enough for the 336px and 353px images to fill it without forcing a third in.
If I increase the min width this solves the issue, however, the images will not properly fall into two rows at smaller resolutions. I tried using a #media declaration for the range where the container div can hold three 195px images, and declaring an absolute max size.
#media (min-width: 1542px) and (max-width: 2000px){
.productimg {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(195px, 350px));
grid-gap: 5px;
grid-auto-flow: row dense;
}
This does seem to prevent three columns from developing, but now it jumps from two to one columns between 1541 and 1542px in screen width because the max width is being prioritized, which is not what I want at all.
What I want to do is keep the columns to a maximum of two but to fill the row completely as long as the images are not smaller than 195px in width, but I can't seem to find any way to do that simply. I have looked at all of the various formatting declarations for grid, but am struggling to conceptualize their use. I wonder if grid is even the appropriate tool to use in this situation, but I haven't found any better solution.
Any assistance in this issue would be appreciated.

Use max() combined with viewport unit to make the column width always big enough to always have a fixed number of columns on big screen (in your case 2).
grid-template-columns: repeat(auto-fit, minmax(max(195px,Xvw), 2fr));
Adjust the X until you get the result you want.
Example:
.box {
display:grid;
grid-template-columns: repeat(auto-fit, minmax(max(195px, 45vw), 2fr));
margin: 10px;
grid-gap: 40px;
}
.box>* {
height: 100px;
background: red
}
<div class="box">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
Related question: CSS grid maximum number of columns without media queries

You can use like that also...
grid-column: 1 / 2;

Related

CSS Grid full width columns on the mobile

I use CSS Grid to show 2 columns (50% grey/50% black). You see how it looks
I want to make them full width on the mobile, so that gray would be in the first line, and black one in the second line.
My CSS:
.container {
display:grid;
height:100vh;
grid-template-columns: repeat(auto-fill, minmax(50%, 1fr))
}
What I am doing wrong?
P.S. I know all things can be done with #media queries, but I need more modern way.

Minimal pixels for gridbox

enter image description hereI have a gridbox with 2 gridboxes, 3fr and 1,5fr. Now I would like to make a CSS that has something like "if gridbox 2 (1,5fr) is smaller than 100px, go to the next row. I don't know if this is possible.
I think flexbox might be easier for what you're looking to do, but if you want to use a grid you will have to use #media.
.container{
display: grid;
grid-template-columns: minmax(200px,3fr) minmax(100px, 1.5fr);
}
#media screen and (max-width:330px) {
.container{
grid-template-columns: 1fr;
}
}
You may have to adjust the numbers depending on your particular code, but this will make the elements inside the "container" class take up 100% when below the threshold.

How to make a CSS GRID, with uneven column widths, responsive?

I have created a CSS GRID and each row has 2 columns. The first column takes up 25% of the width and the second column takes up 75%. I have achieved this using
.grid-container{
display:inline-grid;
grid-template-columns: 1fr 3fr;
}
Now, I want to make it responsive such that when the screen size reduces beyond a certain point, the column should be on top of one another and each column should occupy 100% of the width container.
Every solution I've come across online does make it responsive, BUT it also changes my initial column width from 25%:75% to 50%:50% and I want to avoid that. Any suggestions or tips would be greatly appreciated. Thank you all in advance!
You can use media queries for that:
.grid-container {
display:inline-grid;
grid-template-columns: 1fr 3fr;
}
#media (max-width: 800px) {
.grid-container {
grid-template-columns: 1fr;
}
}

Why is there a small pixel gap between tiles in CSS grid when gap is set to 0 (responsive map)?

I am trying to create a game level select map where I need different sprites from a spritesheet placed on a very specific tile in a grid. I use CSS grid for this and place the tiles on the grid inline with grid-row-start and grid-column-start. Everything works as it should, except for one thing: the tiles have a small gap between them, but gapis set to 0 in the CSS. That small gap is enough to look really jarring (screenshot of map with the gap).
Because this is a mobile game, it is important that the grid is responsive and works on all device sizes. I am therefore working in percentages, and vw in the CSS. I am thinking that the gaps might be caused by decimal rounding errors?
The parent div:
.tilemap {
display: grid;
height: 150vw; /* 15 rows, 10 columns */
grid-template-rows: repeat(15, 1fr); /* 15 rows */
grid-template-columns: repeat(10, 1fr); /* 10 columns */
gap: 0;
place-content: stretch end;
}
The grid is full width (100vw) and each tile is 10vw = 10 columns. The height of the tilemap is 150vw = 15 rows. Each tile is squared (screnshot of CSS grid in devtools).
Common class for all tiles:
.tilemap div {
max-width: 100%;
background-size: 100%;
background-image: url("../images/responsive-spritesheet.png");
}
And each tile has a different background position. Included for completeness:
.tilemap div.road.vertical {
background-position: 0 100%;
}
The problem is not the spritesheet image, because the gaps still appear when I comment this out and replace it with background-color instead (screenshot).
Any help is greatly appreciated.
I know this question was asked forever ago, but I solved a similar issue of my own by dropping fr units, and using exact lengths for my grid cells.
This might help you, because I see you're using repeat() and thus have equal spacing in your grid, like I do.

CSS Grids auto (rest) size doesn't work for me

I'm looking into CSS Grids and I've come across something weird. I want 3 columns: 1 and 3 are 150px wide, and 2 is the rest. That should be possible, I thought, but this didn't work:
grid-template-columns: 150px auto 150px;
So I tried this, and that does work, but is silly:
grid-template-columns: 150px calc(100% - 300px) 150px;
And then I came across this example: http://gridbyexample.com/examples/code/layout1.html which uses this to make the content column auto size:
grid-template-columns: 200px 40px auto 40px 200px;
That's what I did! And their example works, and mine doesn't!?
My example fiddle: http://jsfiddle.net/rudiedirkx/w5xho67w/3/ (3 columns trigger if the screen >= 700px).
I must be doing something stupid, but I can't find the difference...
Not it:
Because the body is grid
Because it's an iframe
Because the wrapper (body) isn't fixed width (neither 100% nor 900px works)
What you are looking for is the fractional units – set the middle column to be 1fr, meaning that it will take up 1/1 of the remaining space when the two other columns are accounted for. It works (basically) like a flexbox flex unit with a flex basis: 0.
grid-template-columns: 150px 1fr 150px;
By contrast, the auto value will size according to content, so an empty middle column (for example) will not take up any space.
I've started writing a series on how the Grid Layout Module works, there is a part on sizing grid tracks (which may be helpful) in there. :-)

Resources