Repository navigation
Description
TL;DR
display: masonry;
masonry-template: repeat(auto-fill, auto);
masonry-direction: column-reverse;
masonry-span: 2;
masonry-threashold: 2px;The primary issue with building masonry layout on top of grid is related to intrinsic-sizing (of the container itself, and intrinsic tracks).
Grid layout works by placing everything in the 2D grid, that is assigning each child a column(s), and row(s), then sizing the grid with all the children fixed within the grid (they can't jump to another grid position).
Masonry layout however works in reverse - by sizing the rows/columns first, then placing children in the "shortest" row/column. This means that we can't correctly size the rows/columns (as we don't know the content), and also can't size the container itself correctly (if sizing using min/max content sizes).
This is detailed in issue: #8206
There are potential workarounds to deal with this issue, e.g. assume that all children are in every row/column for the purposes of sizing, but this prevents some potentially desirable use cases.
One thing that seems desirable is to allow a wider/different syntax for rows/columns than is currently allowed for grid, e.g. masonry-template: repeat(auto-fill, auto).
(Above would measure all the masonry items, and select the best number of tracks to fit the content).
(Arguably above might be a better default than masonry-template: auto for example).
This isn't possible for grid-template for good reasons - but we could accept it for masonry.
One open question is if we need different track sizes or just one would suffice. All the designs I have personally seen have just one track repeated N times. Accepting just one track template would allow easier intrinsic sizing of spanners for example.
One addition which is currently missing with grid repeaters is the ability to clamp to a minimum / maximum number. This is more relevant with masonry. E.g. masonry-template: repeat(auto-fill, /* min */ 1, /* max */ 5, auto) would allow clamping to a maximum of 5 tracks which seems desirable from designs I've seen.
(this is probably a bad syntax but you get the idea).
Another missing in the current proposal is controlling the direction of the masonry flow. E.g. there are cases where you'd want the masonry axis to start from the block-end / inline-end.
This could be covered by a property similar to flex-direction , a simple (and likely understandable property) might be:
masonry-direction: row | row-reverse | column | column-reverse
(this would be similar to the originTop/originLeft controls in masonry.js https://masonry.desandro.com/options.html#originleft )
One issue with masonry style layouts is that things can easily be visually out of order, e.g. if the current tracks are [100px, 99px] the next masonry item would be placed in the 2nd track, when the first would be more natural. A potentially solution to this is some user defined "threshold" to "place within the first track within Xpx of the smallest track"
masonry-threshold: <length>
Things that aren't in this proposal vs. the current draft are:
- No ability to place a masonry item in an explicit row/column. I personally haven't seen people do this. Personally I don't think it's needed, and somewhat harmful accessibility wise.
- No ability to align individual tracks. As discussed: [css-grid-3] Masonry - align-tracks / justify-tracks not defined sufficiently - has issues with spanning items #8208 [css-grid-3] Masonry - align-tracks / justify-tracks potentially not desirable for accessibility. #8207 I personally haven't seen people do this in designs, current spec text leads to undesirable rendering. Personally I don't think it's needed - and bad for accessibility by default.
- The masonry-auto-flow:next feature. Personally I haven't seen any designs use this type of flow - and bad for accessibility.
- No masonry-template:subgrid we could add this. But it'd be a "simple" inheritance, the masonry element wouldn't get to participate in the sizing of the parent grid structure.
Ian
Activity
SebastianZ commentedon Jul 9, 2023
Separating masonry from grid layout was already discussed in #4650 and had a bunch of proponents, including me.
@argyleink, @meyerweb, @AmeliaBR, @rachelandrew, @JoshuaLindquist, @thomascallahan FYI
As indicated by the different properties @bfgeek introduces, masonry layout works differently than grid in several points. And separating its syntax from grid also allows adding features that only apply to it and not to grid and vice versa.
Sebastian
rachelandrew commentedon Jul 18, 2023
I've never been a fan of including masonry in grid layout. Mostly because we'd forever more be having to figure out how new grid stuff works with Masonry layout, which will always be a bit weird because a masonry layout is different to a grid layout.
The current spec makes masonry layouts do a whole bunch of extra things that I've not seen authors ask for, mostly because "why not?" once it's all bundled into grid. Without compelling use cases for these features, it feels as if we'd be adding future problems of needing to work round masonry for grid additions, plus making to harder to add masonry specific things.
I think this proposal achieves the things people want out of masonry, and avoids these pitfalls.
fantasai commentedon Jul 20, 2023
(Fwiw I could have sworn we had a resolution to adopt
column-reverseandrow-reversevalues forgrid-auto-flow, but I can't find it.)Que-tin commentedon Jul 20, 2023
I was actually searching for this quite a lot lately and didn't find a resolution on this and to be honest not even a draft. Would that be smth. for the Cupertino F2F Agenda as well @fantasai?
bfgeek commentedon Jul 20, 2023
So I don't forget - default alignment needs to be different for masonry as well. E.g. its desirable for replaced elements to stretch (vs. the grid default of start) similar to flexbox.
bfgeek commentedon Sep 7, 2023
Another thing so I don't forget - variable track sizes is problematic with items that span multiple tracks with "dense" packing. To correctly place the item in the correct tracks, you need O(N) layout passes on the item.
fantasai commentedon Sep 7, 2023
Breaking the specifics out into separate issues (not that this invalidates this one, or that listing them all here isn't useful, but still should have them filed individually):
column-reverseandrow-reversewere requested for Grid generally in [css-grid-1] Add more directional values to grid-auto-flow #3622SebastianZ commentedon Sep 7, 2023
For what it's worth, I do have a use case for that at https://www.gamestar.de. That page has two columns. And those columns contain different sections, which are placed explicitly in those columns. In horizontal direction the sections are meant to align on tracks while vertically they should be independent from each other. At the same time, they are explicitly placed in one of the tracks.
The two-column split is just one example. There are other pages which use three or even four columns.
So, this seems to be a perfect match for masonry layout.
And I believe, basically every website that is split into several columns could benefit from masonry layout.
Take MDN for example.
Instead of using Grid, its three-column layout could be done using masonry, i.e.
The point of using Masonry here is that the height of the items should not depend on each other nor on a grid.
So, I'd vote for a
masonry-trackproperty that allows to place items explicitly in a track (or even span over multiple ones) similar to howgrid-columnandgrid-rowwork.Sebastian
tabatkins commentedon Sep 7, 2023
That's not really masonry tho, right? They're not selecting the column based on available space; the stuff in the first column is meant for the first column, the stuff in the second is meant for the second.
Today this would just be a grid with two columns and one row, with all the stuff in each column wrapped in a container. To fix the general problem of wanting to flow a mixed set of contents into a single grid cell without having to pre-wrap them in a container div we have #9098
SebastianZ commentedon Sep 7, 2023
Right. That use case avoids the auto-placement algorithm altogether, like it can be done in Grid and Flexbox, already. The benefit over Grid is that they are only aligned on one axis and the benefit over Flexbox is that they don't rely on wrapping logic.
Correct. That's how it's solved right now in the first example.
Yes, that issues would also solve that use case. Though that use case seems to be a natural fit for masonry and less so for grid.
Sebastian
tabatkins commentedon Sep 7, 2023
What I mean is, since there is no column auto-selection at all, this isn't Masonry. Masonry's entire reason for existing is to allow you to place items according to the tracks' current fill height; if you're skipping that entirely, then Masonry might not be the right abstraction for this. This is especially true if reusing Masonry as the layout abstraction for this (and making changes to accommodate it) would harm more core use-cases.
On the other hand, nothing about this case is particularly unusual for Grid. New functionality is needed (flowing multiple items into one cell as if they were grouped into a container element), but it's immediately compatible with the rest of Grid, too; it doesn't force us to reshape anything else about grid to accommodate it. As far as Grid is concerned it's exactly like just having a container element filled with stuff, and that's exactly what the use-case needs and wants out of it.
So yes, in theory we could address some variations of this "flow multiple items into one grid cell" use-case with Masonry, but we can't hit all of them, and even for the ones we can do, it requires adding additional features to Masonry that harm the core use-cases.
SebastianZ commentedon Sep 8, 2023
I'd argue that this is not true. The spec's introduction says this:
This is the main use case for Masonry. The auto-placement algorithm is one aspect of it, like it is for Grid layout.
And restricting it to auto-placement seems arbitrary to me and is also hugely restricting its usefulness. Allowing to place items manually (in addition to auto-placement) opens Masonry up to a whole new class of use cases.
The use case is not "flow multiple items into one grid cell". (That's handily expressed so that it fits to #9098.) The use case is "have multiple tracks in one axis and stack items in the other axis".
Sebastian
112 remaining items
rjgotten commentedon Oct 1, 2024
If subgrid would be generalized across grid, masonry and other layouts capable of using tracks, then it's possible.
You do need the nested containing element which forms the 'sub' that uses masonry - and then in the scenario you describe where:
you simply mark that container as
display:contentsand its children become grid children instead ...Also - the exact layout you're describing is already possible today:
It's just a grid with a flex box in column direction set in the last column, spanning from row 1 to row -1.
nmn commentedon Oct 2, 2024
@rjgotten You're missing the point that all the children are direct children in a single container. Of course that layout is possible by using additional containers for the sidebar, and then using media queries to detect when it goes away and conditional using
display: contentsand then usingorderto move them between the grid elements. However, this would still break tab order.But again, as I said, this is a contrived example, so it feels extra dumb. But I think there is a chance this capability is useful and may enable other layouts that are actually desirable.
tabatkins commentedon Oct 2, 2024
@nmn
That talk is out-of-date on the state of the proposals; similar to the Apple blog post at the top of this issue thread, it states that some things are impossible in one syntax when that is no longer true. (Basically anything written more than a few weeks ago is now useless for the purpose of this conversation and should probably be ignored.) Both syntaxes are capable of expressing the same things; it's a question of what defaults are best, how easy it is to express things, how teachable it is, etc.
Also, and somewhat more importantly, the syntax has never been the reason that one proposal or another had or lacked a particular ability. Chrome had an objection to the functionality of the original Apple proposal, based on the way it was described to work; the syntax used for it was irrelevant. (The Chrome proposal's earlier syntax was more restrictive, so that things with bad performance couldn't be written; it was the perf that was the issue, not how it was written). We've since figured out a great compromise that allows the flexibility that the Apple proposal wanted, and the perf characteristics that the Chrome proposal tried to maintain.
nmn commentedon Oct 2, 2024
@tabatkins
First, let me defend Nicole's talk. It wasn't biased in the same way the webkit blog post was. It didn't favour either API proposal and intentionally tried to stray away from the topic entirely. It mostly presented the cool stuff that might be coming to CSS and asked for feedback. She also presented a few pieces of feedback she had received from various devs. None of these pieces of feedback moved me in favour of either proposal. They were generally ideas about things that neither API support as written today.
And so I just came to add to that conversation, not take part in this heated debate about which API anymore. It's not important enough to me. Even if the proposal I like less gets standardised, I will still be happy about it.
And so...
grid-row: span fit-contentfor grid layout. This is not relevant to the masonry proposal, but I mentioned it here, because I think it's loosely related.I know this debate has gotten quite heated and maybe even a bit ugly, so I understand your reaction. But despite my earlier comments taking sides in the debate, I've moved on.
xcrap commentedon Oct 3, 2024
👍 for display: masonry.
Do not mix concepts, a separate masonry it's the right way even it borrows some grid syntax.
alkorlos commentedon Oct 4, 2024
Think
display: masonry;would be clearer and more convenient for developers.Based on what I see, the main argument in favor of
masonry layout in grida few months ago was the concern that a lot of functionality would be lost ifdisplay: masonry;were implemented. Now, there are examples showing that the functionality of a separate specification would not left one specification.At the same time, the option of
masonry layout in gridhas many problems, which have been discussed above. The most important one for me — is that in amasonry layout in gridimplementation, there would be many properties and values valid only forgridor only formasonry, and to avoid this kind of behavior would be good.Сan't find Mozilla position on this issue. They created the first version of the
masonry layout in gridspecification and implemented it in an experimental browser version, but what do they think about the advantages and disadvantages ofdisplay: masonry;? It would be interesting to read their position.nmn commentedon Oct 4, 2024
I this repeated a lot but I don't see it summarised. All I know is that certain grid properties lose meaning in masonry.
grid-row: 1doesn't make sense in masonry because there are no rows.What else? What's a masonry-only property?
tabatkins commentedon Oct 4, 2024
Any two-dimensional value doesn't make sense in Masonry; it needs to be either re-interpreted as 1d or (perhaps partially) ignored.
grid-row(when you have a column masonry), and the correspondinggrid-row-startandgrid-row-endpropertiesgrid-areausing the 4-value syntaxgrid-template-areasdefining more than one row and columngrid-auto-rows(when you have a column masonry)grid-auto-flowall currently values are useless (Masonry always auto-flows and doesn't dodense). The*-reversekeywords introduced in Grid 3 do make sense in Masonry, but one of them is useless depending on whether you have a row or column masonry. (And, as currently defined, the one that actually works is confusing: a column masonry usesrow-reverseto break column ties from right to left, because a Grid would userow-reverseto indicate that its rows are filled from the right to left. But a column masonry doesn't have rows, it just fills its columns in a directly reminiscent of a Grid filling its rows.)display:masonrytoo)As for Masonry-only:
masonry-slackdetermines when there are ties in masonry placement; it does nothing in Gridrepeat(auto-fit, auto)) only works in Masonrynmn commentedon Oct 5, 2024
Thanks @tabatkins!
This all makes sense. I do think that
repeat(auto-fit, auto)support should be added to grid as well.rjgotten commentedon Oct 7, 2024
It's already been explained why that's not really viable performance-wise.
Yusuf-KaYeht commentedon Oct 7, 2024
Im also definitely going with
display: masonry;.Even tho it would bring even more css declarations that might make it confusing to some people.
Like ``grid-template-rows
tomasonry-template-tracks`With
display: grid; grid-template-rows: masonry;(or as I would prefercollapseas the keyword) is a lot simpler to remember for horizontal and vertical layouts (template-rows/template-columns).But as long as we have the same capabilities and features with the “display: masonry” approach, and even save the developers implementing it some headaches and browser performance, I'm all for it.
nmn commentedon Oct 8, 2024
@rjgotten Yes, but I disagree with the proposed way it's supposed to work in grid. Anyway, that's a discussion for another time.
ethanjv commentedon Oct 15, 2024
Hi everyone! Last week I presented a breakout talk at BlinkOn 19 showcasing some of the unexpected behaviors that the current spec for masonry might surface for web developers (here's a link to the slides).
I believe the examples provided are good points to keep in mind while debating whether we should make masonry its own display type or not, since these behavioral differences will be surfaced in some of the more common scenarios.
korenevskiy commentedon Oct 15, 2024
The choice between displaying
grid:masonryandtemplate-row: masonryis difficult. It's actually quite simple. It is necessary to understand the common problem of both approaches. The fact is that both approaches are not 100%. Let's combine both approaches.Let's take the best of both approaches
most people are bothered by the templatecolumn:masonry (
Display:GRID) scheme, what needs to be written isrow-reverse.NO:
But we will write.
YES
Now it will not be confusing that when specifying the direction of the columns we write lines, now we will just write a change in direction.
Let's take the best of both options.
or you can just write REVERSE
We can already see that this is a property of direction. Let's just write REVERSE in it
YES
You can also write an abbreviated version of the reverse .
There are no problems at all with the fact that you have to write 4 lines, an abbreviated version has always been present in the GRID display.
Can you please rate my suggestions????