-
Notifications
You must be signed in to change notification settings - Fork 682
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-flexbox] Make it easier to define margins that only apply between flex-items #592
Comments
We discussed on the mailing list (pre-github) having a -gap property that works like https://lists.w3.org/Archives/Public/www-style/2016Feb/0333.html |
@rachelandrew Yes, that would probably cover most use-cases. You'd want to be able to set both a column gap and a row gap, and then spacing between flex items should never shrink below that. If any items needed additional or different margin, the regular margin properties would still apply. |
I would also really like to see this. @AmeliaBR I came up with an alternative to the negative margin hack, which instead uses padding inside the flex items: https://oliverjash.me/2016/12/28/gutters-for-flexible-components-with-sliced-padding |
Some sort of gutter/gap property would likely result in code that is much easier to read and maintain. |
I agree I think the lack of such a tool is part of why people struggle with Flexbox. It's common for an author to want to specify a minimum or fixed amount of space between items, and it's confusing to not be sure how we are “supposed to” accomplish such a thing. Margins and padding exist, of course, but they are old dependancies stuck in the midst of a new and better system. It feels like there is supposed to be a new better way to create and control a gap between items. |
I’m definitely in favor of adding For reference, an example of this question being asked in the wild (May 2014): http://stackoverflow.com/questions/23433436/spacing-between-flexbox-items |
This would be a tremendously useful addition and a perfect solution to the problem! In my mind there are currently 2 approaches to multiline flexbox grids:
|
As flex rightly replaces table-* rules for layout, I would appreciate a non-hacky (i.e., not requiring additional, non-semantic markup or relying on inconsistent browser behaviors), meaningfully readable way to provide gutters or gaps between flex items. |
I support the proposal for a flex-specific margin property that overrides the regular margin in the flex-direction and has a space-around vs space-between option. This would be incredibly useful. Is there a use case for it being available on the cross axis as well as the main axis? It feels like that would be useful but I can't think of a concrete example at this moment. |
Definitely saw some use-cases for this, if it's similar to |
I for one would love to see a flex-gap property. It should behave in the same way as the grid-gap property. |
Rather than a
example {
display: flex;
item-gap: 1rem;
}
example {
display: grid;
item-gap: 1rem;
}
example {
columns: 4;
item-gap: 1rem;
} |
I agree, something like flex-grid would help cut down on extra containers or hacks we may have to do. It might be more useful if we can specify a min/max gap to have more control over the layout of the items. |
I was thinking about this recently, before I saw this thread. My thought was that there should be something like 'margin-between' that changed the computed values of margin-*. So, margin-between-x would change horizontal margins (but not the left margin of the leftmost items or the right margin of the rightmost items). margin-between-y would work similarly for vertical. Margin-between-inline and margin-between-block would be even better. This is needed for things other than flex, such as a series of inline-block items on one or more lines. It is needed in horizontal and vertical. And it would benefit from normal margin collapsing, when some items on the line didn't have margin-between, but had some other margin. I've needed this since before I ever started using flexbox. |
This just came up again when talking about margin collapsing in #1324 (and that margin collapsing and gaps between flex items are two different things). @erikjung's suggested generally applying Sebastian |
@SebastianZ wouldn't it be more explicit to state what 'thing' the gap is for? It seems more obvious to me. i.e. |
@morganfeeney Good point! Though I think it's obvious enough to what Sebastian |
I've split out my idea about a Sebastian |
The CSSWG resolved to switch Grid to using generic I'm going to close this issue as fixed; please file a new issue if there's something else to be considered here. :) Thanks everyone~ |
In flexbox it is often desired to apply a fixed or minimum margin between items, without adding a margin in between items and the edges of the container. As far as I can tell, there is no non-hacky way to do this.
The
justify-content
property allows you to specify that extra space should only go in between, and not at the edges. But if there isn't any extra space to distribute, that items end up squished next to each other with no margin.If you apply a fixed margin on the individual flex items, you can ensure they are spaced out, but then that margin effectively creates extra padding space inside the flex container. This can throw off your overall page alignment.
For a single-line flex container, you could use
:first-child
/:last-child
selectors to remove the margins on the outside edge. But that's not possible in a multi-line flex layout, when you don't know which items will be at the edges.The "best" solution I've seen so far is the one used by @heydonworks in his fukol code snippet: apply a negative margin on the flex container to cancel out the margin on the flex items inside it. And then maybe wrap the flex container in an extra
div
if you need to apply its own positive margins under certain conditions.This works for most cases, but it is undeniably a hack. Can we get a neater solution? Perhaps a flex-specific margin property that overrides the regular margin in the flex-direction and has a space-around vs space-between option? Perhaps this could be part of the spec update that also figures out what to do with percentage margins in flexbox?
The text was updated successfully, but these errors were encountered: