News for hackers. Stuff that matters.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by achairapart on Tuesday April 23, 2024 @08:35AM (#40131219)
So, the background story is that CSSWG DevRels from browser vendors are debating how to formally include the Masonry layout into CSS, at least since 2020, when Firefox first proposed it.

The news here is that people at WebKit decided to push the debate to the public, inviting designers and developers to take some action (“post to social media, write blog posts”), in order to get past this.

While it may look just like a formality, I think this will make an important precedent. The real underling debate here is either to treat every layout option as a part of the CSS Grid, or keeping adding new CSS Display proprieties as necessary.

The first option will make even more complex the (already convulsed, IMHO) CSS Grid specs, the latter will bloat the CSS specs with a load of new proprieties (and related sub-proprieties).

Either way, it's not as easy as it looks.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by mvonballmo on Tuesday April 23, 2024 @09:41AM (#40131869)
A "row-less" grid fits very well in the current CSS Grid specification, in that it can reuse the very powerful column-definition property as well as sub-grids. Their examples very convincingly show how orthogonal these features are.

Mostly, you just write grid-row-template: masonry and everything else just works with it. This is nice. It doesn't become harder to use the grid layout than it already is IMHO.

The drawback is mostly for browser-engine authors, for whom the bar for "fully supports CSS Grid" will be set even higher. They also mention that it might avoid "performance traps" where an implementation that needs to support all features of grid might be slower at some parts of grid layout than it would be if the specification were simpler.

If there were a separate display mode, then you'd have to repeat the grid-column specification for the masonry layout, which seems a shame.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by nightpool on Tuesday April 23, 2024 @10:42AM (#40132535)
> If there were a separate display mode, then you'd have to repeat the grid-column specification for the masonry layout, which seems a shame.

Why? What's the downside to reusing grid-column-template or whatever for the CSS Masonry spec?

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by krsdcbl on Tuesday April 23, 2024 @01:32PM (#40134637)
my thoughts exactly - i think this points to a possible naming issue, since "masonry" is a relatively vague alias that does not really describe the underlying layouting logic, it just became ubiquitous enough for people building webstuff to understand because of that one jQuery plugin back in the days.

A better approach might be to lean on the "grid" naming, but still silo it off via an own display directive (a bit like "block" and "inline-block" have shared properties, but also mutually exclusive behaviours)

So maybe an own `display: flex-grid;` could be an interesting solution?

This separate layout mode would avoid "result-specific" nomenclature like "masonry", and could lean on both flexbox & grid to achieve that look: - using `grid-auto-flow` to set a "masonry axis" & distribution logic - using _either_ `grid-template-columns` or `grid-template-rows` to specify the "lanes" - and to make my frankensteinian display-mashup even worse (or genius! for you to decide), the grid items could in turn abuse `flex-grow, flex-shrink, flex-basis` to control their height/width within the main "masonry" axis

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by beardedetim on Tuesday April 23, 2024 @05:23PM (#40137464)
I really like this concept of `flex-grid` because that's exactly what this display would be.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by datavirtue on Wednesday April 24, 2024 @12:36PM (#40146541)
That would be too simple and clean.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by afiori on Wednesday April 24, 2024 @10:27AM (#40144797)
> And yes, while making CSS Grid more complex will make it harder to extend in the future, we believe there’s an advantage to having these two types of grid layouts intertwined. This way the CSS Working Group will always define all new additions for both modular and columnar grids. There won’t be something added to display: grid that will be left out of display: masonry, or vice versa. For example, many developers want CSS Grid Level 4 to provide a mechanism for styling grid areas and grid lines — perhaps a way to add a background color to a track, or create a rule line in a gap. It’d be great to ensure that will work for both modular and columnar grids from Day 1.

> By using subgrid, we can put the year and catalog number on the right of each card — and line up this data for one painting with the same data for the other paintings.

The downside is that all the parts of grid that might be useful in masonry would have to be duplicated in slightly different ways.

This together with the fact the the only two current implementations use display: grid

If it is possible to make them perfectly compatible then the main negative side

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by eyelidlessness on Tuesday April 23, 2024 @11:16AM (#40132897)
> Either way, it's not as easy as it looks.

And exploring the tradeoffs might lead those exploring to reconsider their preferences going in.

Not directly related to the topic, but I recently did some prototyping on a second iteration of an interface with a similar tension—proliferation of similar but distinct types in a data model, or expanding the set of nuances within each existing type to support refinement within it. Going into that prototyping session, I had a very strong bias in favor of the latter. But after exploring the options, I found it vastly simpler to consume the “bloated” interface, and to reason about the resulting application code consuming it.

I don’t have strong feelings on the topic as it applies to CSS masonry, but I suspect there could be similar surprises in how people think about this tension intuitively versus in practice.

And while I think CSS in particular will have a hard time justifying “bloat” (proliferation of use case specific semantics), I think it’s possible that users do tend to find more difficulty using CSS’s denser APIs (like grid).

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by qingcharles on Tuesday April 23, 2024 @11:29AM (#40133061)
It's great they're doing this. I've been nudging everyone involved with this since last year. The Chrome guys are the furthest behind (no support). FF has flag support.

I've been testing it since last year on FF and Safari and have no complaints about their implementations. I know some people are sniffling about the placement and naming of the properties. You have to be pragmatic about it, though, and realize there probably isn't a perfect solution and just implement it.

I refuse to use JavaScript for the fallback, so the fallback is a lot of ugly CSS that doesn't order things in the right way, but it doesn't make a big deal of trouble for the project I'm working on. Most people will fallback to JavaScript right now, though, and when JavaScript is your solution to layout then you're already on a loser.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by aidog on Tuesday April 23, 2024 @11:57AM (#40133429)
look at the examples they made why it should be in CSS grid. I totally agree it should be in that. It's a form of grid. It should be part of CSS grid.

This is one of the layouts that were just impossible in CSS and we really need it soon. I also needed the feature they talk about with the background colors for css grid tracks in 2012. I solved that with DIVs, but it was hard to make it responsive.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by doctorpangloss on Tuesday April 23, 2024 @11:51AM (#40133345)
Is there a JavaScript polyfill today that can read the CSS properties in any of the proposed / implemented specifications that Just Works in Chrome?
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by reactordev on Tuesday April 23, 2024 @12:35PM (#40133880)
CSS Grid is a mistake. Baking in a design philosophy when we had really creative pages without it.

I like grids for pages with content and documentation and such. I don’t like grids for pages of media or other creative content. Cards, flex box, grids, these are all design framework choices and shouldn’t be in CSS spec. We were totally fine with position relative and media-query centric widths and heights. I get that it’s easier to standardize if grids are part of the standard but I miss the days of pages having some creativity in their design. Every site looks the same nowadays.

And before I get downvoted. I have a degree in graphic design. I love CSS and I love the web. Bring back the fun.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by fatbuoy1 on Tuesday April 23, 2024 @01:05PM (#40134280)
Sure what was wrong with table layouts and spacer gifs anyway amirite?

I’m a designer who codes, and CSS Grid has completely transformed (and simplified) my approach to responsive web design, and enabled much more complex layouts as a result. Just because a layout was possible before doesn’t mean it was easy.

By all means do things the old way if you want, but to suggest that it would be better if it didn’t exist is insane.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by reactordev on Tuesday April 23, 2024 @04:54PM (#40137143)
Not that it wouldn’t exist. It just wouldn’t be part of the spec. You are free to declare your own grid.

I’m also not advocating to bring back tables and invisible spacer gifs. We have margins and padding for that. Even borders…

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by rhdunn on Tuesday April 23, 2024 @12:40PM (#40133936)
CSS grid makes it possible to implement wrapped content that is uniformally aligned with the last row keeping the same number of columns as the others, and have that scale correctly to different page widths. This is useful in things like book layouts. You can get close with flex, but the last row doesn't fully work.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by reactordev on Tuesday April 23, 2024 @04:57PM (#40137170)
Does it require a ::last-child hack to get working? I’m a fan of grids when it comes to heavy writing content like I mentioned but I still don’t think it should all be packed in the spec.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by rhdunn on Wednesday April 24, 2024 @03:24AM (#40141620)
No, just:

    main.books {
        display: grid;
        grid-template-columns: repeat(auto-fill, 180px);
        grid-gap: 1rem;
        justify-content: space-between;
    }
That will make the child items 180px wide, repeat as many children in a row as needed to fill the width with a gap of atleast 1rem extended so that the items are justified to the left and right and are spread evenly. The last row has however many children are left with those children positioned to match the rows above.

See https://stackoverflow.com/a/46099319.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by reactordev on Wednesday April 24, 2024 @10:53AM (#40145085)
I meant without CSS Grid.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by rhdunn on Wednesday April 24, 2024 @02:59PM (#40148167)
Ah, ok. I wasn't able to get the last row working right with flex, but didn't experiment too much as the CSS grid approach worked. You could look through the other answers in that StackOverflow question.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by reactordev on Wednesday April 24, 2024 @03:15PM (#40148358)
I have. It’s a mix of people saying it’s can only be done with CSS grid semantics and then a few who prove them wrong with flex box. Reinforcing my above question about “why not :after? :last-child? And flex it into shape?”

But I get it. Grids are easy to grok and grid.css is for the birds. Bake it in. Bloat the spec. And allow pixel perfect precision with one (or two) less div.

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by krsdcbl on Tuesday April 23, 2024 @01:44PM (#40134785)
It was "fun" when tinkering with some really creative special concern, it was an insufferable pain when your job was finding ways to layout all the "fun" designers envisioned who weren't highly familiar with CSS.

Before position and float madness and all, we used to abuse tables -- because layouts for complex information do work best in grid systems. Now CSS finally has a module that serves this purpose, and brings a huge amount of flexibility to make formerly painful stuff easy.

I'm not even talking about "page layouts" as a whole, just simple particle patterns like "a big icon with a title + description to it's right" is so so much cleaner and easier to do in markup AND css with grid.

Embrace the tools, and if you dislike them - you may as well just not use them. But thinks like grid make simple, stupid, deadlined webdesign _work_ so so much less of a grind.

(speaking as a General Graphic Designer who's also been deep, deep in CSS since before 2.1)

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by bfgeek on Tuesday April 23, 2024 @01:52PM (#40134887)
Part of the tension with building masonry on top of grid is that they work in fundamentally different ways.

Grid you place everything in the grid first (e.g. an item goes in col:2,row:3), then size the grid. Masonry ideally you want to size the tracks first, then place items in those tracks.

The first Firefox implementation (and the spec at that stage) basically said you don't consider any masonry items for sizing tracks (except for the first row, and some other rules - its complex). This meant that it is trivial to create items that overflow their tracks.

The specification at the moment asks to place every item in every possible track. This has quadratic performance O(N_tracks * N_items) in the worst (and somewhat common case). Quadratic performance is bad[1] and we don't really have this in other layout algorithms.

With nesting introduced the performance goes (sub-)exponential, which is really poor, even if you have a fast CPU.

One may argue that these cases aren't common, but folks always test the boundaries with layout modes in CSS - so things need to be fast by default.

Note: In grid items size themselves differently depending on what tracks you place them in, which is why you need to place in every possible position.

Masonry potentially needs a different algorithm for sizing tracks to mitigate these problems, (the blog post doesn't go into these issues in sufficient detail). There may have been a version of grid sizing which didn't have item positional dependence but that ship has sailed.

[1] https://randomascii.wordpress.com/2019/12/08/on2-again-now-i...

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by JoshTriplett on Tuesday April 23, 2024 @04:52PM (#40137122)
Why does this require checking every item in every column? It looks like the layout algorithm greedily picks the column with the least total height so far, every time it adds an item.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by bfgeek on Tuesday April 23, 2024 @05:32PM (#40137556)
The requires it for sizing the columns (the step before placing the items into the columns).
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by JoshTriplett on Tuesday April 23, 2024 @06:27PM (#40138089)
The examples in the article seem to have CSS that directly sizes the columns, albeit with some flexibility such as being able to change the number of columns to better fit the width. It seems like the item widths depend on the column widths, rather than the other way around (which seems like it'd be circular). What's an example where the column widths depend on the items?
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by bfgeek on Tuesday April 23, 2024 @06:48PM (#40138252)
If you have something like:

  columns: 1fr auto max-content

  <!-- items -->
  <div>1</div>
  <div>somethingsuperlong</div>
  <div style="span: 2;">something else</div>
You first need to decide how big each of the three columns are going to be, then you want to place the items in each of the columns.

Depending on the non-fixed column type (there are different rules for each type) you want to ensure their constraints are satified (typeically they are big enough to encompass the items).

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by JoshTriplett on Wednesday April 24, 2024 @08:20AM (#40143501)
Thanks, that helps; I'd thought about variable numbers of columns based on the width of what they're contained in, but hadn't thought about the possibility of wanting to choose the number of columns to fit their content better. Mostly because I was imagining content like images that can be scaled or text that can be wrapped, rather than fixed-size content or content with a minimum necessary size.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by fantasai on Tuesday April 23, 2024 @03:48PM (#40136409)
Quadratic performance is a bit of an exaggeration. It's not O(N_items^2). It's N_tracks x N_items, and basically nobody has N_tracks ≈ N_items. Practically speaking, the upper limit is closer to (N_items^1.5) because N_tracks is unlikely to go over sqrt(N_items) in cases where N is large. And almost all of those layouts will have repetitive track sizing patterns, so they can be optimized to a much smaller N_tracks that approaches O(N_items).
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by afiori on Wednesday April 24, 2024 @10:19AM (#40144690)
Grid has a rich functionality for autoplacement of items and one of the arguments for masonry being part of grid is that you can easily mix the two positioning approaches

https://webkit.org/wp-content/uploads/video3-museum-dark.m4v

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by matsemann on Tuesday April 23, 2024 @02:00PM (#40134996)
I think one additional point to consider when adding it as a new display mode vs re-using grid, is that currently these demo's fallback quite nicely now when it's re-using grid. Instead of showing it as masonry (since my browser doesn't support it), it at least behaves as a grid. If it's a new display: X property, it will fallback to div behavior with the content all over the place in unsupported browsers.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by laszlokorte on Tuesday April 23, 2024 @02:12PM (#40135165)
you could simply do: "display: grid; display: masonry;" and all not-supporting browsers would skip the second declaration.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by yurishimo on Tuesday April 23, 2024 @04:24PM (#40136830)
This is assuming there isn’t any cross pollution of shared properties either.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by danielheath on Tuesday April 23, 2024 @08:08PM (#40138870)
`@supports masonry { shared-property: 1; }`
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by jjcm on Tuesday April 23, 2024 @02:16PM (#40135218)
> I think this will make an important precedent

It's worth noting this is not the first time things like this have gone to the community for feedback. They did the same for nested CSS selectors, which ended up working quite well feedback-wise: https://webkit.org/blog/13607/help-choose-from-options-for-c...

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by Izkata on Tuesday April 23, 2024 @03:07PM (#40135891)
> new proprieties

I'm guessing this was going to be "properties"? It kinda looks like "proprietary" instead, so let's double down on that - I propose "display: tetris;"

Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by falcor84 on Tuesday April 23, 2024 @04:22PM (#40136811)
I would suggest that `display: tetris;` is implemented such that each row SHOULD have whitespace, and if at any point a row becomes fully occupied, a conforming rendering agent MUST remove the entire row.
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by flakiness on Tuesday April 23, 2024 @04:33PM (#40136926)
If there is a debate, who is pushing back? Or is it more about details while every vendor is supportive at high-level?
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by madeofpalk on Tuesday April 23, 2024 @06:22PM (#40138060)
It seems like the precursor to this was the whole "Help choose the syntax for CSS Nesting" thing that happened a while back. Browser vendors couldn't figure out the syntax, so they asked the 'wider public' which they preferred https://webkit.org/blog/13607/help-choose-from-options-for-c...
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by pier25 on Tuesday April 23, 2024 @07:58PM (#40138804)
I don't know about the implementation but it makes zero sense that masonry should be part of grid. It's not a grid!
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by eternityforest on Wednesday April 24, 2024 @06:11AM (#40142555)
I've yet to ever find a use case for grid.... I'd prefer this be a flex box feature so I can continue ignoring grid. But it seems like they have good reasons for including it, so I suppose I'll have to actually practice with grid more!
Re:Help us invent CSS Grid Level 3, a.k.a. "Masonry" layout by niutech on Thursday April 25, 2024 @02:44AM (#40154241)
Masonry is easy and has already been solved using CSS Multiple Columns:

    .masonry {
      column-count: 6;
      column-gap: 10px;
    }