Live data from Hacker News

New layouts with CSS Subgrid

joshwcomeau.com

61–70 of 104 posts

Re: New layouts with CSS Subgrid

#61

From the example: .grid { display: grid; grid-template-columns: 35% 1fr 1fr 1fr; } .grid header { grid-row: 1 / 3; } .grid ul { grid-row: span 2; grid-column: span 3; display: grid; grid-template-rows: subgrid; grid-template-columns: subgrid; } I swear you that I had less hard time reading x86 assembly code or even c++ templates. I mean what the hell? Then if you go look at another example: > @media (max-width: 32rem…

[deleted]

Re: New layouts with CSS Subgrid

#62
post #57
post #8

Earlier quoted context omitted.

I also wasn't understanding the value looking at the first two examples, but the pricing packages example I do think I would struggle to implement in a clean way using traditional css.

I'd use . It literally is a table. And for mobile just do a media query to turn it into a flexbox.

Yeah the claim that tables are hard to style for such a simple design doesn't really hold up in my opinion.

This took two seconds to make https://codesandbox.io/p/sandbox/5ry4rl

I do agree though that subgrid makes the HTML more readable though so I'm all for it.

Re: New layouts with CSS Subgrid

#63
Ah yes, a new css concept! I love this kind of article that invariably contains this kind of statement:

> This is mind-bending stuff, but it becomes intuitive with a bit of practice.

The problem is not the language, it's just that you did not spend enough time to learn it the proper way.

Re: New layouts with CSS Subgrid

#64

Earlier quoted context omitted.

Yes and no. layouts were a hack that solved a real problem but came with massive downsides. People didn’t tell you to not use to lay out content because grids are bad (they are quite handy! take a look at Grid Systems by Josef Müller-Brockmann) but because both posed technical and accessibility problems. A layout grid is not a table (or a ). A table (with and without ) comes with attached semantics, hierarchy, readin…

> A layout grid is not a table Ain't it? Rows and columns get you a table.

A table is a grid, but a grid does not have to be a table

Re: New layouts with CSS Subgrid

#65
post #24

Earlier quoted context omitted.

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?

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 tags so I use much less unnecessary ones)

Re: New layouts with CSS Subgrid

#66
The pricing UI example is exactly the type of thing I had to build a few years ago. Deceptively simple, two tables for comparison, but the rows need to line up.

Impossible without subgrid, either you need to have fixed heights or calculate heights with JS, but neither is elegant or simple, especially if you have react components and adhere to modular design, and you need to have those components agree on sizing.

Re: New layouts with CSS Subgrid

#67
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.

You have just restated the similarity I referred to. The ways they are different make them important enough to distinguish.

“Tabulate” doesn’t just mean organising anything by rows and columns, it means organising data for a particular purpose. And layout grids usually end up looking quite different to tables because although they have a broadly similar underlying structure, the purpose is quite different.

Re: New layouts with CSS Subgrid

#68

This sounds useful, but the example of the feature rows reminds me how sad it is that CSS sometimes requires adding information about the document structure to make a layout work. In this case the number of rows.

Ideally, we would have a way to align elements even when they don't share a parent. Or maybe a flex container that can have its layout mimic another flex container so the distribution in them can line up. It seems that there are a lot of heuristics and edge cases though to keep a simple DX.

Re: New layouts with CSS Subgrid

#69
post #43
post #9

is grid intended to replace flex at some point or live side by side

Live side by side unfortunately. I personally however always use grid, flexbox sucks.

Flexbox is great for stacking and distributing elements vertically or horizontally, especially when you don't know how many there are.

Re: New layouts with CSS Subgrid

#70
post #58

Earlier quoted context omitted.

Another way to think about it: flexbox is for alignment of boxes in one dimension: horizontally or vertically. CSS Grid is for two dimensional layout of rows and columns. Back in the day, developers wanted page layout instead of the hacks on top of hacks with table-based layouts, floats and positioning to create layouts. We’ve had CSS Grid designed for page layout on the web, in all browsers since 2017; as of 2022, o…

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.
Post reply on HN