Live data from Hacker News

New layouts with CSS Subgrid

joshwcomeau.com

71–80 of 104 posts

Re: New layouts with CSS Subgrid

#71
post #7

How is this different to, and better than using nested grids?

If you nest two grids, the rows and columns of two sibling grids are not forced two align.

With subgrid the rows and columns of two sibling grids are aligned with each other by glueing them to the rows and columns of the parent grid.

Re: New layouts with CSS Subgrid

#72
post #58

Earlier quoted context omitted.

I use flexbox for grid purposes, simply because the syntax is straightforward and easy to read. Yeah, it’s one dimension, but if you nest it, it becomes two with no issues.

With nested flexbox the nested dimensions are not aligned to each other. With grid the items are aligned across both each row AND each column. With subgrid even nested grids can be aligned across nesting levels.

True. But so far I haven't faced layouts that I could not implement using flexbox.

Re: New layouts with CSS Subgrid

#73
post #24

Earlier quoted context omitted.

Use for tabular data, but for layout you should use grid. Grid doesn't have it's own element like table does, so you have to use css to apply that display to a div. CSS takes a bit of time to understand. It's cascading nature and how certain properties behave differently based on the html structure or display type or direction makes it tricky. I don't blame you sticking with tables for layouts for yourself - making l…

> Grid doesn't have it's own element like table does, so you have to use css to apply that display to a div. Well, OOTB, yeah. I personally like to make use of custom html elements a lot of the time for such things. Such as etc, and apply css styles to those, rather than putting in classes onto divs. Feels a lot more ergonomic to me. Also gives more meaningful markup in the html. (and forces me to name the actual tag…

One of the many things I hate about React: can't easily create custom elements that truly exist in the DOM so I can style them in CSS.

Re: New layouts with CSS Subgrid

#74
post #42

Earlier quoted context omitted.

A table is for tabulating data. They have quite different meaning and purpose, even if they share a couple of characteristics.

HTML spec couldn’t just have added a grid element?

I think CSS grid is too powerful to be represented in markup. I rotated the idea in my head for a bit but the most I could come up was elements that covered a small subset of CSS grid and which completely lost the entire appeal of being able to handle tracks dynamically.

Re: New layouts with CSS Subgrid

#75

Random grid gotcha that drove me crazy some time ago: due to browser bugs we can't use elements with percentage widths or heights as grid items. The grid cell dimensions get blown out to the ones of the original image. Seen in both Firefox and Chromium. Relevant FF bug is probably https://bugzilla.mozilla.org/show_bug.cgi?id=1857365 ' grid item with percentage height, "width: auto", "grid-template-columns: auto", and…

so if the img has a specific size set in width and height attributes or via css, and that size is not percentile or auto, the problem doesn't exist? I'm just confused by the "original image's width".

not OP but (unless silently edited) they wrote

> " elements with percentage widths or heights"

Re: New layouts with CSS Subgrid

#76
post #42

Earlier quoted context omitted.

A table is for tabulating data. They have quite different meaning and purpose, even if they share a couple of characteristics.

Tabulate means to organize by rows and columns. Layout grids organize data by rows and columns.

They don't! Layout grids are less about the rows and columns and more about the lines separating them (which is why those get a lot of attention in CSS grid). Take a look at how layout grids are used in design and you will quickly find examples that are extremely inconvenient to realize with HTML tables. I'm sure it can be done and I'm sure some poor email marketing dev had to, but the result would be entirely static and not able to reflow.

Re: New layouts with CSS Subgrid

#77
Am I the only one who sees "content boxes"/divs with content displayed in different widths as poor design? At least in the example given, I would think you would want the image and its associated content box to be the same size for all four and not have its content vary in width based on how much content it has.

But in terms of functionality, I'm sure there are plenty applications for this!

Re: New layouts with CSS Subgrid

#78

Subgrid is really cool, but I want to note that for the first trivial example, you could make the children participate in grid layout by doing ul { display: contents } it's more efficient, if you don't need subgrid features, but still want the nested element structure for other reasons.

Yes, for that specific example it works. But in general, this essentially deletes the UL from the layout entirely. It won't be stylable[0] and it won't dispatch UI events that occur on it specifically. One example of a reason you might want such an element to still participate in layout is to use that element as an area highlighter. Or you might make it a scrollable section; subgrid makes sticky-header tables rather…

Yeah, though I find in practice I use `display: contents` far more than I do subgrid. Contents effectively deletes the node for layout purposes, but it still participates in the CSS cascade so you can use it in selectors, even `:hover` works when a child gets hovered, or use it as the origin for inheritable properties.

Re: New layouts with CSS Subgrid

#79

Earlier quoted context omitted.

One subtle thing worth adding is that display: contents also changes how accessibility trees are constructed. The element is removed from the visual layout and from the accessibility tree in many browsers, so semantics like list grouping, landmarks, or ARIA roles can disappear unless you re-introduce them manually. That’s why subgrid ends up filling a different niche: you preserve the DOM structure, preserve accessib…

Yeah this is a good callout. My understanding is that display: contents is not meant to impact the accessibility tree but there is a long and ongoing history of browser bugs that make me not want to use it for elements that have an accessible role

From my testing, as far as I've been able to tell it no longer has any impact on accessibility. The element itself does not appear in the tree, this makes sense display:contents is non-interactive. But all of the children correctly appear in the accessibility tree as if they did not have that shared parent element. But I am by no means an expert at operating screen readers, do you know of any specific issues with it?

Re: New layouts with CSS Subgrid

#80
post #13

Earlier quoted context omitted.

Yeah, to expand on that... Flex is, well, flexible, whereas Grid is more rigid like a table. The rigidity of Grid allows you to span rows and columns (2D) just like you can with table cells (colspan/rowspan). Grid is usually used at a macro level for its more deterministic layout (no unintuitive flex quirks), while flex is usually used to lay things out at a component level where you don't care that the next row of i…

So is Grid supposed to be what we should use to replace the html element? That I still use to this day for layouts because CSS still sucks to me?

It's more like a comic book, you define the layout and the elements slot into that. You can define how many rows and columns your comic has and then you can make some panels fit exactly into one spot, or you can have panels that span more than one row or column. So it's more of a 2d design system.

https://l-wortley0811-dp.blogspot.com/2010/10/comic-layoutsj...

Post reply on HN