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

[css-align][css-grid] Suggestion: Flexible Grid Gaps #6275

Closed
thatcort opened this issue May 7, 2021 · 10 comments
Closed

[css-align][css-grid] Suggestion: Flexible Grid Gaps #6275

thatcort opened this issue May 7, 2021 · 10 comments
Labels
Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. Commenter Satisfied Commenter has indicated satisfaction with the resolution / edits. css-align-3 Current Work css-grid-1

Comments

@thatcort
Copy link

thatcort commented May 7, 2021

It would be useful to be able to allocate extra space in a grid to the space between grid items. This would allow the grid cells to be sized according to their row/column max-content while still allocating space around them. To do this I suggest the option of specifying the row/column gap with a flexible length fr value.

This is different than justifying items within flexible grid cells since it retains the uniform column/row edges regardless of varying sizes of contained grid items.

Motivating use case:
CSS grids have been a huge help in laying out complex tabular data and components. Imagine a table that contains both right-aligned numerical columns and left-aligned text columns. As the table grows in width I would like the columns to remain horizontally compact, but equidistant from each other. Allocating space to the grid tracks results in problems: If a numerical column is set to justify-end, it will bump up against the next left-aligned column. If columns are set to justify-center within the column there will be a ragged edge as the items are of varying widths. Instead we want a column that doesn't grow and column gaps that do.

@thatcort thatcort changed the title [css-align][css-grid] Suggestion: Allow Flexible Lengths for Grid Gaps [css-align][css-grid] Suggestion: Flexible Lengths for Grid Gaps May 7, 2021
@thatcort thatcort changed the title [css-align][css-grid] Suggestion: Flexible Lengths for Grid Gaps [css-align][css-grid] Suggestion: Flexible Grid Gap Lengths May 7, 2021
@thatcort thatcort changed the title [css-align][css-grid] Suggestion: Flexible Grid Gap Lengths [css-align][css-grid] Suggestion: Flexible Grid Gaps May 7, 2021
@SelenIT
Copy link
Collaborator

SelenIT commented May 8, 2021

Is the result of distributing extra space between columns using justify-content: space-between significantly different from this proposal?

@Loirooriol
Copy link
Contributor

Loirooriol commented May 9, 2021

Yeah, you don't want to distribute grid items within the grid areas, it sounds like you want to distribute grid tracks within the grid container. https://drafts.csswg.org/css-align-3/#distribution-grid

@tabatkins
Copy link
Member

Yeah, the content-distribution properties (align-content, justify-content) do precisely this. They don't allow arbitrary control over gap sizing (so you can't, for example, have one gap be twice as large as the rest), but they should cover all the common cases.

Since you +1'd Oriol's comment, we'll go ahead and assume that closing this is acceptable. ^_^

@fantasai fantasai added Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. Commenter Satisfied Commenter has indicated satisfaction with the resolution / edits. labels Jun 16, 2021
@thatcort
Copy link
Author

I +1'd Oriol's comment because I thought he was agreeing with me. Please reopen and remove the tags! I'll create an image to illustrate what I mean. The current functionality does not cover this use case that I feel is rather common.

@thatcort
Copy link
Author

Here is a quick diagram that attempts to illustrate the problem:

image

The black table is the grid and the blue rounded rectangles are components placed within it. In the top image the columns end up poorly spaced with right- and left-aligned columns awkwardly bumping up against each other or leaving huge gaps between them. In the bottom image the orange shaded areas are grid gaps sized based on the available free space, so that the columns retain their start/end justification and are also evenly spaced.

@Loirooriol
Copy link
Contributor

@thatcort We understand your usecase. We don't understand why justify-content: space-between doesn't cover your usecase.

@tabatkins tabatkins removed the Commenter Satisfied Commenter has indicated satisfaction with the resolution / edits. label Jun 18, 2021
@SelenIT
Copy link
Collaborator

SelenIT commented Jun 21, 2021

@thatcort here is a demo of distributing free space between columns using justify-content (based on your diagram): https://codepen.io/SelenIT/pen/oNZOKPz. Doesn't it solve your case?

@thatcort
Copy link
Author

thatcort commented Jun 29, 2021 via email

@Loirooriol Loirooriol added the Commenter Satisfied Commenter has indicated satisfaction with the resolution / edits. label Jun 29, 2021
@kizu
Copy link
Member

kizu commented Apr 21, 2023

Just wanted to mention a case that justify-content does not cover: we could want to limit the amount of space distributed. Imagine we would have something like gap: clamp(1rem, 1fr, 5rem) — this could work similarly to having a gap + having a justify-content, where the gaps would try to use the remaining space, but could increase to max of 5rem.

Link to a mastodon thread by @matuzo where I saw a case that could benefitted from the fr for gaps: https://front-end.social/@matuzo/110236145041434483

The only workaround for this that I found involves adding extra elements that would be used instead of the gaps — https://codepen.io/kizu/pen/gOBLyRR?editors=1100

Video showing how this works in action:

Screen.Recording.2023-04-21.at.13.02.49.mov

@thatcort
Copy link
Author

Chrome finally implemented subgrid, so I was finally able to test out using justify-content: space-between in our production app and it helps enormously, but still isn't ideal. The problem with space-between is that every column gap is identical. In our tables it would be far preferable to have some columns but up against each other (e.g. checkbox column with a name column) and others far apart. There are workarounds, but being able to specify fr units on column gaps would be really, really nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closed as Question Answered Used when the issue is more of a question than a problem, and it's been answered. Commenter Satisfied Commenter has indicated satisfaction with the resolution / edits. css-align-3 Current Work css-grid-1
Projects
None yet
Development

No branches or pull requests

6 participants