Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically flowing grid items based on available space #70

Open
snookca opened this issue Apr 26, 2017 · 7 comments
Open

Dynamically flowing grid items based on available space #70

snookca opened this issue Apr 26, 2017 · 7 comments

Comments

@snookca
Copy link

snookca commented Apr 26, 2017

I'd like to flow grid items based on available grid columns. repeat() using auto-fill or auto-fit create this behaviour. It will automatically create the number of grid tracks that can fit within the container. Given repeat(auto-fit, 100px) and the container is 450px wide, there will be 4 columns. If the container is 550px wide, there will be 5 columns.

The problem (if you want to call it that) is that I'd like to enable similar behaviour with explicitly defined grid tracks. If I say grid-template-columns: 100px 300px 100px, I'd like to only flow items into that third column if there's enough room within the container. Instead, from what I can tell, once a track is defined, a grid item will be put into that track regardless of whether there's room or not. (Assuming all grid items are set to span a single column.)

Thoughts?

@rachelandrew
Copy link
Owner

I'm really not clear on the problem you are trying to solve here. Can you demonstrate with some code?

@snookca
Copy link
Author

snookca commented May 2, 2017

Hopefully this explains it better: https://codepen.io/snookca/pen/PmjExO

@rachelandrew
Copy link
Owner

So you want to be able to specify a track listing that could repeat n number of times?

For example if I have available 400 pixels, and want to auto fill a pattern of 100px 200px, that would give me tracks of 100px 200px 100px (as the 100px repeats again from the listing).

If I have available 600px in the container I'd get 100px 200px 100px 200px.

Is that right? If so what is the use case for wanting that ability? What are you trying to build with it?

@snookca
Copy link
Author

snookca commented May 8, 2017

Yes, that's correct.

The intent is to allow for a responsive layout without needing to use media queries (or container queries).

An example use case: When there's enough room, the component has 1 wide and 2 narrow columns. When there isn't enough room, the content in the 3rd column flows either into the 1st or 2nd column. When the container is smaller still, the content from the 2nd column flows into the 1st.

The current home page of CNN.com actually has a good example of this. On a wider screen, there's a 3 col grid of videos. When there isn't enough room, it drops to 2 columns.

In looking at their implementation, they seem to be using an Element Query approach to this problem (as evidenced by the data-eq-state attributes).

screen shot 2017-05-08 at 2 10 02 pm
screen shot 2017-05-08 at 2 09 53 pm

@thundernixon
Copy link

For what it's worth, I also came to ask this question (I think so, at least – please correct me if I'm wrong, @snookca).

If I were using flexbox, I might set flex-wrap: wrap; and set a flex-basis: 200px to each card in a layout. This would allow it to reflow sensibly when a layout gets wider or narrower. An example of this, contrasted to grid, is here: https://codepen.io/thundernixon/full/PmeOJW/

Does grid have a corollary to flex-wrap and flex-basis? Or, is this a case in which flex and grid might somehow be mixed, to achieve the desired effect? Thank you for any insight!

@rachelandrew
Copy link
Owner

@thundernixon no - your example is just the basic auto-fill one https://codepen.io/rachelandrew/pen/ZKoRaK?editors=1100

See: http://gridbyexample.com/video/series-auto-fill-auto-fit/

@rachelandrew
Copy link
Owner

@snookca I think your example requires the use of media queries. I'd probably just apply the span 2 at a particular breakpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants