0

I am trying to create the following layout with css grid:

grid-example

What I have tried so far looks as follows:

.grid {
    padding: 16px;
    display: grid;
    grid-template-rows: repeat(auto-fill, 200px);
}

img {
    height: 100%;
    width: auto;
}

but I am having trouble making the column count dynamic. Is it not possible at all?

2
  • You can define a fixed height for the rows ` grid-auto-rows: 150px;` and this for the columns grid-template-columns:repeat(auto-fill,150px); the max width for the columns will be 150px, you might see some overlaps, if so you need max-width:100% Commented Jun 26, 2018 at 14:11
  • 1
    Please provide enough code to reproduce the problem. Commented Jun 26, 2018 at 14:38

1 Answer 1

2

CSS grid isn't a good fit for the layout you want. Luckily you can use flexbox to achieve something similar:

.grid {
    display: flex;
    flex-wrap: wrap;
}

img {
    flex: 1;
    height: 200px; /* or whatever fixed height you need */
    max-width: 100%;
    object-fit: cover;
}

Here's a Codepen: https://codepen.io/wiiiiilllllll/pen/ZRMxpo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.