How do you collapse unused row in a CSS grid?

Instead of this: grid-template-rows: min-content auto Try this: grid-template-rows: min-content 1fr With 1fr you’re telling the second row to consume any and all free space available in the column. Hence, when there is no content in the .first element, the .second element takes the space. The problem with auto is that its length is relative … Read more

CSS Grid auto fit with max-content

I had a similar question when playing around with grid: grid-template-columns: repeat(auto-fit, minmax(max-content, 1fr)) If we take a look at the documentation we can see that minmax command is valid: https://developer.mozilla.org/en-US/docs/Web/CSS/minmax But in a repeat documentation on csswg, it states one simple rule that disallows all of this from happening; https://drafts.csswg.org/css-grid/#funcdef-repeat The generic form of … Read more

How to make CSS Grid items take up remaining space?

Adding grid-template-rows: 1fr min-content; to your .grid will get you exactly what you’re after :). .grid { display: grid; grid-template-columns: 1fr 3fr; grid-template-rows: 1fr min-content; grid-template-areas: “one two” “one three” } .one { background: red; grid-area: one; padding: 50px 0; } .two { background: green; grid-area: two; } .three { background: blue; grid-area: three; } … Read more

How can I target a specific column or row in a CSS grid layout?

To style an arbitrary row, you could use a wrapper element with its display set to contents. See the code snippet below: .grid-container { display: grid; grid-template-columns: repeat(5, 1fr); grid-gap: 2px; } .grid-item { border: 1px solid black; padding: 5px; } .grid-row-wrapper { display: contents; } .grid-row-wrapper > .grid-item { background: skyblue; } <div class=”grid-container”> … Read more

Change the column order in a css grid

Grid Layout provides multiple methods for re-arranging grid items. I’ve listed four below. The grid-template-areas property Line-based placement The order property The dense function of the grid-auto-flow property. (Possibly the simplest, easiest and most robust solution for this type of layout, as it works for any number of grid items.) Here’s the original layout: grid-container … Read more

How can I create a row-based masonry layout using CSS?

2021 Update CSS Grid Layout Level 3 includes a masonry feature. Code will look like this: grid-template-rows: masonry grid-template-columns: masonry As of March 2021, it’s only available in Firefox (after activating the flag). https://drafts.csswg.org/css-grid-3/#masonry-layout https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Masonry_Layout end update; original answer below Flexbox A dynamic masonry layout is not possible with flexbox, at least not in a … Read more