Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

CSS Grid - How do you get a item to take up 100% of the free space available to it

I thought the difference in using auto-fit and auto-fill, was that auto-fit collapsed the width of the implicit column, thus allowing for an actual 'real' column could take up the free space available to it?

I have THREE columns - I'm trying to get it, so that when the layout breaks into a TWO column layout, the third item (the first to break to a new line), takes up 100% of the container. Yet, it doesn't do that.... Why?

The CSS has been written with sass

<div class="wrapper">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>
.wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-auto-rows: minmax(50px, 1fr);
  grid-gap: 10px;
   & .child {
    background-color: red;
  }
}

https://codepen.io/Woodenchops/pen/MQJrYy

1 Answer

Steven Parker
Steven Parker
231,046 Points

As to why, it's because it has to share that 1fr with it's implicit neighbor cell.

But to get around it, the first thing that came to mind was a media query to make it span both columns:

@media (max-width: 936px) {
  .child:last-child {
    grid-column: 1 / -1;
  }
}