Layout on mobile browsers breaks when grid-template-columns is not explicitly set.
The first two images show the layout on mobile. When there is no nested text the layout is fine and doesnt overflow, however as soon as I add text inside the image card, the content overflows.
NOTE: this is only an issue on mobile browsers. it is not an issue on desktop versions of safari, chrome, and firefox which makes it even harder to test for me because it looks perfectly fine there.
Here is the code that doesn't work.
My understanding was that I could just set grid with no template columns and use media queries to specify columns as screen size changes (I learned this from kevin). But for some reason it doesnt work in this case?- the fix I found? setting grid-cols-1. My question is, why is it completely broken unless I set this value.
My understanding was that I could just set grid with no template columns and use media queries to specify columns as screen size changes (I learned this from kevin). But for some reason it doesnt work in this case?- the fix I found? setting grid-cols-1. My question is, why is it completely broken unless I set this value.



4 Replies
You shouldn't need to add grid-cols-1 or remove the grid.
Where do you expect the text to go?
I have just a quick test based on your code and didn't have the same issue.
I suspect that something else is going on that is affecting this.
Can you create a minimal Codepen (or similar) to demonstrate the issue?
Note - seeing as you are using the figure element, it would make sense to use the figcaption element for the accompanying text.
The text is suppose to just be under the image. if you look at this photo this is what it looked like on my pc. It was only broken on all the browsers on my phone.

And thats exactly my point. I shouldnt need to remove the grid or add grid cols. The layout was only broken on my phones browsers. There a layout issue at all when testing it responsively on my pc
I’ll try throwing it up on codepen and see if it breaks
It may be due to defining the aspect ratio 🤔
If you don't define a grid-column width (as is the case for the smaller viewport) the width will be "auto" and therefore defined by the content within it. As you have given both the figure and the image an aspect ratio, these are "forcing" the width and the height which is possibly causing the issue that you are having (I can't reproduce it with your code which is why I thought that it may be something else that you haven't included).
By defining
grid-template-columns: 1fr
you are telling the column to have 100% width of the parent rather than the width of the children.